From 1d79b3e5a0481ebb8d83f8a494a659b0fe0b53ce Mon Sep 17 00:00:00 2001 From: matthew clemens Date: Sat, 9 Feb 2019 14:36:34 -0800 Subject: [PATCH] Fix directives on fields with custom scalars --- codegen/input.gotpl | 2 +- codegen/testserver/generated.go | 46 +++++++++++++++++++++++++++++++ codegen/testserver/gqlgen.yml | 2 ++ codegen/testserver/models-gen.go | 1 + codegen/testserver/schema.graphql | 3 ++ codegen/testserver/thirdparty.go | 30 ++++++++++++++++++++ 6 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 codegen/testserver/thirdparty.go diff --git a/codegen/input.gotpl b/codegen/input.gotpl index 7345bf4fe16..c8ac7ad3a5d 100644 --- a/codegen/input.gotpl +++ b/codegen/input.gotpl @@ -35,7 +35,7 @@ if err != nil { return it, err } - if data, ok := tmp.({{ $field.TypeReference.GO }}) ; ok { + if data, ok := tmp.({{ $field.TypeReference.GO | ref }}) ; ok { it.{{$field.GoFieldName}} = data } else { return it, fmt.Errorf(`unexpected type %T from directive, should be {{ $field.TypeReference.GO }}`, tmp) diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index 7375bb8a83a..7bba322c1d7 100644 --- a/codegen/testserver/generated.go +++ b/codegen/testserver/generated.go @@ -759,10 +759,13 @@ input OuterInput { inner: InnerInput! } +scalar ThirdParty + input InputDirectives { text: String! @length(min: 0, max: 7, message: "not valid") inner: InnerDirectives! innerNullable: InnerDirectives + thirdParty: ThirdParty @length(min: 0, max: 7, message: "not valid") } input InnerDirectives { @@ -3489,6 +3492,26 @@ func (ec *executionContext) unmarshalInputInputDirectives(ctx context.Context, v if err != nil { return it, err } + case "thirdParty": + var err error + getField0 := func(ctx context.Context) (interface{}, error) { + return ec.unmarshalOThirdParty2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐThirdParty(ctx, v) + } + getField1 := func(ctx context.Context) (res interface{}, err error) { + max := 7 + n := getField0 + return ec.directives.Length(ctx, it, n, 0, &max) + } + + tmp, err := getField1(ctx) + if err != nil { + return it, err + } + if data, ok := tmp.(*ThirdParty); ok { + it.ThirdParty = data + } else { + return it, fmt.Errorf(`unexpected type %T from directive, should be *github.com/99designs/gqlgen/codegen/testserver.ThirdParty`, tmp) + } } } @@ -5211,6 +5234,29 @@ func (ec *executionContext) marshalOString2ᚖstring(ctx context.Context, sel as return ec.marshalOString2string(ctx, sel, *v) } +func (ec *executionContext) unmarshalOThirdParty2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐThirdParty(ctx context.Context, v interface{}) (ThirdParty, error) { + return UnmarshalThirdParty(v) +} + +func (ec *executionContext) marshalOThirdParty2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐThirdParty(ctx context.Context, sel ast.SelectionSet, v ThirdParty) graphql.Marshaler { + return MarshalThirdParty(v) +} + +func (ec *executionContext) unmarshalOThirdParty2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐThirdParty(ctx context.Context, v interface{}) (*ThirdParty, error) { + if v == nil { + return nil, nil + } + res, err := ec.unmarshalOThirdParty2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐThirdParty(ctx, v) + return &res, err +} + +func (ec *executionContext) marshalOThirdParty2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐThirdParty(ctx context.Context, sel ast.SelectionSet, v *ThirdParty) graphql.Marshaler { + if v == nil { + return graphql.Null + } + return ec.marshalOThirdParty2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐThirdParty(ctx, sel, *v) +} + func (ec *executionContext) unmarshalOTime2timeᚐTime(ctx context.Context, v interface{}) (time.Time, error) { return graphql.UnmarshalTime(v) } diff --git a/codegen/testserver/gqlgen.yml b/codegen/testserver/gqlgen.yml index 7a6e8e5674e..f326e6d0556 100644 --- a/codegen/testserver/gqlgen.yml +++ b/codegen/testserver/gqlgen.yml @@ -38,3 +38,5 @@ models: model: "github.com/99designs/gqlgen/codegen/testserver.Error" EmbeddedPointer: model: "github.com/99designs/gqlgen/codegen/testserver.EmbeddedPointerModel" + ThirdParty: + model: "github.com/99designs/gqlgen/codegen/testserver.ThirdParty" diff --git a/codegen/testserver/models-gen.go b/codegen/testserver/models-gen.go index 08034e49877..716b09bedea 100644 --- a/codegen/testserver/models-gen.go +++ b/codegen/testserver/models-gen.go @@ -25,6 +25,7 @@ type InputDirectives struct { Text string `json:"text"` Inner InnerDirectives `json:"inner"` InnerNullable *InnerDirectives `json:"innerNullable"` + ThirdParty *ThirdParty `json:"thirdParty"` } type Keywords struct { diff --git a/codegen/testserver/schema.graphql b/codegen/testserver/schema.graphql index 1247187a109..21da0e9b219 100644 --- a/codegen/testserver/schema.graphql +++ b/codegen/testserver/schema.graphql @@ -70,10 +70,13 @@ input OuterInput { inner: InnerInput! } +scalar ThirdParty + input InputDirectives { text: String! @length(min: 0, max: 7, message: "not valid") inner: InnerDirectives! innerNullable: InnerDirectives + thirdParty: ThirdParty @length(min: 0, max: 7, message: "not valid") } input InnerDirectives { diff --git a/codegen/testserver/thirdparty.go b/codegen/testserver/thirdparty.go new file mode 100644 index 00000000000..104548ce714 --- /dev/null +++ b/codegen/testserver/thirdparty.go @@ -0,0 +1,30 @@ +package testserver + +import ( + "fmt" + "github.com/99designs/gqlgen/graphql" + "io" + "strconv" +) + +type ThirdParty struct { + str string +} + +func MarshalThirdParty(tp ThirdParty) graphql.Marshaler { + return graphql.WriterFunc(func(w io.Writer) { + _, err := io.WriteString(w, strconv.Quote(tp.str)) + if err != nil { + panic(err) + } + }) +} + +func UnmarshalThirdParty(input interface{}) (ThirdParty, error) { + switch input := input.(type) { + case string: + return ThirdParty{str: input}, nil + default: + return ThirdParty{}, fmt.Errorf("unknown type for input: %s", input) + } +}