Skip to content

Commit 3d6f14a

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

File tree

1 file changed

+97
-68
lines changed

1 file changed

+97
-68
lines changed

graphql_test.go

Lines changed: 97 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
}
@@ -64,10 +68,10 @@ func (v *customDirectiveVisitor) After(ctx context.Context, directive *types.Dir
6468
}
6569

6670
if value, ok := directive.Arguments.Get("customAttribute"); ok {
71+
fmt.Printf("before was called: %s\n", value)
6772
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
7073
}
74+
return fmt.Sprintf("Directive '%s' modified result: %s", directive.Name.Name, output.(string)), nil
7175
}
7276

7377
type theNumberResolver struct {
@@ -237,6 +241,34 @@ func TestHelloWorld(t *testing.T) {
237241
})
238242
}
239243

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+
240272
func TestCustomDirective(t *testing.T) {
241273
t.Parallel()
242274

@@ -295,6 +327,69 @@ func TestCustomDirective(t *testing.T) {
295327
}
296328
`,
297329
},
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+
},
298393
})
299394
}
300395

@@ -4632,69 +4727,3 @@ func TestQueryService(t *testing.T) {
46324727
},
46334728
})
46344729
}
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)