GitHub - graphql-go/handler: Golang HTTP.Handler for graphl-go (original) (raw)

Golang HTTP.Handler for graphl-go

Usage

package main

import ( "net/http" "github.com/graphql-go/handler" )

func main() { schema, _ := graphql.NewSchema(...)

h := handler.New(&handler.Config{
    Schema: &schema,
    Pretty: true,
    GraphiQL: true,
})

http.Handle("/graphql", h)
http.ListenAndServe(":8080", nil)

}

Using Playground

h := handler.New(&handler.Config{ Schema: &schema, Pretty: true, GraphiQL: false, Playground: true, })

Details

The handler will accept requests with the parameters:

GraphQL will first look for each parameter in the URL's query-string:

/graphql?query=query+getUser($id:ID){user(id:$id){name}}&variables={"id":"4"}

If not found in the query-string, it will look in the POST request body. The handler will interpret it depending on the provided Content-Type header.

Examples

Test

$ go get github.com/graphql-go/handler $ go build && go test ./...