forked from graph-gophers/graphql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_nullbool_test.go
59 lines (51 loc) · 1.18 KB
/
example_nullbool_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
55
56
57
58
59
package graphql_test
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/graph-gophers/graphql-go"
)
type mutnb struct{}
func (*mutnb) Toggle(args struct{ Enabled graphql.NullBool }) string {
if !args.Enabled.Set {
return "input value was not provided"
} else if args.Enabled.Value == nil {
return "enabled is 'null'"
}
return fmt.Sprintf("enabled '%v'", *args.Enabled.Value)
}
// ExampleNullBool demonstrates how to use nullable Bool type when it is necessary to differentiate between nil and not set.
func ExampleNullBool() {
const s = `
schema {
query: Query
mutation: Mutation
}
type Query{}
type Mutation{
toggle(enabled: Boolean): String!
}
`
schema := graphql.MustParseSchema(s, &mutnb{})
const query = `mutation{
toggle1: toggle()
toggle2: toggle(enabled: null)
toggle3: toggle(enabled: true)
}`
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": {
// "toggle1": "input value was not provided",
// "toggle2": "enabled is 'null'",
// "toggle3": "enabled 'true'"
// }
// }
}