Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for OBJECT directive #1117

Merged
merged 2 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ func (b *builder) bindField(obj *Object, f *Field) (errret error) {
}
f.TypeReference = tr
}
if f.TypeReference != nil {
dirs, err := b.getDirectives(f.TypeReference.Definition.Directives)
if err != nil {
errret = err
}
f.Directives = append(dirs, f.Directives...)
}
}()

f.Stream = obj.Stream
Expand Down
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.