Skip to content

Commit a6abb31

Browse files
pavelnikolovKNiepok
authored andcommitted
Add executable examples (graph-gophers#567)
1 parent ee4db16 commit a6abb31

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

examples_test.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package graphql_test
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"os"
7+
8+
"github.com/graph-gophers/graphql-go"
9+
"github.com/graph-gophers/graphql-go/example/starwars"
10+
)
11+
12+
type Resolver struct {
13+
post *Post
14+
}
15+
16+
func (r *Resolver) Post() *Post {
17+
return r.post
18+
}
19+
20+
type Post struct {
21+
id graphql.ID
22+
title string
23+
}
24+
25+
func (p *Post) ID() graphql.ID {
26+
return p.id
27+
}
28+
29+
func (p *Post) Title() string {
30+
return p.title
31+
}
32+
33+
func ExampleID() {
34+
schemaString := `
35+
schema {
36+
query: Query
37+
}
38+
39+
type Query {
40+
post: Post!
41+
}
42+
43+
type Post {
44+
id: ID!
45+
title: String!
46+
}
47+
`
48+
49+
resolver := &Resolver{
50+
post: &Post{
51+
id: graphql.ID("5"),
52+
title: "title",
53+
},
54+
}
55+
56+
schema := graphql.MustParseSchema(schemaString, resolver)
57+
58+
query := `
59+
query {
60+
post {
61+
id
62+
title
63+
}
64+
}
65+
`
66+
67+
res := schema.Exec(context.Background(), query, "", nil)
68+
69+
enc := json.NewEncoder(os.Stdout)
70+
enc.SetIndent("", " ")
71+
err := enc.Encode(res)
72+
if err != nil {
73+
panic(err)
74+
}
75+
// output:
76+
// {
77+
// "data": {
78+
// "post": {
79+
// "id": "5",
80+
// "title": "title"
81+
// }
82+
// }
83+
// }
84+
}
85+
86+
func ExampleMaxDepth() {
87+
schema := graphql.MustParseSchema(starwars.Schema, &starwars.Resolver{}, graphql.MaxDepth(3))
88+
89+
// this query has a depth of 4
90+
query := `
91+
query {
92+
hero(episode:EMPIRE) { # level 1
93+
name # level 2
94+
friends {
95+
name # level 3
96+
friends {
97+
id # level 4 - this would exceed the max depth
98+
}
99+
}
100+
}
101+
}`
102+
103+
res := schema.Exec(context.Background(), query, "", nil)
104+
105+
enc := json.NewEncoder(os.Stdout)
106+
enc.SetIndent("", " ")
107+
err := enc.Encode(res)
108+
if err != nil {
109+
panic(err)
110+
}
111+
// output:
112+
// {
113+
// "errors": [
114+
// {
115+
// "message": "Field \"id\" has depth 4 that exceeds max depth 3",
116+
// "locations": [
117+
// {
118+
// "line": 8,
119+
// "column": 12
120+
// }
121+
// ]
122+
// }
123+
// ]
124+
// }
125+
}
126+
127+
func ExampleMaxQueryLength() {
128+
schema := graphql.MustParseSchema(starwars.Schema, &starwars.Resolver{}, graphql.MaxQueryLength(50))
129+
130+
// this query has a length of 53
131+
query := `{
132+
hero(episode:EMPIRE) {
133+
id
134+
name
135+
}
136+
}`
137+
138+
res := schema.Exec(context.Background(), query, "", nil)
139+
140+
enc := json.NewEncoder(os.Stdout)
141+
enc.SetIndent("", " ")
142+
err := enc.Encode(res)
143+
if err != nil {
144+
panic(err)
145+
}
146+
// output:
147+
// {
148+
// "errors": [
149+
// {
150+
// "message": "query length 53 exceeds the maximum allowed query length of 50 bytes"
151+
// }
152+
// ]
153+
// }
154+
}

0 commit comments

Comments
 (0)