@@ -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
}
@@ -65,9 +69,8 @@ func (v *customDirectiveVisitor) After(ctx context.Context, directive *types.Dir
65
69
66
70
if value , ok := directive .Arguments .Get ("customAttribute" ); ok {
67
71
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
72
}
73
+ return fmt .Sprintf ("Directive '%s' modified result: %s" , directive .Name .Name , output .(string )), nil
71
74
}
72
75
73
76
type theNumberResolver struct {
@@ -237,6 +240,34 @@ func TestHelloWorld(t *testing.T) {
237
240
})
238
241
}
239
242
243
+ func TestHelloWorldStructFieldResolver (t * testing.T ) {
244
+ gqltesting .RunTests (t , []* gqltesting.Test {
245
+ {
246
+ Schema : graphql .MustParseSchema (`
247
+ schema {
248
+ query: Query
249
+ }
250
+
251
+ type Query {
252
+ hello: String!
253
+ }
254
+ ` ,
255
+ & structFieldResolver {Hello : "Hello world!" },
256
+ graphql .UseFieldResolvers ()),
257
+ Query : `
258
+ {
259
+ hello
260
+ }
261
+ ` ,
262
+ ExpectedResult : `
263
+ {
264
+ "hello": "Hello world!"
265
+ }
266
+ ` ,
267
+ },
268
+ })
269
+ }
270
+
240
271
func TestCustomDirective (t * testing.T ) {
241
272
t .Parallel ()
242
273
@@ -295,6 +326,69 @@ func TestCustomDirective(t *testing.T) {
295
326
}
296
327
` ,
297
328
},
329
+
330
+ // tests for struct field resolvers
331
+
332
+ })
333
+ }
334
+
335
+ func TestCustomDirectiveStructFieldResolver (t * testing.T ) {
336
+ schemaOpt := []graphql.SchemaOpt {
337
+ graphql .DirectiveVisitors (map [string ]types.DirectiveVisitor {
338
+ "customDirective" : & customDirectiveVisitor {},
339
+ }),
340
+ graphql .UseFieldResolvers (),
341
+ }
342
+
343
+ gqltesting .RunTests (t , []* gqltesting.Test {
344
+ {
345
+ Schema : graphql .MustParseSchema (`
346
+ directive @customDirective on FIELD_DEFINITION
347
+
348
+ schema {
349
+ query: Query
350
+ }
351
+
352
+ type Query {
353
+ hello: String! @customDirective
354
+ }
355
+ ` , & structFieldResolver {Hello : "Hello world!" }, schemaOpt ... ),
356
+ Query : `
357
+ {
358
+ hello
359
+ }
360
+ ` ,
361
+ ExpectedResult : `
362
+ {
363
+ "hello": "Directive 'customDirective' modified result: Hello world!"
364
+ }
365
+ ` ,
366
+ },
367
+ {
368
+ Schema : graphql .MustParseSchema (`
369
+ directive @customDirective(
370
+ customAttribute: String!
371
+ ) on FIELD_DEFINITION
372
+
373
+ schema {
374
+ query: Query
375
+ }
376
+
377
+ type Query {
378
+ hello: String! @customDirective(customAttribute: hi)
379
+ }
380
+ ` , & structFieldResolver {Hello : "Hello world!" }, schemaOpt ... ),
381
+ Query : `
382
+ {
383
+ hello
384
+ }
385
+ ` ,
386
+ ExpectedResult : `
387
+ {
388
+ "hello": "Directive 'customDirective' (with arg 'hi') modified result: Hello world!"
389
+ }
390
+ ` ,
391
+ },
298
392
})
299
393
}
300
394
@@ -4632,69 +4726,3 @@ func TestQueryService(t *testing.T) {
4632
4726
},
4633
4727
})
4634
4728
}
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