Documentation ¶
Overview ¶
Package graphqlhttp provides functions for serving GraphQL over HTTP as described in https://graphql.org/learn/serving-over-http/.
Example ¶
package main import ( "log" "net/http" "zombiezen.com/go/graphql-server/graphql" "zombiezen.com/go/graphql-server/graphqlhttp" ) // Query is the GraphQL object read from the server. type Query struct { Greeting string } func main() { // Set up the server. schema, err := graphql.ParseSchema(` type Query { greeting: String! } `, nil) if err != nil { log.Fatal(err) } queryObject := &Query{Greeting: "Hello, World!"} server, err := graphql.NewServer(schema, queryObject, nil) if err != nil { log.Fatal(err) } // Serve over HTTP using NewHandler. http.Handle("/graphql", graphqlhttp.NewHandler(server)) http.ListenAndServe(":8080", nil) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
Parse parses a GraphQL HTTP request. If an error is returned, StatusCode will return the proper HTTP status code to use. Parse may return a validated query, but it may not always do so.
Request methods may be GET, HEAD, or POST. If the method is not one of these, then an error is returned that will make StatusCode return http.StatusMethodNotAllowed.
func StatusCode ¶
StatusCode returns the HTTP status code an error indicates.
func WriteResponse ¶
func WriteResponse(w http.ResponseWriter, response graphql.Response)
WriteResponse writes a GraphQL result as an HTTP response.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler serves GraphQL HTTP requests by executing them on its server.
func NewHandler ¶
NewHandler returns a new handler that sends requests to the given server.