-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain-2.go
69 lines (61 loc) · 1.26 KB
/
main-2.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"context"
"encoding/json"
"fmt"
graphql "github.com/graph-gophers/graphql-go"
)
// Define a schema string:
const schemaString = `
# Define what the schema is capable of:
schema {
query: Query
}
# Define what the queries are capable of:
type Query {
# Generic greeting, e.g. "Hello, world!":
greet: String!
}
`
// Define a root resolver to hook queries onto:
type RootResolver struct{}
// Define the greet: String! query:
func (*RootResolver) Greet() string {
return "Hello, world!"
}
// There are two ways we can define a schema:
//
// - graphql.MustParseSchema(...) *graphql.Schema // Panics on error.
// - graphql.ParseSchema(...) (*graphql.Schema, error)
//
// Define a schema:
var Schema = graphql.MustParseSchema(schemaString, &RootResolver{})
func main() {
query := `{
greet
}`
//
// You can also use these syntax forms if you prefer:
//
// descriptiveQuery := `query {
// greet
// }`
//
// moreDescriptiveQuery := `query Greet {
// greet
// }`
ctx := context.Background()
resp := Schema.Exec(ctx, query, "", nil)
json, err := json.MarshalIndent(resp, "", "\t")
if err != nil {
panic(err)
}
fmt.Println(string(json))
// Expected output:
//
// {
// "data": {
// "greet": "Hello, world!"
// }
// }
}