Skip to content

Commit

Permalink
Reintroduce special cast case for string enums
Browse files Browse the repository at this point in the history
This reverts commit 8996066.
  • Loading branch information
lwc committed Aug 13, 2020
1 parent 8561c05 commit c56848e
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 32 deletions.
41 changes: 36 additions & 5 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (b *Binder) PointerTo(ref *TypeReference) *TypeReference {
newRef := &TypeReference{
GO: types.NewPointer(ref.GO),
GQL: ref.GQL,
CastType: ref.CastType,
Definition: ref.Definition,
Unmarshaler: ref.Unmarshaler,
Marshaler: ref.Marshaler,
Expand All @@ -167,6 +168,7 @@ type TypeReference struct {
GQL *ast.Type
GO types.Type
Target types.Type
CastType types.Type // Before calling marshalling functions cast from/to this base type
Marshaler *types.Func // When using external marshalling functions this will point to the Marshal function
Unmarshaler *types.Func // When using external marshalling functions this will point to the Unmarshal function
IsMarshaler bool // Does the type implement graphql.Marshaler and graphql.Unmarshaler
Expand All @@ -178,6 +180,7 @@ func (ref *TypeReference) Elem() *TypeReference {
GO: p.Elem(),
Target: ref.Target,
GQL: ref.GQL,
CastType: ref.CastType,
Definition: ref.Definition,
Unmarshaler: ref.Unmarshaler,
Marshaler: ref.Marshaler,
Expand All @@ -190,6 +193,7 @@ func (ref *TypeReference) Elem() *TypeReference {
GO: ref.GO.(*types.Slice).Elem(),
Target: ref.Target,
GQL: ref.GQL.Elem,
CastType: ref.CastType,
Definition: ref.Definition,
Unmarshaler: ref.Unmarshaler,
Marshaler: ref.Marshaler,
Expand Down Expand Up @@ -345,16 +349,27 @@ func (b *Binder) TypeReference(schemaType *ast.Type, bindTarget types.Type) (ret
return nil, err
}

fun, isFunc := obj.(*types.Func)
switch {
case isFunc:
if fun, isFunc := obj.(*types.Func); isFunc {
ref.GO = fun.Type().(*types.Signature).Params().At(0).Type()
ref.Marshaler = fun
ref.Unmarshaler = types.NewFunc(0, fun.Pkg(), "Unmarshal"+typeName, nil)
case hasMethod(obj.Type(), "MarshalGQL") && hasMethod(obj.Type(), "UnmarshalGQL"):
} else if hasMethod(obj.Type(), "MarshalGQL") && hasMethod(obj.Type(), "UnmarshalGQL") {
ref.GO = obj.Type()
ref.IsMarshaler = true
default:
} else if underlying := basicUnderlying(obj.Type()); def.IsLeafType() && underlying != nil && underlying.Kind() == types.String {
// Special case for named types wrapping strings. Used by default enum implementations.

ref.GO = obj.Type()
ref.CastType = underlying

underlyingRef, err := b.TypeReference(&ast.Type{NamedType: "String"}, nil)
if err != nil {
return nil, err
}

ref.Marshaler = underlyingRef.Marshaler
ref.Unmarshaler = underlyingRef.Unmarshaler
} else {
ref.GO = obj.Type()
}

Expand Down Expand Up @@ -431,3 +446,19 @@ func hasMethod(it types.Type, name string) bool {
}
return false
}

func basicUnderlying(it types.Type) *types.Basic {
if ptr, isPtr := it.(*types.Pointer); isPtr {
it = ptr.Elem()
}
namedType, ok := it.(*types.Named)
if !ok {
return nil
}

if basic, ok := namedType.Underlying().(*types.Basic); ok {
return basic
}

return nil
}
119 changes: 116 additions & 3 deletions codegen/testserver/generated.go

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

4 changes: 4 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ func (r *queryResolver) ScalarSlice(ctx context.Context) ([]byte, error) {
panic("not implemented")
}

func (r *queryResolver) Fallback(ctx context.Context, arg FallbackToStringEncoding) (FallbackToStringEncoding, error) {
panic("not implemented")
}

func (r *queryResolver) OptionalUnion(ctx context.Context) (TestUnion, error) {
panic("not implemented")
}
Expand Down
4 changes: 4 additions & 0 deletions codegen/testserver/stub.go

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

9 changes: 9 additions & 0 deletions codegen/testserver/typefallback.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extend type Query {
fallback(arg: FallbackToStringEncoding!): FallbackToStringEncoding!
}

enum FallbackToStringEncoding {
A
B
C
}
28 changes: 28 additions & 0 deletions codegen/testserver/typefallback_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package testserver

import (
"context"
"testing"

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/stretchr/testify/require"
)

func TestTypeFallback(t *testing.T) {
resolvers := &Stub{}

c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers})))

resolvers.QueryResolver.Fallback = func(ctx context.Context, arg FallbackToStringEncoding) (FallbackToStringEncoding, error) {
return arg, nil
}

t.Run("fallback to string passthrough", func(t *testing.T) {
var resp struct {
Fallback string
}
c.MustPost(`query { fallback(arg: A) }`, &resp)
require.Equal(t, "A", resp.Fallback)
})
}
22 changes: 1 addition & 21 deletions codegen/testserver/wrapped_type.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
package testserver

import (
"fmt"
"io"
"strconv"

"github.com/99designs/gqlgen/codegen/testserver/otherpkg"
"github.com/99designs/gqlgen/graphql"
)
import "github.com/99designs/gqlgen/codegen/testserver/otherpkg"

type WrappedScalar otherpkg.Scalar
type WrappedStruct otherpkg.Struct
type WrappedMap otherpkg.Map
type WrappedSlice otherpkg.Slice

func (e *WrappedScalar) UnmarshalGQL(v interface{}) error {
s, err := graphql.UnmarshalString(v)
if err != nil {
return err
}
*e = WrappedScalar(s)
return nil
}

func (e WrappedScalar) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(string(e)))
}
Loading

0 comments on commit c56848e

Please sign in to comment.