From 160ebab52f7ed9d95944dadaa07050a08e81ac36 Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Tue, 10 Jul 2018 11:42:27 +1000 Subject: [PATCH] Fix a bug custom scalar marshallers in external packages --- codegen/build.go | 6 +- codegen/import.go | 17 +++- codegen/import_build.go | 6 +- codegen/type.go | 12 +-- codegen/util.go | 5 +- example/scalars/.gqlgen.yml | 15 ++-- example/scalars/generated.go | 77 ++++++++++--------- .../{models_gen.go => model/generated.go} | 2 +- example/scalars/{ => model}/model.go | 2 +- example/scalars/resolvers.go | 33 ++++---- test/config.yaml | 8 +- test/generated.go | 42 +++++----- test/{ => models-go}/element.go | 2 +- test/{models => models-go}/generated.go | 0 test/{ => models-go}/viewer.go | 2 +- test/resolvers_test.go | 16 ++-- 16 files changed, 127 insertions(+), 118 deletions(-) rename example/scalars/{models_gen.go => model/generated.go} (92%) rename example/scalars/{ => model}/model.go (99%) rename test/{ => models-go}/element.go (68%) rename test/{models => models-go}/generated.go (100%) rename test/{ => models-go}/viewer.go (81%) diff --git a/codegen/build.go b/codegen/build.go index 42dff9243bf..d56fc06f1f5 100644 --- a/codegen/build.go +++ b/codegen/build.go @@ -103,19 +103,19 @@ func (cfg *Config) bind() (*Build, error) { // Poke a few magic methods into query q := b.Objects.ByName(b.QueryRoot.GQLType) q.Fields = append(q.Fields, Field{ - Type: &Type{namedTypes["__Schema"], []string{modPtr}, ""}, + Type: &Type{namedTypes["__Schema"], []string{modPtr}, nil}, GQLName: "__schema", NoErr: true, GoMethodName: "ec.introspectSchema", Object: q, }) q.Fields = append(q.Fields, Field{ - Type: &Type{namedTypes["__Type"], []string{modPtr}, ""}, + Type: &Type{namedTypes["__Type"], []string{modPtr}, nil}, GQLName: "__type", NoErr: true, GoMethodName: "ec.introspectType", Args: []FieldArgument{ - {GQLName: "name", Type: &Type{namedTypes["String"], []string{}, ""}, Object: &Object{}}, + {GQLName: "name", Type: &Type{namedTypes["String"], []string{}, nil}, Object: &Object{}}, }, Object: q, }) diff --git a/codegen/import.go b/codegen/import.go index 123d310f6d9..b511e8f6fcc 100644 --- a/codegen/import.go +++ b/codegen/import.go @@ -5,9 +5,10 @@ import ( ) type Import struct { - Name string - Alias string - Path string + Name string + Path string + + alias string } type Imports struct { @@ -16,5 +17,13 @@ type Imports struct { } func (i *Import) Write() string { - return i.Alias + " " + strconv.Quote(i.Path) + return i.Alias() + " " + strconv.Quote(i.Path) +} + +func (i *Import) Alias() string { + if i.alias == "" { + panic("alias called before imports are finalized") + } + + return i.alias } diff --git a/codegen/import_build.go b/codegen/import_build.go index 24646b74a20..f0877ed3d42 100644 --- a/codegen/import_build.go +++ b/codegen/import_build.go @@ -88,10 +88,10 @@ func (s Imports) finalize() []*Import { alias = imp.Name + strconv.Itoa(i) i++ if i > 10 { - panic(fmt.Errorf("too many collisions, last attempt was %s", imp.Alias)) + panic(fmt.Errorf("too many collisions, last attempt was %s", alias)) } } - imp.Alias = alias + imp.alias = alias } return s.imports @@ -108,7 +108,7 @@ func (s Imports) findByPath(importPath string) *Import { func (s Imports) findByAlias(alias string) *Import { for _, imp := range s.imports { - if imp.Alias == alias { + if imp.alias == alias { return imp } } diff --git a/codegen/type.go b/codegen/type.go index 31fcefc1317..7af24b3c83c 100644 --- a/codegen/type.go +++ b/codegen/type.go @@ -27,7 +27,7 @@ type Type struct { *NamedType Modifiers []string - CastType string // the type to cast to when unmarshalling + CastType *Ref // the type to cast to when unmarshalling } const ( @@ -40,10 +40,10 @@ func (t Ref) FullName() string { } func (t Ref) PkgDot() string { - if t.Import == nil || t.Import.Alias == "" { + if t.Import == nil || t.Import.Alias() == "" { return "" } - return t.Import.Alias + "." + return t.Import.Alias() + "." } func (t Type) Signature() string { @@ -125,7 +125,7 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s } realResult := result - if t.CastType != "" { + if t.CastType != nil { result = "castTmp" } @@ -140,7 +140,7 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s err = (&{{.result}}).UnmarshalGQL({{.raw}}) {{- end }} {{- if .t.CastType }} - {{ .realResult }} = {{.t.CastType}}(castTmp) + {{ .realResult }} = {{.t.CastType.FullName}}(castTmp) {{- end }}`, map[string]interface{}{ "realResult": realResult, "result": result, @@ -150,7 +150,7 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s } func (t Type) Marshal(val string) string { - if t.CastType != "" { + if t.CastType != nil { val = t.GoType + "(" + val + ")" } diff --git a/codegen/util.go b/codegen/util.go index dfd44d50a45..03e8e51d1d5 100644 --- a/codegen/util.go +++ b/codegen/util.go @@ -185,10 +185,7 @@ func bindObject(t types.Type, object *Object, imports *Imports) error { case normalizeVendor(structField.Type().Underlying().String()): pkg, typ := pkgAndType(structField.Type().String()) imp := imports.findByPath(pkg) - field.CastType = typ - if imp != nil { - field.CastType = imp.Alias + "." + typ - } + field.CastType = &Ref{GoType: typ, Import: imp} default: // type mismatch, require custom resolver for field diff --git a/example/scalars/.gqlgen.yml b/example/scalars/.gqlgen.yml index 366f4f2f50b..a1806a548ec 100644 --- a/example/scalars/.gqlgen.yml +++ b/example/scalars/.gqlgen.yml @@ -1,13 +1,16 @@ +model: + filename: model/generated.go + models: User: - model: github.com/vektah/gqlgen/example/scalars.User + model: github.com/vektah/gqlgen/example/scalars/model.User Timestamp: - model: github.com/vektah/gqlgen/example/scalars.Timestamp + model: github.com/vektah/gqlgen/example/scalars/model.Timestamp SearchArgs: - model: github.com/vektah/gqlgen/example/scalars.SearchArgs + model: github.com/vektah/gqlgen/example/scalars/model.SearchArgs Point: - model: github.com/vektah/gqlgen/example/scalars.Point + model: github.com/vektah/gqlgen/example/scalars/model.Point ID: - model: github.com/vektah/gqlgen/example/scalars.ID + model: github.com/vektah/gqlgen/example/scalars/model.ID Tier: - model: github.com/vektah/gqlgen/example/scalars.Tier + model: github.com/vektah/gqlgen/example/scalars/model.Tier diff --git a/example/scalars/generated.go b/example/scalars/generated.go index 732e3922db3..dc588ec34a0 100644 --- a/example/scalars/generated.go +++ b/example/scalars/generated.go @@ -9,6 +9,7 @@ import ( strconv "strconv" time "time" + model "github.com/vektah/gqlgen/example/scalars/model" graphql "github.com/vektah/gqlgen/graphql" introspection "github.com/vektah/gqlgen/neelance/introspection" query "github.com/vektah/gqlgen/neelance/query" @@ -26,11 +27,11 @@ func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema { } type Resolvers interface { - Query_user(ctx context.Context, id external.ObjectID) (*User, error) - Query_search(ctx context.Context, input SearchArgs) ([]User, error) + Query_user(ctx context.Context, id external.ObjectID) (*model.User, error) + Query_search(ctx context.Context, input model.SearchArgs) ([]model.User, error) - User_primitiveResolver(ctx context.Context, obj *User) (string, error) - User_customResolver(ctx context.Context, obj *User) (Point, error) + User_primitiveResolver(ctx context.Context, obj *model.User) (string, error) + User_customResolver(ctx context.Context, obj *model.User) (model.Point, error) } type ResolverRoot interface { @@ -38,31 +39,31 @@ type ResolverRoot interface { User() UserResolver } type QueryResolver interface { - User(ctx context.Context, id external.ObjectID) (*User, error) - Search(ctx context.Context, input SearchArgs) ([]User, error) + User(ctx context.Context, id external.ObjectID) (*model.User, error) + Search(ctx context.Context, input model.SearchArgs) ([]model.User, error) } type UserResolver interface { - PrimitiveResolver(ctx context.Context, obj *User) (string, error) - CustomResolver(ctx context.Context, obj *User) (Point, error) + PrimitiveResolver(ctx context.Context, obj *model.User) (string, error) + CustomResolver(ctx context.Context, obj *model.User) (model.Point, error) } type shortMapper struct { r ResolverRoot } -func (s shortMapper) Query_user(ctx context.Context, id external.ObjectID) (*User, error) { +func (s shortMapper) Query_user(ctx context.Context, id external.ObjectID) (*model.User, error) { return s.r.Query().User(ctx, id) } -func (s shortMapper) Query_search(ctx context.Context, input SearchArgs) ([]User, error) { +func (s shortMapper) Query_search(ctx context.Context, input model.SearchArgs) ([]model.User, error) { return s.r.Query().Search(ctx, input) } -func (s shortMapper) User_primitiveResolver(ctx context.Context, obj *User) (string, error) { +func (s shortMapper) User_primitiveResolver(ctx context.Context, obj *model.User) (string, error) { return s.r.User().PrimitiveResolver(ctx, obj) } -func (s shortMapper) User_customResolver(ctx context.Context, obj *User) (Point, error) { +func (s shortMapper) User_customResolver(ctx context.Context, obj *model.User) (model.Point, error) { return s.r.User().CustomResolver(ctx, obj) } @@ -107,7 +108,7 @@ type executionContext struct { var addressImplementors = []string{"Address"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) _Address(ctx context.Context, sel []query.Selection, obj *Address) graphql.Marshaler { +func (ec *executionContext) _Address(ctx context.Context, sel []query.Selection, obj *model.Address) graphql.Marshaler { fields := graphql.CollectFields(ec.Doc, sel, addressImplementors, ec.Variables) out := graphql.NewOrderedMap(len(fields)) @@ -129,7 +130,7 @@ func (ec *executionContext) _Address(ctx context.Context, sel []query.Selection, return out } -func (ec *executionContext) _Address_id(ctx context.Context, field graphql.CollectedField, obj *Address) graphql.Marshaler { +func (ec *executionContext) _Address_id(ctx context.Context, field graphql.CollectedField, obj *model.Address) graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.Object = "Address" rctx.Args = nil @@ -137,10 +138,10 @@ func (ec *executionContext) _Address_id(ctx context.Context, field graphql.Colle rctx.PushField(field.Alias) defer rctx.Pop() res := obj.ID - return MarshalID(res) + return model.MarshalID(res) } -func (ec *executionContext) _Address_location(ctx context.Context, field graphql.CollectedField, obj *Address) graphql.Marshaler { +func (ec *executionContext) _Address_location(ctx context.Context, field graphql.CollectedField, obj *model.Address) graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.Object = "Address" rctx.Args = nil @@ -192,7 +193,7 @@ func (ec *executionContext) _Query_user(ctx context.Context, field graphql.Colle var arg0 external.ObjectID if tmp, ok := field.Args["id"]; ok { var err error - arg0, err = UnmarshalID(tmp) + arg0, err = model.UnmarshalID(tmp) if err != nil { ec.Error(ctx, err) return graphql.Null @@ -223,7 +224,7 @@ func (ec *executionContext) _Query_user(ctx context.Context, field graphql.Colle if resTmp == nil { return graphql.Null } - res := resTmp.(*User) + res := resTmp.(*model.User) if res == nil { return graphql.Null } @@ -233,7 +234,7 @@ func (ec *executionContext) _Query_user(ctx context.Context, field graphql.Colle func (ec *executionContext) _Query_search(ctx context.Context, field graphql.CollectedField) graphql.Marshaler { args := map[string]interface{}{} - var arg0 SearchArgs + var arg0 model.SearchArgs if tmp, ok := field.Args["input"]; ok { var err error arg0, err = UnmarshalSearchArgs(tmp) @@ -267,7 +268,7 @@ func (ec *executionContext) _Query_search(ctx context.Context, field graphql.Col }() resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { - return ec.resolvers.Query_search(ctx, args["input"].(SearchArgs)) + return ec.resolvers.Query_search(ctx, args["input"].(model.SearchArgs)) }) if err != nil { ec.Error(ctx, err) @@ -276,7 +277,7 @@ func (ec *executionContext) _Query_search(ctx context.Context, field graphql.Col if resTmp == nil { return graphql.Null } - res := resTmp.([]User) + res := resTmp.([]model.User) arr1 := graphql.Array{} for idx1 := range res { arr1 = append(arr1, func() graphql.Marshaler { @@ -332,7 +333,7 @@ func (ec *executionContext) _Query___type(ctx context.Context, field graphql.Col var userImplementors = []string{"User"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) _User(ctx context.Context, sel []query.Selection, obj *User) graphql.Marshaler { +func (ec *executionContext) _User(ctx context.Context, sel []query.Selection, obj *model.User) graphql.Marshaler { fields := graphql.CollectFields(ec.Doc, sel, userImplementors, ec.Variables) out := graphql.NewOrderedMap(len(fields)) @@ -366,7 +367,7 @@ func (ec *executionContext) _User(ctx context.Context, sel []query.Selection, ob return out } -func (ec *executionContext) _User_id(ctx context.Context, field graphql.CollectedField, obj *User) graphql.Marshaler { +func (ec *executionContext) _User_id(ctx context.Context, field graphql.CollectedField, obj *model.User) graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.Object = "User" rctx.Args = nil @@ -374,10 +375,10 @@ func (ec *executionContext) _User_id(ctx context.Context, field graphql.Collecte rctx.PushField(field.Alias) defer rctx.Pop() res := obj.ID - return MarshalID(res) + return model.MarshalID(res) } -func (ec *executionContext) _User_name(ctx context.Context, field graphql.CollectedField, obj *User) graphql.Marshaler { +func (ec *executionContext) _User_name(ctx context.Context, field graphql.CollectedField, obj *model.User) graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.Object = "User" rctx.Args = nil @@ -388,7 +389,7 @@ func (ec *executionContext) _User_name(ctx context.Context, field graphql.Collec return graphql.MarshalString(res) } -func (ec *executionContext) _User_created(ctx context.Context, field graphql.CollectedField, obj *User) graphql.Marshaler { +func (ec *executionContext) _User_created(ctx context.Context, field graphql.CollectedField, obj *model.User) graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.Object = "User" rctx.Args = nil @@ -396,10 +397,10 @@ func (ec *executionContext) _User_created(ctx context.Context, field graphql.Col rctx.PushField(field.Alias) defer rctx.Pop() res := obj.Created - return MarshalTimestamp(res) + return model.MarshalTimestamp(res) } -func (ec *executionContext) _User_isBanned(ctx context.Context, field graphql.CollectedField, obj *User) graphql.Marshaler { +func (ec *executionContext) _User_isBanned(ctx context.Context, field graphql.CollectedField, obj *model.User) graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.Object = "User" rctx.Args = nil @@ -410,7 +411,7 @@ func (ec *executionContext) _User_isBanned(ctx context.Context, field graphql.Co return graphql.MarshalBoolean(bool(res)) } -func (ec *executionContext) _User_primitiveResolver(ctx context.Context, field graphql.CollectedField, obj *User) graphql.Marshaler { +func (ec *executionContext) _User_primitiveResolver(ctx context.Context, field graphql.CollectedField, obj *model.User) graphql.Marshaler { ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{ Object: "User", Args: nil, @@ -440,7 +441,7 @@ func (ec *executionContext) _User_primitiveResolver(ctx context.Context, field g }) } -func (ec *executionContext) _User_customResolver(ctx context.Context, field graphql.CollectedField, obj *User) graphql.Marshaler { +func (ec *executionContext) _User_customResolver(ctx context.Context, field graphql.CollectedField, obj *model.User) graphql.Marshaler { ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{ Object: "User", Args: nil, @@ -465,12 +466,12 @@ func (ec *executionContext) _User_customResolver(ctx context.Context, field grap if resTmp == nil { return graphql.Null } - res := resTmp.(Point) + res := resTmp.(model.Point) return res }) } -func (ec *executionContext) _User_address(ctx context.Context, field graphql.CollectedField, obj *User) graphql.Marshaler { +func (ec *executionContext) _User_address(ctx context.Context, field graphql.CollectedField, obj *model.User) graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.Object = "User" rctx.Args = nil @@ -481,7 +482,7 @@ func (ec *executionContext) _User_address(ctx context.Context, field graphql.Col return ec._Address(ctx, field.Selections, &res) } -func (ec *executionContext) _User_tier(ctx context.Context, field graphql.CollectedField, obj *User) graphql.Marshaler { +func (ec *executionContext) _User_tier(ctx context.Context, field graphql.CollectedField, obj *model.User) graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.Object = "User" rctx.Args = nil @@ -1218,15 +1219,15 @@ func (ec *executionContext) ___Type_ofType(ctx context.Context, field graphql.Co return ec.___Type(ctx, field.Selections, res) } -func UnmarshalSearchArgs(v interface{}) (SearchArgs, error) { - var it SearchArgs +func UnmarshalSearchArgs(v interface{}) (model.SearchArgs, error) { + var it model.SearchArgs var asMap = v.(map[string]interface{}) for k, v := range asMap { switch k { case "location": var err error - var ptr1 Point + var ptr1 model.Point if v != nil { err = (&ptr1).UnmarshalGQL(v) it.Location = &ptr1 @@ -1239,7 +1240,7 @@ func UnmarshalSearchArgs(v interface{}) (SearchArgs, error) { var err error var ptr1 time.Time if v != nil { - ptr1, err = UnmarshalTimestamp(v) + ptr1, err = model.UnmarshalTimestamp(v) it.CreatedAfter = &ptr1 } @@ -1251,7 +1252,7 @@ func UnmarshalSearchArgs(v interface{}) (SearchArgs, error) { var castTmp bool castTmp, err = graphql.UnmarshalBoolean(v) - it.IsBanned = Banned(castTmp) + it.IsBanned = model.Banned(castTmp) if err != nil { return it, err } diff --git a/example/scalars/models_gen.go b/example/scalars/model/generated.go similarity index 92% rename from example/scalars/models_gen.go rename to example/scalars/model/generated.go index 4a13829c15c..39afffeb75b 100644 --- a/example/scalars/models_gen.go +++ b/example/scalars/model/generated.go @@ -1,6 +1,6 @@ // Code generated by github.com/vektah/gqlgen, DO NOT EDIT. -package scalars +package model import ( external "external" diff --git a/example/scalars/model.go b/example/scalars/model/model.go similarity index 99% rename from example/scalars/model.go rename to example/scalars/model/model.go index 6c64475852b..7771affd63b 100644 --- a/example/scalars/model.go +++ b/example/scalars/model/model.go @@ -1,4 +1,4 @@ -package scalars +package model import ( "errors" diff --git a/example/scalars/resolvers.go b/example/scalars/resolvers.go index bf5cc020982..8b45f80246c 100644 --- a/example/scalars/resolvers.go +++ b/example/scalars/resolvers.go @@ -4,27 +4,28 @@ package scalars import ( context "context" + "external" "fmt" - "time" + time "time" - "external" + "github.com/vektah/gqlgen/example/scalars/model" ) type Resolver struct { } -func (r *Resolver) Query_user(ctx context.Context, id external.ObjectID) (*User, error) { - return &User{ +func (r *Resolver) Query_user(ctx context.Context, id external.ObjectID) (*model.User, error) { + return &model.User{ ID: id, Name: fmt.Sprintf("Test User %d", id), Created: time.Now(), - Address: Address{ID: 1, Location: &Point{1, 2}}, - Tier: TierC, + Address: model.Address{ID: 1, Location: &model.Point{1, 2}}, + Tier: model.TierC, }, nil } -func (r *Resolver) Query_search(ctx context.Context, input SearchArgs) ([]User, error) { - location := Point{1, 2} +func (r *Resolver) Query_search(ctx context.Context, input model.SearchArgs) ([]model.User, error) { + location := model.Point{1, 2} if input.Location != nil { location = *input.Location } @@ -34,28 +35,28 @@ func (r *Resolver) Query_search(ctx context.Context, input SearchArgs) ([]User, created = *input.CreatedAfter } - return []User{ + return []model.User{ { ID: 1, Name: "Test User 1", Created: created, - Address: Address{ID: 2, Location: &location}, - Tier: TierA, + Address: model.Address{ID: 2, Location: &location}, + Tier: model.TierA, }, { ID: 2, Name: "Test User 2", Created: created, - Address: Address{ID: 1, Location: &location}, - Tier: TierC, + Address: model.Address{ID: 1, Location: &location}, + Tier: model.TierC, }, }, nil } -func (r *Resolver) User_primitiveResolver(ctx context.Context, obj *User) (string, error) { +func (r *Resolver) User_primitiveResolver(ctx context.Context, obj *model.User) (string, error) { return "test", nil } -func (r *Resolver) User_customResolver(ctx context.Context, obj *User) (Point, error) { - return Point{5, 1}, nil +func (r *Resolver) User_customResolver(ctx context.Context, obj *model.User) (model.Point, error) { + return model.Point{5, 1}, nil } diff --git a/test/config.yaml b/test/config.yaml index 45481ebd415..4a7950158c8 100644 --- a/test/config.yaml +++ b/test/config.yaml @@ -2,16 +2,14 @@ schema: schema.graphql exec: filename: generated.go - # package: model: - filename: models/generated.go - # package: + filename: models-go/generated.go models: Element: - model: github.com/vektah/gqlgen/test.Element + model: github.com/vektah/gqlgen/test/models-go.Element Viewer: - model: github.com/vektah/gqlgen/test.Viewer + model: github.com/vektah/gqlgen/test/models-go.Viewer User: model: remote_api.User fields: diff --git a/test/generated.go b/test/generated.go index 2dde771bb54..612bc6fc33f 100644 --- a/test/generated.go +++ b/test/generated.go @@ -12,7 +12,7 @@ import ( introspection "github.com/vektah/gqlgen/neelance/introspection" query "github.com/vektah/gqlgen/neelance/query" schema "github.com/vektah/gqlgen/neelance/schema" - models "github.com/vektah/gqlgen/test/models" + models "github.com/vektah/gqlgen/test/models-go" ) // MakeExecutableSchema creates an ExecutableSchema from the Resolvers interface. @@ -26,11 +26,11 @@ func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema { } type Resolvers interface { - Element_child(ctx context.Context, obj *Element) (Element, error) - Element_error(ctx context.Context, obj *Element) (bool, error) - Query_path(ctx context.Context) ([]Element, error) + Element_child(ctx context.Context, obj *models.Element) (models.Element, error) + Element_error(ctx context.Context, obj *models.Element) (bool, error) + Query_path(ctx context.Context) ([]models.Element, error) Query_date(ctx context.Context, filter models.DateFilter) (bool, error) - Query_viewer(ctx context.Context) (*Viewer, error) + Query_viewer(ctx context.Context) (*models.Viewer, error) Query_jsonEncoding(ctx context.Context) (string, error) User_likes(ctx context.Context, obj *remote_api.User) ([]string, error) @@ -42,13 +42,13 @@ type ResolverRoot interface { User() UserResolver } type ElementResolver interface { - Child(ctx context.Context, obj *Element) (Element, error) - Error(ctx context.Context, obj *Element) (bool, error) + Child(ctx context.Context, obj *models.Element) (models.Element, error) + Error(ctx context.Context, obj *models.Element) (bool, error) } type QueryResolver interface { - Path(ctx context.Context) ([]Element, error) + Path(ctx context.Context) ([]models.Element, error) Date(ctx context.Context, filter models.DateFilter) (bool, error) - Viewer(ctx context.Context) (*Viewer, error) + Viewer(ctx context.Context) (*models.Viewer, error) JsonEncoding(ctx context.Context) (string, error) } type UserResolver interface { @@ -59,15 +59,15 @@ type shortMapper struct { r ResolverRoot } -func (s shortMapper) Element_child(ctx context.Context, obj *Element) (Element, error) { +func (s shortMapper) Element_child(ctx context.Context, obj *models.Element) (models.Element, error) { return s.r.Element().Child(ctx, obj) } -func (s shortMapper) Element_error(ctx context.Context, obj *Element) (bool, error) { +func (s shortMapper) Element_error(ctx context.Context, obj *models.Element) (bool, error) { return s.r.Element().Error(ctx, obj) } -func (s shortMapper) Query_path(ctx context.Context) ([]Element, error) { +func (s shortMapper) Query_path(ctx context.Context) ([]models.Element, error) { return s.r.Query().Path(ctx) } @@ -75,7 +75,7 @@ func (s shortMapper) Query_date(ctx context.Context, filter models.DateFilter) ( return s.r.Query().Date(ctx, filter) } -func (s shortMapper) Query_viewer(ctx context.Context) (*Viewer, error) { +func (s shortMapper) Query_viewer(ctx context.Context) (*models.Viewer, error) { return s.r.Query().Viewer(ctx) } @@ -128,7 +128,7 @@ type executionContext struct { var elementImplementors = []string{"Element"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) _Element(ctx context.Context, sel []query.Selection, obj *Element) graphql.Marshaler { +func (ec *executionContext) _Element(ctx context.Context, sel []query.Selection, obj *models.Element) graphql.Marshaler { fields := graphql.CollectFields(ec.Doc, sel, elementImplementors, ec.Variables) out := graphql.NewOrderedMap(len(fields)) @@ -150,7 +150,7 @@ func (ec *executionContext) _Element(ctx context.Context, sel []query.Selection, return out } -func (ec *executionContext) _Element_child(ctx context.Context, field graphql.CollectedField, obj *Element) graphql.Marshaler { +func (ec *executionContext) _Element_child(ctx context.Context, field graphql.CollectedField, obj *models.Element) graphql.Marshaler { ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{ Object: "Element", Args: nil, @@ -175,12 +175,12 @@ func (ec *executionContext) _Element_child(ctx context.Context, field graphql.Co if resTmp == nil { return graphql.Null } - res := resTmp.(Element) + res := resTmp.(models.Element) return ec._Element(ctx, field.Selections, &res) }) } -func (ec *executionContext) _Element_error(ctx context.Context, field graphql.CollectedField, obj *Element) graphql.Marshaler { +func (ec *executionContext) _Element_error(ctx context.Context, field graphql.CollectedField, obj *models.Element) graphql.Marshaler { ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{ Object: "Element", Args: nil, @@ -272,7 +272,7 @@ func (ec *executionContext) _Query_path(ctx context.Context, field graphql.Colle if resTmp == nil { return graphql.Null } - res := resTmp.([]Element) + res := resTmp.([]models.Element) arr1 := graphql.Array{} for idx1 := range res { arr1 = append(arr1, func() graphql.Marshaler { @@ -352,7 +352,7 @@ func (ec *executionContext) _Query_viewer(ctx context.Context, field graphql.Col if resTmp == nil { return graphql.Null } - res := resTmp.(*Viewer) + res := resTmp.(*models.Viewer) if res == nil { return graphql.Null } @@ -497,7 +497,7 @@ func (ec *executionContext) _User_likes(ctx context.Context, field graphql.Colle var viewerImplementors = []string{"Viewer"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) _Viewer(ctx context.Context, sel []query.Selection, obj *Viewer) graphql.Marshaler { +func (ec *executionContext) _Viewer(ctx context.Context, sel []query.Selection, obj *models.Viewer) graphql.Marshaler { fields := graphql.CollectFields(ec.Doc, sel, viewerImplementors, ec.Variables) out := graphql.NewOrderedMap(len(fields)) @@ -517,7 +517,7 @@ func (ec *executionContext) _Viewer(ctx context.Context, sel []query.Selection, return out } -func (ec *executionContext) _Viewer_user(ctx context.Context, field graphql.CollectedField, obj *Viewer) graphql.Marshaler { +func (ec *executionContext) _Viewer_user(ctx context.Context, field graphql.CollectedField, obj *models.Viewer) graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.Object = "Viewer" rctx.Args = nil diff --git a/test/element.go b/test/models-go/element.go similarity index 68% rename from test/element.go rename to test/models-go/element.go index 860109e6b1b..91e9767fa79 100644 --- a/test/element.go +++ b/test/models-go/element.go @@ -1,4 +1,4 @@ -package test +package models type Element struct { ID int diff --git a/test/models/generated.go b/test/models-go/generated.go similarity index 100% rename from test/models/generated.go rename to test/models-go/generated.go diff --git a/test/viewer.go b/test/models-go/viewer.go similarity index 81% rename from test/viewer.go rename to test/models-go/viewer.go index 44786f0bf49..9b0c9811277 100644 --- a/test/viewer.go +++ b/test/models-go/viewer.go @@ -1,4 +1,4 @@ -package test +package models import "remote_api" diff --git a/test/resolvers_test.go b/test/resolvers_test.go index 94192722306..0b5fdf33874 100644 --- a/test/resolvers_test.go +++ b/test/resolvers_test.go @@ -17,7 +17,7 @@ import ( "github.com/vektah/gqlgen/client" "github.com/vektah/gqlgen/graphql" "github.com/vektah/gqlgen/handler" - models "github.com/vektah/gqlgen/test/models" + "github.com/vektah/gqlgen/test/models-go" ) func TestCustomErrorPresenter(t *testing.T) { @@ -104,8 +104,8 @@ func (r *testResolvers) Query_jsonEncoding(ctx context.Context) (string, error) return "\U000fe4ed", nil } -func (r *testResolvers) Query_viewer(ctx context.Context) (*Viewer, error) { - return &Viewer{ +func (r *testResolvers) Query_viewer(ctx context.Context) (*models.Viewer, error) { + return &models.Viewer{ User: &remote_api.User{Name: "Bob"}, }, nil } @@ -114,15 +114,15 @@ func (r *testResolvers) Query_date(ctx context.Context, filter models.DateFilter return r.queryDate(ctx, filter) } -func (r *testResolvers) Query_path(ctx context.Context) ([]Element, error) { - return []Element{{1}, {2}, {3}, {4}}, nil +func (r *testResolvers) Query_path(ctx context.Context) ([]models.Element, error) { + return []models.Element{{1}, {2}, {3}, {4}}, nil } -func (r *testResolvers) Element_child(ctx context.Context, obj *Element) (Element, error) { - return Element{obj.ID * 10}, nil +func (r *testResolvers) Element_child(ctx context.Context, obj *models.Element) (models.Element, error) { + return models.Element{obj.ID * 10}, nil } -func (r *testResolvers) Element_error(ctx context.Context, obj *Element) (bool, error) { +func (r *testResolvers) Element_error(ctx context.Context, obj *models.Element) (bool, error) { // A silly hack to make the result order stable time.Sleep(time.Duration(obj.ID) * 10 * time.Millisecond)