Skip to content

Commit

Permalink
Remove missing field warning and add test for scalar resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Mar 26, 2018
1 parent 0d65846 commit 463228c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
4 changes: 0 additions & 4 deletions codegen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@ func bindObject(t types.Type, object *Object, imports Imports) {
}
continue
}

if field.IsScalar {
fmt.Fprintf(os.Stderr, "unable to bind %s.%s to anything, %s has no suitable fields or methods\n", object.GQLType, field.GQLName, namedType.String())
}
}
}

Expand Down
45 changes: 44 additions & 1 deletion example/scalars/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func MakeExecutableSchema(resolvers Resolvers, opts ...ExecutableOption) graphql
type Resolvers interface {
Query_user(ctx context.Context, id string) (*User, error)
Query_search(ctx context.Context, input SearchArgs) ([]User, error)

User_primitiveResolver(ctx context.Context, obj *User) (string, error)
User_customResolver(ctx context.Context, obj *User) (Point, error)
}

type ExecutableOption func(*executableSchema)
Expand Down Expand Up @@ -227,6 +230,10 @@ func (ec *executionContext) _User(sel []query.Selection, obj *User) graphql.Mars
out.Values[i] = ec._User_location(field, obj)
case "isBanned":
out.Values[i] = ec._User_isBanned(field, obj)
case "primitiveResolver":
out.Values[i] = ec._User_primitiveResolver(field, obj)
case "customResolver":
out.Values[i] = ec._User_customResolver(field, obj)
default:
panic("unknown field " + strconv.Quote(field.Name))
}
Expand Down Expand Up @@ -260,6 +267,42 @@ func (ec *executionContext) _User_isBanned(field graphql.CollectedField, obj *Us
return graphql.MarshalBoolean(bool(res))
}

func (ec *executionContext) _User_primitiveResolver(field graphql.CollectedField, obj *User) graphql.Marshaler {
return graphql.Defer(func() (ret graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
userErr := ec.recover(r)
ec.Error(userErr)
ret = graphql.Null
}
}()
res, err := ec.resolvers.User_primitiveResolver(ec.ctx, obj)
if err != nil {
ec.Error(err)
return graphql.Null
}
return graphql.MarshalString(res)
})
}

func (ec *executionContext) _User_customResolver(field graphql.CollectedField, obj *User) graphql.Marshaler {
return graphql.Defer(func() (ret graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
userErr := ec.recover(r)
ec.Error(userErr)
ret = graphql.Null
}
}()
res, err := ec.resolvers.User_customResolver(ec.ctx, obj)
if err != nil {
ec.Error(err)
return graphql.Null
}
return res
})
}

var __DirectiveImplementors = []string{"__Directive"}

// nolint: gocyclo, errcheck, gas, goconst
Expand Down Expand Up @@ -794,7 +837,7 @@ func UnmarshalSearchArgs(v interface{}) (SearchArgs, error) {
return it, nil
}

var parsedSchema = schema.MustParse("type Query {\n user(id: ID!): User\n search(input: SearchArgs = {location: \"37,144\"}): [User!]!\n}\n\ntype User {\n id: ID!\n name: String!\n created: Timestamp\n location: Point\n isBanned: Boolean!\n}\n\ninput SearchArgs {\n location: Point\n createdAfter: Timestamp\n isBanned: Boolean\n}\n\nscalar Timestamp\nscalar Point\n")
var parsedSchema = schema.MustParse("type Query {\n user(id: ID!): User\n search(input: SearchArgs = {location: \"37,144\"}): [User!]!\n}\n\ntype User {\n id: ID!\n name: String!\n created: Timestamp\n location: Point\n isBanned: Boolean!\n primitiveResolver: String!\n customResolver: Point!\n}\n\ninput SearchArgs {\n location: Point\n createdAfter: Timestamp\n isBanned: Boolean\n}\n\nscalar Timestamp\nscalar Point\n")

func (ec *executionContext) introspectSchema() *introspection.Schema {
return introspection.WrapSchema(parsedSchema)
Expand Down
8 changes: 8 additions & 0 deletions example/scalars/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@ func (r *Resolver) Query_search(ctx context.Context, input SearchArgs) ([]User,
},
}, nil
}

func (r *Resolver) User_primitiveResolver(ctx context.Context, obj *User) (string, error) {
return "test", nil
}

func (r *Resolver) User_customResolver(ctx context.Context, obj *User) (Point, error) {
return Point{5, 1}, nil
}
20 changes: 15 additions & 5 deletions example/scalars/scalar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (
)

type RawUser struct {
ID string
Name string
Created int64
Location string
ID string
Name string
Created int64
Location string
PrimitiveResolver string
CustomResolver string
}

func TestScalars(t *testing.T) {
Expand Down Expand Up @@ -51,13 +53,21 @@ func TestScalars(t *testing.T) {
require.Equal(t, "37,144", resp.Search[0].Location)
})

t.Run("test custom error messages", func(t *testing.T) {
t.Run("custom error messages", func(t *testing.T) {
var resp struct{ Search []RawUser }

err := c.Post(`{ search(input:{createdAfter:"2014"}) { id } }`, &resp)
require.EqualError(t, err, "errors: [graphql: time should be a unix timestamp]")
})

t.Run("scalar resolver methods", func(t *testing.T) {
var resp struct{ User RawUser }
c.MustPost(`{ user(id: "1") { primitiveResolver, customResolver } }`, &resp)

require.Equal(t, "test", resp.User.PrimitiveResolver)
require.Equal(t, "5,1", resp.User.CustomResolver)
})

t.Run("introspection", func(t *testing.T) {
// Make sure we can run the graphiql introspection query without errors
var resp interface{}
Expand Down
2 changes: 2 additions & 0 deletions example/scalars/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type User {
created: Timestamp
location: Point
isBanned: Boolean!
primitiveResolver: String!
customResolver: Point!
}

input SearchArgs {
Expand Down

0 comments on commit 463228c

Please sign in to comment.