From 30d235bc781c1976c08def2ca5283998f4786d76 Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Mon, 11 Mar 2019 12:42:15 +1100 Subject: [PATCH] Fix default scalars --- codegen/testserver/generated.go | 85 +++++++++++++++++++++++ codegen/testserver/models-gen.go | 4 ++ codegen/testserver/scalar_default.graphql | 4 ++ plugin/modelgen/models.go | 22 ++++-- 4 files changed, 109 insertions(+), 6 deletions(-) diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index ff1805d44be..20153bebead 100644 --- a/codegen/testserver/generated.go +++ b/codegen/testserver/generated.go @@ -74,6 +74,10 @@ type ComplexityRoot struct { Area func(childComplexity int) int } + EmbeddedDefaultScalar struct { + Value func(childComplexity int) int + } + EmbeddedPointer struct { ID func(childComplexity int) int Title func(childComplexity int) int @@ -308,6 +312,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Circle.Area(childComplexity), true + case "EmbeddedDefaultScalar.Value": + if e.complexity.EmbeddedDefaultScalar.Value == nil { + break + } + + return e.complexity.EmbeddedDefaultScalar.Value(childComplexity), true + case "EmbeddedPointer.ID": if e.complexity.EmbeddedPointer.ID == nil { break @@ -925,6 +936,10 @@ scalar MarshalPanic """ This doesnt have an implementation in the typemap, so it should act like a string """ scalar DefaultScalarImplementation + +type EmbeddedDefaultScalar { + value: DefaultScalarImplementation +} `}, &ast.Source{Name: "schema.graphql", Input: `type Query { invalidIdentifier: InvalidIdentifier @@ -1918,6 +1933,29 @@ func (ec *executionContext) _Circle_area(ctx context.Context, field graphql.Coll return ec.marshalOFloat2float64(ctx, field.Selections, res) } +func (ec *executionContext) _EmbeddedDefaultScalar_value(ctx context.Context, field graphql.CollectedField, obj *EmbeddedDefaultScalar) graphql.Marshaler { + ctx = ec.Tracer.StartFieldExecution(ctx, field) + defer func() { ec.Tracer.EndFieldExecution(ctx) }() + rctx := &graphql.ResolverContext{ + Object: "EmbeddedDefaultScalar", + Field: field, + Args: nil, + } + ctx = graphql.WithResolverContext(ctx, rctx) + ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) + resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.Value, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*string) + rctx.Result = res + ctx = ec.Tracer.StartFieldChildExecution(ctx) + return ec.marshalODefaultScalarImplementation2ᚖstring(ctx, field.Selections, res) +} + func (ec *executionContext) _EmbeddedPointer_ID(ctx context.Context, field graphql.CollectedField, obj *EmbeddedPointerModel) graphql.Marshaler { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { ec.Tracer.EndFieldExecution(ctx) }() @@ -4760,6 +4798,30 @@ func (ec *executionContext) _Circle(ctx context.Context, sel ast.SelectionSet, o return out } +var embeddedDefaultScalarImplementors = []string{"EmbeddedDefaultScalar"} + +func (ec *executionContext) _EmbeddedDefaultScalar(ctx context.Context, sel ast.SelectionSet, obj *EmbeddedDefaultScalar) graphql.Marshaler { + fields := graphql.CollectFields(ctx, sel, embeddedDefaultScalarImplementors) + + out := graphql.NewFieldSet(fields) + invalid := false + for i, field := range fields { + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString("EmbeddedDefaultScalar") + case "value": + out.Values[i] = ec._EmbeddedDefaultScalar_value(ctx, field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + out.Dispatch() + if invalid { + return graphql.Null + } + return out +} + var embeddedPointerImplementors = []string{"EmbeddedPointer"} func (ec *executionContext) _EmbeddedPointer(ctx context.Context, sel ast.SelectionSet, obj *EmbeddedPointerModel) graphql.Marshaler { @@ -6380,6 +6442,29 @@ func (ec *executionContext) marshalOCircle2ᚖgithubᚗcomᚋ99designsᚋgqlgen return ec._Circle(ctx, sel, v) } +func (ec *executionContext) unmarshalODefaultScalarImplementation2string(ctx context.Context, v interface{}) (string, error) { + return graphql.UnmarshalString(v) +} + +func (ec *executionContext) marshalODefaultScalarImplementation2string(ctx context.Context, sel ast.SelectionSet, v string) graphql.Marshaler { + return graphql.MarshalString(v) +} + +func (ec *executionContext) unmarshalODefaultScalarImplementation2ᚖstring(ctx context.Context, v interface{}) (*string, error) { + if v == nil { + return nil, nil + } + res, err := ec.unmarshalODefaultScalarImplementation2string(ctx, v) + return &res, err +} + +func (ec *executionContext) marshalODefaultScalarImplementation2ᚖstring(ctx context.Context, sel ast.SelectionSet, v *string) graphql.Marshaler { + if v == nil { + return graphql.Null + } + return ec.marshalODefaultScalarImplementation2string(ctx, sel, *v) +} + func (ec *executionContext) marshalOError2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐError(ctx context.Context, sel ast.SelectionSet, v Error) graphql.Marshaler { return ec._Error(ctx, sel, &v) } diff --git a/codegen/testserver/models-gen.go b/codegen/testserver/models-gen.go index 2ab880335cc..7e26b2ccdad 100644 --- a/codegen/testserver/models-gen.go +++ b/codegen/testserver/models-gen.go @@ -17,6 +17,10 @@ type AbIt struct { ID string `json:"id"` } +type EmbeddedDefaultScalar struct { + Value *string `json:"value"` +} + type InnerDirectives struct { Message string `json:"message"` } diff --git a/codegen/testserver/scalar_default.graphql b/codegen/testserver/scalar_default.graphql index 56495bad727..5e3a9c08fd1 100644 --- a/codegen/testserver/scalar_default.graphql +++ b/codegen/testserver/scalar_default.graphql @@ -4,3 +4,7 @@ extend type Query { """ This doesnt have an implementation in the typemap, so it should act like a string """ scalar DefaultScalarImplementation + +type EmbeddedDefaultScalar { + value: DefaultScalarImplementation +} diff --git a/plugin/modelgen/models.go b/plugin/modelgen/models.go index b4e089e994f..41c183c167e 100644 --- a/plugin/modelgen/models.go +++ b/plugin/modelgen/models.go @@ -118,12 +118,22 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error { return err } } else { - // no user defined model, must reference another generated model - typ = types.NewNamed( - types.NewTypeName(0, cfg.Model.Pkg(), templates.ToGo(field.Type.Name()), nil), - nil, - nil, - ) + fieldDef := schema.Types[field.Type.Name()] + if fieldDef.Kind == ast.Scalar { + // no user defined model, referencing a default scalar + typ = types.NewNamed( + types.NewTypeName(0, cfg.Model.Pkg(), "string", nil), + nil, + nil, + ) + } else { + // no user defined model, must reference another generated model + typ = types.NewNamed( + types.NewTypeName(0, cfg.Model.Pkg(), templates.ToGo(field.Type.Name()), nil), + nil, + nil, + ) + } } name := field.Name