1# graphql-go-handler [](https://travis-ci.org/graphql-go/handler) [](https://godoc.org/github.com/graphql-go/handler) [](https://coveralls.io/github/graphql-go/handler?branch=master) [](https://gitter.im/graphql-go/graphql?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
2
3Golang HTTP.Handler for [graphl-go](https://github.com/graphql-go/graphql)
4
5### Notes:
6This is based on alpha version of `graphql-go` and `graphql-relay-go`.
7Be sure to watch both repositories for latest changes.
8
9### Usage
10
11```go
12package main
13
14import (
15 "net/http"
16 "github.com/graphql-go/handler"
17)
18
19func main() {
20
21 // define GraphQL schema using relay library helpers
22 schema := graphql.NewSchema(...)
23
24 h := handler.New(&handler.Config{
25 Schema: &schema,
26 Pretty: true,
27 GraphiQL: true,
28 })
29
30 // serve HTTP
31 http.Handle("/graphql", h)
32 http.ListenAndServe(":8080", nil)
33}
34```
35
36### Details
37
38The handler will accept requests with
39the parameters:
40
41 * **`query`**: A string GraphQL document to be executed.
42
43 * **`variables`**: The runtime values to use for any GraphQL query variables
44 as a JSON object.
45
46 * **`operationName`**: If the provided `query` contains multiple named
47 operations, this specifies which operation should be executed. If not
48 provided, an 400 error will be returned if the `query` contains multiple
49 named operations.
50
51GraphQL will first look for each parameter in the URL's query-string:
52
53```
54/graphql?query=query+getUser($id:ID){user(id:$id){name}}&variables={"id":"4"}
55```
56
57If not found in the query-string, it will look in the POST request body.
58The `handler` will interpret it
59depending on the provided `Content-Type` header.
60
61 * **`application/json`**: the POST body will be parsed as a JSON
62 object of parameters.
63
64 * **`application/x-www-form-urlencoded`**: this POST body will be
65 parsed as a url-encoded string of key-value pairs.
66
67 * **`application/graphql`**: The POST body will be parsed as GraphQL
68 query string, which provides the `query` parameter.
69
70
71### Examples
72- [golang-graphql-playground](https://github.com/graphql-go/playground)
73- [golang-relay-starter-kit](https://github.com/sogko/golang-relay-starter-kit)
74- [todomvc-relay-go](https://github.com/sogko/todomvc-relay-go)
75
76### Test
77```bash
78$ go get github.com/graphql-go/handler
79$ go build && go test ./...