@@ -11,6 +11,7 @@ import (
11
11
gqlerrors "github.com/graph-gophers/graphql-go/errors"
12
12
"github.com/graph-gophers/graphql-go/example/starwars"
13
13
"github.com/graph-gophers/graphql-go/gqltesting"
14
+ "github.com/graph-gophers/graphql-go/pkg/common"
14
15
)
15
16
16
17
type helloWorldResolver1 struct {}
@@ -45,6 +46,27 @@ func (r *helloSnakeResolver2) SayHello(ctx context.Context, args struct{ FullNam
45
46
return "Hello " + args .FullName + "!" , nil
46
47
}
47
48
49
+ type customDirectiveVisitor struct {
50
+ beforeWasCalled bool
51
+ }
52
+
53
+ func (v * customDirectiveVisitor ) Before (ctx context.Context , directive * common.Directive , input interface {}) error {
54
+ v .beforeWasCalled = true
55
+ return nil
56
+ }
57
+
58
+ func (v * customDirectiveVisitor ) After (ctx context.Context , directive * common.Directive , output interface {}) (interface {}, error ) {
59
+ if v .beforeWasCalled == false {
60
+ return nil , errors .New ("Before directive visitor method wasn't called." )
61
+ }
62
+
63
+ if value , ok := directive .Args .Get ("customAttribute" ); ok {
64
+ return fmt .Sprintf ("Directive '%s' (with arg '%s') modified result: %s" , directive .Name .Name , value .String (), output .(string )), nil
65
+ } else {
66
+ return fmt .Sprintf ("Directive '%s' modified result: %s" , directive .Name .Name , output .(string )), nil
67
+ }
68
+ }
69
+
48
70
type theNumberResolver struct {
49
71
number int32
50
72
}
@@ -188,7 +210,6 @@ func TestHelloWorld(t *testing.T) {
188
210
}
189
211
` ,
190
212
},
191
-
192
213
{
193
214
Schema : graphql .MustParseSchema (`
194
215
schema {
@@ -213,6 +234,67 @@ func TestHelloWorld(t *testing.T) {
213
234
})
214
235
}
215
236
237
+ func TestCustomDirective (t * testing.T ) {
238
+ t .Parallel ()
239
+
240
+ gqltesting .RunTests (t , []* gqltesting.Test {
241
+ {
242
+ Schema : graphql .MustParseSchema (`
243
+ directive @customDirective on FIELD_DEFINITION
244
+
245
+ schema {
246
+ query: Query
247
+ }
248
+
249
+ type Query {
250
+ hello_html: String! @customDirective
251
+ }
252
+ ` , & helloSnakeResolver1 {}),
253
+ Query : `
254
+ {
255
+ hello_html
256
+ }
257
+ ` ,
258
+ ExpectedResult : `
259
+ {
260
+ "hello_html": "Directive 'customDirective' modified result: Hello snake!"
261
+ }
262
+ ` ,
263
+ DirectiveVisitors : map [string ]common.DirectiveVisitor {
264
+ "customDirective" : & customDirectiveVisitor {},
265
+ },
266
+ },
267
+ {
268
+ Schema : graphql .MustParseSchema (`
269
+ directive @customDirective(
270
+ customAttribute: String!
271
+ ) on FIELD_DEFINITION
272
+
273
+ schema {
274
+ query: Query
275
+ }
276
+
277
+ type Query {
278
+ say_hello(full_name: String!): String! @customDirective(customAttribute: hi)
279
+ }
280
+ ` , & helloSnakeResolver1 {}),
281
+ Query : `
282
+ {
283
+ say_hello(full_name: "Johnny")
284
+ }
285
+ ` ,
286
+ ExpectedResult : `
287
+ {
288
+ "say_hello": "Directive 'customDirective' (with arg 'hi') modified result: Hello Johnny!"
289
+ }
290
+ ` ,
291
+ DirectiveVisitors : map [string ]common.DirectiveVisitor {
292
+ "customDirective" : & customDirectiveVisitor {},
293
+ },
294
+ },
295
+ })
296
+ }
297
+
216
298
func TestHelloSnake (t * testing.T ) {
217
299
t .Parallel ()
218
300
@@ -3728,7 +3810,7 @@ func TestSchema_Exec_without_resolver(t *testing.T) {
3728
3810
t .Fail ()
3729
3811
}
3730
3812
}()
3731
- _ = s .Exec (context .Background (), tt .Args .Query , "" , map [string ]interface {}{})
3813
+ _ = s .Exec (context .Background (), tt .Args .Query , "" , map [string ]interface {}{}, map [ string ]common. DirectiveVisitor {} )
3732
3814
})
3733
3815
}
3734
3816
}
0 commit comments