From 630a3cfc60cb0dff9d32431b11c966bdcc596f58 Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Mon, 26 Mar 2018 22:40:24 +1100 Subject: [PATCH 1/2] failing test --- example/scalars/generated.go | 88 ++++++++++++++++++++---- example/scalars/model.go | 21 +++++- example/scalars/models_gen.go | 13 ++++ example/scalars/resolvers.go | 11 +-- example/scalars/schema.graphql | 8 ++- example/scalars/types.json | 3 +- example/scalars/vendor/external/model.go | 3 + example/scalars/vendor/readme.md | 1 + 8 files changed, 129 insertions(+), 19 deletions(-) create mode 100644 example/scalars/models_gen.go create mode 100644 example/scalars/vendor/external/model.go create mode 100644 example/scalars/vendor/readme.md diff --git a/example/scalars/generated.go b/example/scalars/generated.go index ea002bfe4d..0ee95783ab 100644 --- a/example/scalars/generated.go +++ b/example/scalars/generated.go @@ -24,11 +24,12 @@ func MakeExecutableSchema(resolvers Resolvers, opts ...ExecutableOption) graphql } type Resolvers interface { - Query_user(ctx context.Context, id string) (*User, error) + Query_user(ctx context.Context, id ObjectID) (*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) + User_address(ctx context.Context, obj *User) (*Address, error) } type ExecutableOption func(*executableSchema) @@ -85,6 +86,53 @@ type executionContext struct { recover graphql.RecoverFunc } +var addressImplementors = []string{"Address"} + +// nolint: gocyclo, errcheck, gas, goconst +func (ec *executionContext) _Address(sel []query.Selection, obj *Address) graphql.Marshaler { + fields := graphql.CollectFields(ec.doc, sel, addressImplementors, ec.variables) + out := graphql.NewOrderedMap(len(fields)) + for i, field := range fields { + out.Keys[i] = field.Alias + + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString("Address") + case "id": + out.Values[i] = ec._Address_id(field, obj) + case "street": + out.Values[i] = ec._Address_street(field, obj) + case "location": + out.Values[i] = ec._Address_location(field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + + return out +} + +func (ec *executionContext) _Address_id(field graphql.CollectedField, obj *Address) graphql.Marshaler { + res := obj.ID + return MarshalID(res) +} + +func (ec *executionContext) _Address_street(field graphql.CollectedField, obj *Address) graphql.Marshaler { + res := obj.Street + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) +} + +func (ec *executionContext) _Address_location(field graphql.CollectedField, obj *Address) graphql.Marshaler { + res := obj.Location + if res == nil { + return graphql.Null + } + return *res +} + var queryImplementors = []string{"Query"} // nolint: gocyclo, errcheck, gas, goconst @@ -114,10 +162,10 @@ func (ec *executionContext) _Query(sel []query.Selection) graphql.Marshaler { } func (ec *executionContext) _Query_user(field graphql.CollectedField) graphql.Marshaler { - var arg0 string + var arg0 ObjectID if tmp, ok := field.Args["id"]; ok { var err error - arg0, err = graphql.UnmarshalID(tmp) + arg0, err = UnmarshalID(tmp) if err != nil { ec.Error(err) return graphql.Null @@ -226,14 +274,14 @@ func (ec *executionContext) _User(sel []query.Selection, obj *User) graphql.Mars out.Values[i] = ec._User_name(field, obj) case "created": out.Values[i] = ec._User_created(field, obj) - case "location": - 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) + case "address": + out.Values[i] = ec._User_address(field, obj) default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -244,7 +292,7 @@ func (ec *executionContext) _User(sel []query.Selection, obj *User) graphql.Mars func (ec *executionContext) _User_id(field graphql.CollectedField, obj *User) graphql.Marshaler { res := obj.ID - return graphql.MarshalID(res) + return MarshalID(res) } func (ec *executionContext) _User_name(field graphql.CollectedField, obj *User) graphql.Marshaler { @@ -257,11 +305,6 @@ func (ec *executionContext) _User_created(field graphql.CollectedField, obj *Use return MarshalTimestamp(res) } -func (ec *executionContext) _User_location(field graphql.CollectedField, obj *User) graphql.Marshaler { - res := obj.Location - return res -} - func (ec *executionContext) _User_isBanned(field graphql.CollectedField, obj *User) graphql.Marshaler { res := obj.IsBanned return graphql.MarshalBoolean(bool(res)) @@ -303,6 +346,27 @@ func (ec *executionContext) _User_customResolver(field graphql.CollectedField, o }) } +func (ec *executionContext) _User_address(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_address(ec.ctx, obj) + if err != nil { + ec.Error(err) + return graphql.Null + } + if res == nil { + return graphql.Null + } + return ec._Address(field.Selections, res) + }) +} + var __DirectiveImplementors = []string{"__Directive"} // nolint: gocyclo, errcheck, gas, goconst @@ -837,7 +901,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 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") +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 isBanned: Boolean!\n primitiveResolver: String!\n customResolver: Point!\n address: Address\n}\n\ntype Address {\n id: ID!\n street: String\n location: 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) diff --git a/example/scalars/model.go b/example/scalars/model.go index ed82c96a51..ceaa34f7a9 100644 --- a/example/scalars/model.go +++ b/example/scalars/model.go @@ -8,13 +8,15 @@ import ( "strings" "time" + "external" + "github.com/vektah/gqlgen/graphql" ) type Banned bool type User struct { - ID string + ID external.ObjectID Name string Location Point // custom scalar types Created time.Time // direct binding to builtin types with external Marshal/Unmarshal methods @@ -71,6 +73,23 @@ func UnmarshalTimestamp(v interface{}) (time.Time, error) { return time.Time{}, errors.New("time should be a unix timestamp") } +// Lets redefine the base ID type to use an id from an external library +func MarshalID(id external.ObjectID) graphql.Marshaler { + return graphql.WriterFunc(func(w io.Writer) { + io.WriteString(w, fmt.Sprintf("%d", id)) + }) +} + +// And the same for the unmarshaler +func UnmarshalID(v interface{}) (external.ObjectID, error) { + str, ok := v.(string) + if !ok { + return 0, fmt.Errorf("ids must be strings") + } + i, err := strconv.Atoi(str) + return external.ObjectID(i), err +} + type SearchArgs struct { Location *Point CreatedAfter *time.Time diff --git a/example/scalars/models_gen.go b/example/scalars/models_gen.go new file mode 100644 index 0000000000..01991436bd --- /dev/null +++ b/example/scalars/models_gen.go @@ -0,0 +1,13 @@ +// This file was generated by github.com/vektah/gqlgen, DO NOT EDIT + +package scalars + +import ( + external "github.com/vektah/gqlgen/example/scalars/vendor/external" +) + +type Address struct { + ID external.ObjectID + Street *string + Location *Point +} diff --git a/example/scalars/resolvers.go b/example/scalars/resolvers.go index 5afe061238..615a38b25e 100644 --- a/example/scalars/resolvers.go +++ b/example/scalars/resolvers.go @@ -4,16 +4,19 @@ package scalars import ( context "context" + "fmt" "time" + + "external" ) type Resolver struct { } -func (r *Resolver) Query_user(ctx context.Context, id string) (*User, error) { +func (r *Resolver) Query_user(ctx context.Context, id external.ObjectID) (*User, error) { return &User{ ID: id, - Name: "Test User " + id, + Name: fmt.Sprintf("Test User %d", id), Created: time.Now(), Location: Point{1, 2}, }, nil @@ -32,13 +35,13 @@ func (r *Resolver) Query_search(ctx context.Context, input SearchArgs) ([]User, return []User{ { - ID: "1", + ID: 1, Name: "Test User 1", Created: created, Location: location, }, { - ID: "2", + ID: 2, Name: "Test User 2", Created: created, Location: location, diff --git a/example/scalars/schema.graphql b/example/scalars/schema.graphql index 4162b9f9ab..3f89904d39 100644 --- a/example/scalars/schema.graphql +++ b/example/scalars/schema.graphql @@ -7,10 +7,16 @@ type User { id: ID! name: String! created: Timestamp - location: Point isBanned: Boolean! primitiveResolver: String! customResolver: Point! + address: Address +} + +type Address { + id: ID! + street: String + location: Point } input SearchArgs { diff --git a/example/scalars/types.json b/example/scalars/types.json index 60f347c605..8cb0542833 100644 --- a/example/scalars/types.json +++ b/example/scalars/types.json @@ -2,5 +2,6 @@ "User": "github.com/vektah/gqlgen/example/scalars.User", "Timestamp": "github.com/vektah/gqlgen/example/scalars.Timestamp", "SearchArgs": "github.com/vektah/gqlgen/example/scalars.SearchArgs", - "Point": "github.com/vektah/gqlgen/example/scalars.Point" + "Point": "github.com/vektah/gqlgen/example/scalars.Point", + "ID": "github.com/vektah/gqlgen/example/scalars.ID" } diff --git a/example/scalars/vendor/external/model.go b/example/scalars/vendor/external/model.go new file mode 100644 index 0000000000..88875d907b --- /dev/null +++ b/example/scalars/vendor/external/model.go @@ -0,0 +1,3 @@ +package external + +type ObjectID int diff --git a/example/scalars/vendor/readme.md b/example/scalars/vendor/readme.md new file mode 100644 index 0000000000..a7ae70d827 --- /dev/null +++ b/example/scalars/vendor/readme.md @@ -0,0 +1 @@ +fake vendor directory for scalar tests. From 2ff9f32f6e6195f9ee8b760e872fc2f8b052de07 Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Mon, 26 Mar 2018 23:18:05 +1100 Subject: [PATCH 2/2] Fix vendored import paths --- codegen/build.go | 4 +-- codegen/import_build.go | 65 ++++++++++++++++++---------------- codegen/type_build.go | 12 +++++-- codegen/util.go | 4 +-- example/scalars/generated.go | 38 ++++---------------- example/scalars/model.go | 6 ++-- example/scalars/models_gen.go | 3 +- example/scalars/resolvers.go | 24 ++++++------- example/scalars/scalar_test.go | 16 ++++----- example/scalars/schema.graphql | 1 - 10 files changed, 77 insertions(+), 96 deletions(-) diff --git a/codegen/build.go b/codegen/build.go index d24149a954..6cbf077d39 100644 --- a/codegen/build.go +++ b/codegen/build.go @@ -39,7 +39,7 @@ func Models(schema *schema.Schema, userTypes map[string]string, destDir string) panic(err) } - bindTypes(imports, namedTypes, prog) + bindTypes(imports, namedTypes, destDir, prog) models := buildModels(namedTypes, schema, prog) return &ModelBuild{ @@ -59,7 +59,7 @@ func Bind(schema *schema.Schema, userTypes map[string]string, destDir string) (* return nil, err } - bindTypes(imports, namedTypes, prog) + imports = bindTypes(imports, namedTypes, destDir, prog) objects := buildObjects(namedTypes, schema, prog, imports) inputs := buildInputs(namedTypes, schema, prog, imports) diff --git a/codegen/import_build.go b/codegen/import_build.go index e3f27e3997..cafbb25396 100644 --- a/codegen/import_build.go +++ b/codegen/import_build.go @@ -23,43 +23,46 @@ func buildImports(types NamedTypes, destDir string) Imports { } for _, t := range types { - if t.Package == "" { - continue - } + imports, t.Import = imports.addPkg(types, destDir, t.Package) + } - if existing := imports.findByPkg(t.Package); existing != nil { - t.Import = existing - continue - } + return imports +} - localName := "" - if !strings.HasSuffix(destDir, t.Package) { - localName = filepath.Base(t.Package) - i := 1 - imp := imports.findByName(localName) - for imp != nil && imp.Package != t.Package { - localName = filepath.Base(t.Package) + strconv.Itoa(i) - imp = imports.findByName(localName) - i++ - if i > 10 { - panic("too many collisions") - } - } - } +func (s Imports) addPkg(types NamedTypes, destDir string, pkg string) (Imports, *Import) { + if pkg == "" { + return s, nil + } + + if existing := s.findByPkg(pkg); existing != nil { + return s, existing + } - imp := &Import{ - Name: localName, - Package: t.Package, + localName := "" + if !strings.HasSuffix(destDir, pkg) { + localName = filepath.Base(pkg) + i := 1 + imp := s.findByName(localName) + for imp != nil && imp.Package != pkg { + localName = filepath.Base(pkg) + strconv.Itoa(i) + imp = s.findByName(localName) + i++ + if i > 10 { + panic("too many collisions") + } } - t.Import = imp - imports = append(imports, imp) } - return imports + imp := &Import{ + Name: localName, + Package: pkg, + } + s = append(s, imp) + return s, imp } -func (i Imports) findByPkg(pkg string) *Import { - for _, imp := range i { +func (s Imports) findByPkg(pkg string) *Import { + for _, imp := range s { if imp.Package == pkg { return imp } @@ -67,8 +70,8 @@ func (i Imports) findByPkg(pkg string) *Import { return nil } -func (i Imports) findByName(name string) *Import { - for _, imp := range i { +func (s Imports) findByName(name string) *Import { + for _, imp := range s { if imp.Name == name { return imp } diff --git a/codegen/type_build.go b/codegen/type_build.go index 387edecea2..8e5a14aa7d 100644 --- a/codegen/type_build.go +++ b/codegen/type_build.go @@ -30,7 +30,7 @@ func buildNamedTypes(s *schema.Schema, userTypes map[string]string) NamedTypes { return types } -func bindTypes(imports Imports, namedTypes NamedTypes, prog *loader.Program) { +func bindTypes(imports Imports, namedTypes NamedTypes, destDir string, prog *loader.Program) Imports { for _, t := range namedTypes { if t.Package == "" { continue @@ -44,9 +44,10 @@ func bindTypes(imports Imports, namedTypes NamedTypes, prog *loader.Program) { t.Marshaler = &cpy t.Package, t.GoType = pkgAndType(sig.Params().At(0).Type().String()) - t.Import = imports.findByName(t.Package) + imports, t.Import = imports.addPkg(namedTypes, destDir, t.Package) } } + return imports } // namedTypeFromSchema objects for every graphql type, including primitives. @@ -71,7 +72,12 @@ func pkgAndType(name string) (string, string) { return "", name } - return strings.Join(parts[:len(parts)-1], "."), parts[len(parts)-1] + return normalizeVendor(strings.Join(parts[:len(parts)-1], ".")), parts[len(parts)-1] +} + +func normalizeVendor(pkg string) string { + parts := strings.Split(pkg, "/vendor/") + return parts[len(parts)-1] } func (n NamedTypes) getType(t common.Type) *Type { diff --git a/codegen/util.go b/codegen/util.go index bad467ee46..a0ef5414d9 100644 --- a/codegen/util.go +++ b/codegen/util.go @@ -178,10 +178,10 @@ func bindObject(t types.Type, object *Object, imports Imports) { field.GoVarName = structField.Name() switch field.Type.FullSignature() { - case structField.Type().String(): + case normalizeVendor(structField.Type().String()): // everything is fine - case structField.Type().Underlying().String(): + case normalizeVendor(structField.Type().Underlying().String()): pkg, typ := pkgAndType(structField.Type().String()) imp := imports.findByPkg(pkg) field.CastType = typ diff --git a/example/scalars/generated.go b/example/scalars/generated.go index 0ee95783ab..353924ba12 100644 --- a/example/scalars/generated.go +++ b/example/scalars/generated.go @@ -5,6 +5,7 @@ package scalars import ( "bytes" context "context" + external "external" strconv "strconv" time "time" @@ -24,12 +25,11 @@ func MakeExecutableSchema(resolvers Resolvers, opts ...ExecutableOption) graphql } type Resolvers interface { - Query_user(ctx context.Context, id ObjectID) (*User, error) + Query_user(ctx context.Context, id external.ObjectID) (*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) - User_address(ctx context.Context, obj *User) (*Address, error) } type ExecutableOption func(*executableSchema) @@ -100,8 +100,6 @@ func (ec *executionContext) _Address(sel []query.Selection, obj *Address) graphq out.Values[i] = graphql.MarshalString("Address") case "id": out.Values[i] = ec._Address_id(field, obj) - case "street": - out.Values[i] = ec._Address_street(field, obj) case "location": out.Values[i] = ec._Address_location(field, obj) default: @@ -117,14 +115,6 @@ func (ec *executionContext) _Address_id(field graphql.CollectedField, obj *Addre return MarshalID(res) } -func (ec *executionContext) _Address_street(field graphql.CollectedField, obj *Address) graphql.Marshaler { - res := obj.Street - if res == nil { - return graphql.Null - } - return graphql.MarshalString(*res) -} - func (ec *executionContext) _Address_location(field graphql.CollectedField, obj *Address) graphql.Marshaler { res := obj.Location if res == nil { @@ -162,7 +152,7 @@ func (ec *executionContext) _Query(sel []query.Selection) graphql.Marshaler { } func (ec *executionContext) _Query_user(field graphql.CollectedField) graphql.Marshaler { - var arg0 ObjectID + var arg0 external.ObjectID if tmp, ok := field.Args["id"]; ok { var err error arg0, err = UnmarshalID(tmp) @@ -347,24 +337,8 @@ func (ec *executionContext) _User_customResolver(field graphql.CollectedField, o } func (ec *executionContext) _User_address(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_address(ec.ctx, obj) - if err != nil { - ec.Error(err) - return graphql.Null - } - if res == nil { - return graphql.Null - } - return ec._Address(field.Selections, res) - }) + res := obj.Address + return ec._Address(field.Selections, &res) } var __DirectiveImplementors = []string{"__Directive"} @@ -901,7 +875,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 isBanned: Boolean!\n primitiveResolver: String!\n customResolver: Point!\n address: Address\n}\n\ntype Address {\n id: ID!\n street: String\n location: Point\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 isBanned: Boolean!\n primitiveResolver: String!\n customResolver: Point!\n address: Address\n}\n\ntype Address {\n id: ID!\n location: 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) diff --git a/example/scalars/model.go b/example/scalars/model.go index ceaa34f7a9..ce86b28c26 100644 --- a/example/scalars/model.go +++ b/example/scalars/model.go @@ -18,9 +18,9 @@ type Banned bool type User struct { ID external.ObjectID Name string - Location Point // custom scalar types Created time.Time // direct binding to builtin types with external Marshal/Unmarshal methods IsBanned Banned // aliased primitive + Address Address } // Point is serialized as a simple array, eg [1, 2] @@ -76,7 +76,7 @@ func UnmarshalTimestamp(v interface{}) (time.Time, error) { // Lets redefine the base ID type to use an id from an external library func MarshalID(id external.ObjectID) graphql.Marshaler { return graphql.WriterFunc(func(w io.Writer) { - io.WriteString(w, fmt.Sprintf("%d", id)) + io.WriteString(w, strconv.Quote(fmt.Sprintf("=%d=", id))) }) } @@ -86,7 +86,7 @@ func UnmarshalID(v interface{}) (external.ObjectID, error) { if !ok { return 0, fmt.Errorf("ids must be strings") } - i, err := strconv.Atoi(str) + i, err := strconv.Atoi(str[1 : len(str)-1]) return external.ObjectID(i), err } diff --git a/example/scalars/models_gen.go b/example/scalars/models_gen.go index 01991436bd..d8b21cf3d8 100644 --- a/example/scalars/models_gen.go +++ b/example/scalars/models_gen.go @@ -3,11 +3,10 @@ package scalars import ( - external "github.com/vektah/gqlgen/example/scalars/vendor/external" + external "external" ) type Address struct { ID external.ObjectID - Street *string Location *Point } diff --git a/example/scalars/resolvers.go b/example/scalars/resolvers.go index 615a38b25e..07f8088480 100644 --- a/example/scalars/resolvers.go +++ b/example/scalars/resolvers.go @@ -15,10 +15,10 @@ type Resolver struct { func (r *Resolver) Query_user(ctx context.Context, id external.ObjectID) (*User, error) { return &User{ - ID: id, - Name: fmt.Sprintf("Test User %d", id), - Created: time.Now(), - Location: Point{1, 2}, + ID: id, + Name: fmt.Sprintf("Test User %d", id), + Created: time.Now(), + Address: Address{ID: 1, Location: &Point{1, 2}}, }, nil } @@ -35,16 +35,16 @@ func (r *Resolver) Query_search(ctx context.Context, input SearchArgs) ([]User, return []User{ { - ID: 1, - Name: "Test User 1", - Created: created, - Location: location, + ID: 1, + Name: "Test User 1", + Created: created, + Address: Address{ID: 2, Location: &location}, }, { - ID: 2, - Name: "Test User 2", - Created: created, - Location: location, + ID: 2, + Name: "Test User 2", + Created: created, + Address: Address{ID: 1, Location: &location}, }, }, nil } diff --git a/example/scalars/scalar_test.go b/example/scalars/scalar_test.go index e3c84bb511..b26ed9af97 100644 --- a/example/scalars/scalar_test.go +++ b/example/scalars/scalar_test.go @@ -15,7 +15,7 @@ type RawUser struct { ID string Name string Created int64 - Location string + Address struct{ Location string } PrimitiveResolver string CustomResolver string } @@ -30,27 +30,27 @@ func TestScalars(t *testing.T) { Search []RawUser } c.MustPost(`{ - user(id:"1") { + user(id:"=1=") { ...UserData } search(input:{location:"6,66", createdAfter:666}) { ...UserData } } - fragment UserData on User { id name created location }`, &resp) + fragment UserData on User { id name created address { location } }`, &resp) - require.Equal(t, "1,2", resp.User.Location) + require.Equal(t, "1,2", resp.User.Address.Location) require.Equal(t, time.Now().Unix(), resp.User.Created) - require.Equal(t, "6,66", resp.Search[0].Location) + require.Equal(t, "6,66", resp.Search[0].Address.Location) require.Equal(t, int64(666), resp.Search[0].Created) }) t.Run("default search location", func(t *testing.T) { var resp struct{ Search []RawUser } - err := c.Post(`{ search { location } }`, &resp) + err := c.Post(`{ search { address { location } } }`, &resp) require.NoError(t, err) - require.Equal(t, "37,144", resp.Search[0].Location) + require.Equal(t, "37,144", resp.Search[0].Address.Location) }) t.Run("custom error messages", func(t *testing.T) { @@ -62,7 +62,7 @@ func TestScalars(t *testing.T) { t.Run("scalar resolver methods", func(t *testing.T) { var resp struct{ User RawUser } - c.MustPost(`{ user(id: "1") { primitiveResolver, customResolver } }`, &resp) + c.MustPost(`{ user(id: "=1=") { primitiveResolver, customResolver } }`, &resp) require.Equal(t, "test", resp.User.PrimitiveResolver) require.Equal(t, "5,1", resp.User.CustomResolver) diff --git a/example/scalars/schema.graphql b/example/scalars/schema.graphql index 3f89904d39..29c6641792 100644 --- a/example/scalars/schema.graphql +++ b/example/scalars/schema.graphql @@ -15,7 +15,6 @@ type User { type Address { id: ID! - street: String location: Point }