Skip to content

Commit fd885f0

Browse files
author
Sean Sorrell
committed
simplify tests, improve organization of test file
1 parent 5e217c2 commit fd885f0

File tree

1 file changed

+96
-68
lines changed

1 file changed

+96
-68
lines changed

graphql_test.go

Lines changed: 96 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ func (r *helloSnakeResolver2) SayHello(ctx context.Context, args struct{ FullNam
4949
return "Hello " + args.FullName + "!", nil
5050
}
5151

52+
type structFieldResolver struct {
53+
Hello string
54+
}
55+
5256
type customDirectiveVisitor struct {
5357
beforeWasCalled bool
5458
}
@@ -65,9 +69,8 @@ func (v *customDirectiveVisitor) After(ctx context.Context, directive *types.Dir
6569

6670
if value, ok := directive.Arguments.Get("customAttribute"); ok {
6771
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
7072
}
73+
return fmt.Sprintf("Directive '%s' modified result: %s", directive.Name.Name, output.(string)), nil
7174
}
7275

7376
type theNumberResolver struct {
@@ -237,6 +240,34 @@ func TestHelloWorld(t *testing.T) {
237240
})
238241
}
239242

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+
240271
func TestCustomDirective(t *testing.T) {
241272
t.Parallel()
242273

@@ -295,6 +326,69 @@ func TestCustomDirective(t *testing.T) {
295326
}
296327
`,
297328
},
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+
},
298392
})
299393
}
300394

@@ -4632,69 +4726,3 @@ func TestQueryService(t *testing.T) {
46324726
},
46334727
})
46344728
}
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

Comments
 (0)