-
Notifications
You must be signed in to change notification settings - Fork 12
/
directives_test.go
97 lines (85 loc) · 1.78 KB
/
directives_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package tools
import (
"testing"
"github.com/graphql-go/graphql"
)
func TestDirectives(t *testing.T) {
typeDefs := `
directive @test(message: String) on FIELD_DEFINITION
type Foo {
name: String!
description: String
}
type Query {
foos(
name: String
): [Foo] @test(message: "foobar")
}
`
// create some data
foos := []map[string]interface{}{
{
"name": "foo",
"description": "a foo",
},
}
// make the schema
schema, err := MakeExecutableSchema(ExecutableSchema{
TypeDefs: typeDefs,
Resolvers: ResolverMap{
"Query": &ObjectResolver{
Fields: FieldResolveMap{
"foos": &FieldResolve{
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return foos, nil
},
},
},
},
},
SchemaDirectives: SchemaDirectiveVisitorMap{
"test": &SchemaDirectiveVisitor{
VisitFieldDefinition: func(v VisitFieldDefinitionParams) error {
resolveFunc := v.Config.Resolve
v.Config.Resolve = func(p graphql.ResolveParams) (interface{}, error) {
result, err := resolveFunc(p)
if err != nil {
return result, err
}
res := result.([]map[string]interface{})
res0 := res[0]
res0["description"] = v.Args["message"]
return res, nil
}
return nil
},
},
},
})
if err != nil {
t.Error(err)
return
}
// perform a query
r := graphql.Do(graphql.Params{
Schema: schema,
RequestString: `query Query {
foos(name:"foo") {
name
description
}
}`,
})
if r.HasErrors() {
t.Error(r.Errors)
return
}
d := r.Data.(map[string]interface{})
fooResult := d["foos"]
foos0 := fooResult.([]interface{})[0]
foos0Desc := foos0.(map[string]interface{})["description"]
if foos0Desc.(string) != "foobar" {
t.Error("failed to set field with directive")
return
}
}