-
Notifications
You must be signed in to change notification settings - Fork 493
/
example_test.go
54 lines (45 loc) · 937 Bytes
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package graphql_test
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/graph-gophers/graphql-go"
)
type exampleResolver struct{}
func (*exampleResolver) Greet(ctx context.Context, args struct{ Name string }) string {
return fmt.Sprintf("Hello, %s!", args.Name)
}
// Example demonstrates how to parse a GraphQL schema and execute a query against it.
func Example() {
s := `
schema {
query: Query
}
type Query {
greet(name: String!): String!
}
`
opts := []graphql.SchemaOpt{
// schema options go here
}
schema := graphql.MustParseSchema(s, &exampleResolver{}, opts...)
query := `
query {
greet(name: "GraphQL")
}
`
res := schema.Exec(context.Background(), query, "", nil)
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
err := enc.Encode(res)
if err != nil {
panic(err)
}
// output:
// {
// "data": {
// "greet": "Hello, GraphQL!"
// }
// }
}