Skip to content

Commit

Permalink
Fix directives on fields with custom scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
enjoylife committed Feb 10, 2019
1 parent 1b8b1ea commit 1d79b3e
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion codegen/input.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 46 additions & 0 deletions codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions codegen/testserver/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions codegen/testserver/models-gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions codegen/testserver/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions codegen/testserver/thirdparty.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 1d79b3e

Please sign in to comment.