Skip to content

Commit

Permalink
add test for object directive
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ichikawa committed Mar 25, 2020
1 parent 5c3812c commit ea21092
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 9 deletions.
6 changes: 4 additions & 2 deletions codegen/testserver/directive.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ directive @toNull on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | FIELD_DEFINI
directive @directive1 on FIELD_DEFINITION
directive @directive2 on FIELD_DEFINITION
directive @unimplemented on FIELD_DEFINITION
directive @order(location: String!) on FIELD_DEFINITION | OBJECT

extend type Query {
directiveArg(arg: String! @length(min:1, max: 255, message: "invalid length")): String
directiveNullableArg(arg: Int @range(min:0), arg2: Int @range, arg3: String @toNull): String
directiveInputNullable(arg: InputDirectives): String
directiveInput(arg: InputDirectives!): String
directiveInputType(arg: InnerInput! @custom): String
directiveObject: ObjectDirectives
directiveObject: ObjectDirectives @order(location: "Query_field")
directiveObjectWithCustomGoModel: ObjectDirectivesWithCustomGoModel
directiveFieldDef(ret: String!): String! @length(min: 1, message: "not valid")
directiveField: String
Expand All @@ -40,9 +41,10 @@ input InnerDirectives {
message: String! @length(min: 1, message: "not valid")
}

type ObjectDirectives {
type ObjectDirectives @order(location: "ObjectDirectives_object") {
text: String! @length(min: 0, max: 7, message: "not valid")
nullableText: String @toNull
order: [String!]!
}

type ObjectDirectivesWithCustomGoModel {
Expand Down
12 changes: 11 additions & 1 deletion codegen/testserver/directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ func TestDirectives(t *testing.T) {
Directive2: func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
return next(ctx)
},
Order: func(ctx context.Context, obj interface{}, next graphql.Resolver, location string) (res interface{}, err error) {
order := []string{location}
res, err = next(ctx)
od := res.(*ObjectDirectives)
od.Order = append(order, od.Order...)
return od, err
},
Unimplemented: nil,
},
}))
Expand Down Expand Up @@ -361,14 +368,17 @@ func TestDirectives(t *testing.T) {
DirectiveObject *struct {
Text string
NullableText *string
Order []string
}
}

err := c.Post(`query { directiveObject{ text nullableText } }`, &resp)
err := c.Post(`query { directiveObject{ text nullableText order} }`, &resp)

require.Nil(t, err)
require.Equal(t, "Ok", resp.DirectiveObject.Text)
require.True(t, resp.DirectiveObject.NullableText == nil)
require.Equal(t, "Query_field", resp.DirectiveObject.Order[0])
require.Equal(t, "ObjectDirectives_object", resp.DirectiveObject.Order[1])
})
t.Run("when directive returns nil & custom go field is not nilable", func(t *testing.T) {
var resp struct {
Expand Down
103 changes: 99 additions & 4 deletions codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions codegen/testserver/models-gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ea21092

Please sign in to comment.