Skip to content

Commit

Permalink
Merge pull request #764 from 99designs/fix-field-directives-on-roots
Browse files Browse the repository at this point in the history
fix field schema directives applied to roots
  • Loading branch information
vektah authored Jun 27, 2019
2 parents 17ee40b + ef3830b commit 5ffc297
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 173 deletions.
3 changes: 3 additions & 0 deletions codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ func (f *Field) HasDirectives() bool {
}

func (f *Field) DirectiveObjName() string {
if f.Object.Root {
return "nil"
}
return f.GoReceiverName
}

Expand Down
25 changes: 25 additions & 0 deletions codegen/testserver/directive.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
directive @length(min: Int!, max: Int, message: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | FIELD_DEFINITION
directive @range(min: Int = 0, max: Int) on ARGUMENT_DEFINITION
directive @custom on ARGUMENT_DEFINITION
directive @logged(id: UUID!) on FIELD

extend type Query {
directiveArg(arg: String! @length(min:1, max: 255, message: "invalid length")): String
directiveNullableArg(arg: Int @range(min:0), arg2: Int @range): String
directiveInputNullable(arg: InputDirectives): String
directiveInput(arg: InputDirectives!): String
directiveInputType(arg: InnerInput! @custom): String
directiveFieldDef(ret: String!): String! @length(min: 1, message: "not valid")
directiveField: String
}

input InputDirectives {
text: String! @length(min: 0, max: 7, message: "not valid")
inner: InnerDirectives!
innerNullable: InnerDirectives
thirdParty: ThirdParty @length(min: 0, max: 7)
}

input InnerDirectives {
message: String! @length(min: 1, message: "not valid")
}
25 changes: 25 additions & 0 deletions codegen/testserver/directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,31 @@ func TestDirectives(t *testing.T) {
require.Equal(t, "Ok", *resp.DirectiveArg)
})
})
t.Run("field definition directives", func(t *testing.T) {
resolvers.QueryResolver.DirectiveFieldDef = func(ctx context.Context, ret string) (i string, e error) {
return ret, nil
}

t.Run("too short", func(t *testing.T) {
var resp struct {
DirectiveFieldDef string
}

err := c.Post(`query { directiveFieldDef(ret: "") }`, &resp)

require.EqualError(t, err, `[{"message":"not valid","path":["directiveFieldDef"]}]`)
})

t.Run("ok", func(t *testing.T) {
var resp struct {
DirectiveFieldDef string
}

c.MustPost(`query { directiveFieldDef(ret: "aaa") }`, &resp)

require.Equal(t, "aaa", resp.DirectiveFieldDef)
})
})
t.Run("field directives", func(t *testing.T) {
t.Run("add field directive", func(t *testing.T) {
var resp struct {
Expand Down
Loading

0 comments on commit 5ffc297

Please sign in to comment.