@@ -49,6 +49,10 @@ func (r *helloSnakeResolver2) SayHello(ctx context.Context, args struct{ FullNam
49
49
return "Hello " + args .FullName + "!" , nil
50
50
}
51
51
52
+ type structFieldResolver struct {
53
+ Hello string
54
+ }
55
+
52
56
type customDirectiveVisitor struct {
53
57
beforeWasCalled bool
54
58
}
@@ -64,10 +68,10 @@ func (v *customDirectiveVisitor) After(ctx context.Context, directive *types.Dir
64
68
}
65
69
66
70
if value , ok := directive .Arguments .Get ("customAttribute" ); ok {
71
+ fmt .Printf ("before was called: %s\n " , value )
67
72
return fmt .Sprintf ("Directive '%s' (with arg '%s') modified result: %s" , directive .Name .Name , value .String (), output .(string )), nil
68
- } else {
69
- return fmt .Sprintf ("Directive '%s' modified result: %s" , directive .Name .Name , output .(string )), nil
70
73
}
74
+ return fmt .Sprintf ("Directive '%s' modified result: %s" , directive .Name .Name , output .(string )), nil
71
75
}
72
76
73
77
type theNumberResolver struct {
@@ -237,6 +241,34 @@ func TestHelloWorld(t *testing.T) {
237
241
})
238
242
}
239
243
244
+ func TestHelloWorldStructFieldResolver (t * testing.T ) {
245
+ gqltesting .RunTests (t , []* gqltesting.Test {
246
+ {
247
+ Schema : graphql .MustParseSchema (`
248
+ schema {
249
+ query: Query
250
+ }
251
+
252
+ type Query {
253
+ hello: String!
254
+ }
255
+ ` ,
256
+ & structFieldResolver {Hello : "Hello world!" },
257
+ graphql .UseFieldResolvers ()),
258
+ Query : `
259
+ {
260
+ hello
261
+ }
262
+ ` ,
263
+ ExpectedResult : `
264
+ {
265
+ "hello": "Hello world!"
266
+ }
267
+ ` ,
268
+ },
269
+ })
270
+ }
271
+
240
272
func TestCustomDirective (t * testing.T ) {
241
273
t .Parallel ()
242
274
@@ -295,6 +327,69 @@ func TestCustomDirective(t *testing.T) {
295
327
}
296
328
` ,
297
329
},
330
+
331
+ // tests for struct field resolvers
332
+
333
+ })
334
+ }
335
+
336
+ func TestCustomDirectiveStructFieldResolver (t * testing.T ) {
337
+ schemaOpt := []graphql.SchemaOpt {
338
+ graphql .DirectiveVisitors (map [string ]types.DirectiveVisitor {
339
+ "customDirective" : & customDirectiveVisitor {},
340
+ }),
341
+ graphql .UseFieldResolvers (),
342
+ }
343
+
344
+ gqltesting .RunTests (t , []* gqltesting.Test {
345
+ {
346
+ Schema : graphql .MustParseSchema (`
347
+ directive @customDirective on FIELD_DEFINITION
348
+
349
+ schema {
350
+ query: Query
351
+ }
352
+
353
+ type Query {
354
+ hello: String! @customDirective
355
+ }
356
+ ` , & structFieldResolver {Hello : "Hello world!" }, schemaOpt ... ),
357
+ Query : `
358
+ {
359
+ hello
360
+ }
361
+ ` ,
362
+ ExpectedResult : `
363
+ {
364
+ "hello": "Directive 'customDirective' modified result: Hello world!"
365
+ }
366
+ ` ,
367
+ },
368
+ {
369
+ Schema : graphql .MustParseSchema (`
370
+ directive @customDirective(
371
+ customAttribute: String!
372
+ ) on FIELD_DEFINITION
373
+
374
+ schema {
375
+ query: Query
376
+ }
377
+
378
+ type Query {
379
+ hello: String! @customDirective(customAttribute: hi)
380
+ }
381
+ ` , & structFieldResolver {Hello : "Hello world!" }, schemaOpt ... ),
382
+ Query : `
383
+ {
384
+ hello
385
+ }
386
+ ` ,
387
+ ExpectedResult : `
388
+ {
389
+ "hello": "Directive 'customDirective' (with arg 'hi') modified result: Hello world!"
390
+ }
391
+ ` ,
392
+ },
298
393
})
299
394
}
300
395
@@ -4632,69 +4727,3 @@ func TestQueryService(t *testing.T) {
4632
4727
},
4633
4728
})
4634
4729
}
4635
-
4636
- type StructFieldResolver struct {
4637
- Hello string
4638
- }
4639
-
4640
- func TestStructFieldResolver (t * testing.T ) {
4641
- gqltesting .RunTests (t , []* gqltesting.Test {
4642
- {
4643
- Schema : graphql .MustParseSchema (`
4644
- schema {
4645
- query: Query
4646
- }
4647
-
4648
- type Query {
4649
- hello: String!
4650
- }
4651
- ` , & StructFieldResolver {Hello : "Hello world!" }, graphql .UseFieldResolvers ()),
4652
- Query : `
4653
- {
4654
- hello
4655
- }
4656
- ` ,
4657
- ExpectedResult : `
4658
- {
4659
- "hello": "Hello world!"
4660
- }
4661
- ` ,
4662
- },
4663
- })
4664
- }
4665
-
4666
- func TestDirectiveStructFieldResolver (t * testing.T ) {
4667
- schemaOpt := []graphql.SchemaOpt {
4668
- graphql .DirectiveVisitors (map [string ]types.DirectiveVisitor {
4669
- "customDirective" : & customDirectiveVisitor {},
4670
- }),
4671
- graphql .UseFieldResolvers (),
4672
- }
4673
-
4674
- gqltesting .RunTests (t , []* gqltesting.Test {
4675
-
4676
- {
4677
- Schema : graphql .MustParseSchema (`
4678
- directive @customDirective on FIELD_DEFINITION
4679
-
4680
- schema {
4681
- query: Query
4682
- }
4683
-
4684
- type Query {
4685
- hello: String! @customDirective
4686
- }
4687
- ` , & StructFieldResolver {Hello : "Hello world!" }, schemaOpt ... ),
4688
- Query : `
4689
- {
4690
- hello
4691
- }
4692
- ` ,
4693
- ExpectedResult : `
4694
- {
4695
- "hello": "Directive 'customDirective' modified result: Hello world!"
4696
- }
4697
- ` ,
4698
- }})
4699
-
4700
- }
0 commit comments