Skip to content

Commit

Permalink
Merge pull request #604 from cevou/arg-scalar
Browse files Browse the repository at this point in the history
Fix directives on args with custom type
  • Loading branch information
vektah authored Mar 11, 2019
2 parents d7b5dc2 + d02736d commit 22be59d
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion codegen/args.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (ec *executionContext) {{ $name }}(ctx context.Context, rawArgs map[string]
if err != nil {
return nil, err
}
if data, ok := tmp.({{ $arg.TypeReference.GO }}) ; ok {
if data, ok := tmp.({{ $arg.TypeReference.GO | ref }}) ; ok {
arg{{$i}} = data
} else {
return nil, fmt.Errorf(`unexpected type %T from directive, should be {{ $arg.TypeReference.GO }}`, tmp)
Expand Down
18 changes: 18 additions & 0 deletions codegen/testserver/directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func TestDirectives(t *testing.T) {
return &s, nil
}

resolvers.QueryResolver.DirectiveInputType = func(ctx context.Context, arg InnerInput) (i *string, e error) {
s := "Ok"
return &s, nil
}

srv := httptest.NewServer(
handler.GraphQL(
NewExecutableSchema(Config{
Expand Down Expand Up @@ -90,6 +95,9 @@ func TestDirectives(t *testing.T) {
}
return nil, fmt.Errorf("unsupported type %T", res)
},
Custom: func(ctx context.Context, obj interface{}, next graphql.Resolver) (interface{}, error) {
return next(ctx)
},
},
}),
handler.ResolverMiddleware(func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
Expand Down Expand Up @@ -206,5 +214,15 @@ func TestDirectives(t *testing.T) {
require.Nil(t, err)
require.Equal(t, "Ok", *resp.DirectiveInputNullable)
})
t.Run("when arg has directive", func(t *testing.T) {
var resp struct {
DirectiveInputType *string
}

err := c.Post(`query { directiveInputType(arg: {id: 1}) }`, &resp)

require.Nil(t, err)
require.Equal(t, "Ok", *resp.DirectiveInputType)
})
})
}
93 changes: 93 additions & 0 deletions codegen/testserver/generated.go

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

3 changes: 3 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func (r *queryResolver) DirectiveInputNullable(ctx context.Context, arg *InputDi
func (r *queryResolver) DirectiveInput(ctx context.Context, arg InputDirectives) (*string, error) {
panic("not implemented")
}
func (r *queryResolver) DirectiveInputType(ctx context.Context, arg InnerInput) (*string, error) {
panic("not implemented")
}
func (r *queryResolver) InputSlice(ctx context.Context, arg []string) (bool, error) {
panic("not implemented")
}
Expand Down
2 changes: 2 additions & 0 deletions codegen/testserver/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Query {
directiveNullableArg(arg: Int @range(min:0), arg2: Int @range): String
directiveInputNullable(arg: InputDirectives): String
directiveInput(arg: InputDirectives!): String
directiveInputType(arg: InnerInput! @custom): String
inputSlice(arg: [String!]!): Boolean!
shapeUnion: ShapeUnion!
autobind: Autobind
Expand Down Expand Up @@ -126,6 +127,7 @@ type EmbeddedPointer {

directive @length(min: Int!, max: Int) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
directive @range(min: Int = 0, max: Int) on ARGUMENT_DEFINITION
directive @custom on ARGUMENT_DEFINITION

enum Status {
OK
Expand Down
4 changes: 4 additions & 0 deletions codegen/testserver/stub.go

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

0 comments on commit 22be59d

Please sign in to comment.