Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix enum resolution #218

Merged
merged 6 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 99 additions & 24 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package graphql_test
import (
"context"
"errors"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -1606,13 +1607,50 @@ func TestUnexportedField(t *testing.T) {
}
}

type Enum string
type StringEnum string

const (
EnumOption1 Enum = "Option1"
EnumOption2 Enum = "Option2"
EnumOption1 StringEnum = "Option1"
EnumOption2 StringEnum = "Option2"
)

type IntEnum int

const (
IntEnum0 IntEnum = iota
IntEnum1
)

func (e IntEnum) String() string {
switch int(e) {
case 0:
return "Int0"
case 1:
return "Int1"
default:
return "IntN"
}
}

func (IntEnum) ImplementsGraphQLType(name string) bool {
return name == "IntEnum"
}

func (e *IntEnum) UnmarshalGraphQL(input interface{}) error {
if str, ok := input.(string); ok {
switch str {
case "Int0":
*e = IntEnum(0)
case "Int1":
*e = IntEnum(1)
default:
*e = IntEnum(-1)
}
return nil
}
return fmt.Errorf("wrong type for IntEnum: %T", input)
}

type inputResolver struct{}

func (r *inputResolver) Int(args struct{ Value int32 }) int32 {
Expand Down Expand Up @@ -1656,19 +1694,35 @@ func (r *inputResolver) NullableList(args struct{ Value *[]*struct{ V int32 } })
return &l
}

func (r *inputResolver) EnumString(args struct{ Value string }) string {
func (r *inputResolver) StringEnumValue(args struct{ Value string }) string {
return args.Value
}

func (r *inputResolver) NullableEnumString(args struct{ Value *string }) *string {
func (r *inputResolver) NullableStringEnumValue(args struct{ Value *string }) *string {
return args.Value
}

func (r *inputResolver) Enum(args struct{ Value Enum }) Enum {
func (r *inputResolver) StringEnum(args struct{ Value StringEnum }) StringEnum {
return args.Value
}

func (r *inputResolver) NullableEnum(args struct{ Value *Enum }) *Enum {
func (r *inputResolver) NullableStringEnum(args struct{ Value *StringEnum }) *StringEnum {
return args.Value
}

func (r *inputResolver) IntEnumValue(args struct{ Value string }) string {
return args.Value
}

func (r *inputResolver) NullableIntEnumValue(args struct{ Value *string }) *string {
return args.Value
}

func (r *inputResolver) IntEnum(args struct{ Value IntEnum }) IntEnum {
return args.Value
}

func (r *inputResolver) NullableIntEnum(args struct{ Value *IntEnum }) *IntEnum {
return args.Value
}

Expand Down Expand Up @@ -1704,10 +1758,14 @@ func TestInput(t *testing.T) {
nullable(value: Int): Int
list(value: [Input!]!): [Int!]!
nullableList(value: [Input]): [Int]
enumString(value: Enum!): Enum!
nullableEnumString(value: Enum): Enum
enum(value: Enum!): Enum!
nullableEnum(value: Enum): Enum
stringEnumValue(value: StringEnum!): StringEnum!
nullableStringEnumValue(value: StringEnum): StringEnum
stringEnum(value: StringEnum!): StringEnum!
nullableStringEnum(value: StringEnum): StringEnum
intEnumValue(value: IntEnum!): IntEnum!
nullableIntEnumValue(value: IntEnum): IntEnum
intEnum(value: IntEnum!): IntEnum!
nullableIntEnum(value: IntEnum): IntEnum
recursive(value: RecursiveInput!): Int!
id(value: ID!): ID!
}
Expand All @@ -1720,10 +1778,15 @@ func TestInput(t *testing.T) {
next: RecursiveInput
}

enum Enum {
enum StringEnum {
Option1
Option2
}

enum IntEnum {
Int0
Int1
}
`, &inputResolver{})
gqltesting.RunTests(t, []*gqltesting.Test{
{
Expand All @@ -1741,12 +1804,18 @@ func TestInput(t *testing.T) {
list2: list(value: {v: 42})
nullableList1: nullableList(value: [{v: 41}, null, {v: 43}])
nullableList2: nullableList(value: null)
enumString(value: Option1)
nullableEnumString1: nullableEnum(value: Option1)
nullableEnumString2: nullableEnum(value: null)
enum(value: Option2)
nullableEnum1: nullableEnum(value: Option2)
nullableEnum2: nullableEnum(value: null)
stringEnumValue(value: Option1)
nullableStringEnumValue1: nullableStringEnum(value: Option1)
nullableStringEnumValue2: nullableStringEnum(value: null)
stringEnum(value: Option2)
nullableStringEnum1: nullableStringEnum(value: Option2)
nullableStringEnum2: nullableStringEnum(value: null)
intEnumValue(value: Int1)
nullableIntEnumValue1: nullableIntEnumValue(value: Int1)
nullableIntEnumValue2: nullableIntEnumValue(value: null)
intEnum(value: Int1)
nullableIntEnum1: nullableIntEnum(value: Int1)
nullableIntEnum2: nullableIntEnum(value: null)
recursive(value: {next: {next: {}}})
intID: id(value: 1234)
strID: id(value: "1234")
Expand All @@ -1765,12 +1834,18 @@ func TestInput(t *testing.T) {
"list2": [42],
"nullableList1": [41, null, 43],
"nullableList2": null,
"enumString": "Option1",
"nullableEnumString1": "Option1",
"nullableEnumString2": null,
"enum": "Option2",
"nullableEnum1": "Option2",
"nullableEnum2": null,
"stringEnumValue": "Option1",
"nullableStringEnumValue1": "Option1",
"nullableStringEnumValue2": null,
"stringEnum": "Option2",
"nullableStringEnum1": "Option2",
"nullableStringEnum2": null,
"intEnumValue": "Int1",
"nullableIntEnumValue1": "Int1",
"nullableIntEnumValue2": null,
"intEnum": "Int1",
"nullableIntEnum1": "Int1",
"nullableIntEnum2": null,
"recursive": 3,
"intID": "1234",
"strID": "1234"
Expand Down
7 changes: 6 additions & 1 deletion internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"reflect"
"sync"

Expand Down Expand Up @@ -276,8 +277,12 @@ func (r *Request) execSelectionSet(ctx context.Context, sels []selected.Selectio
out.Write(data)

case *schema.Enum:
var stringer fmt.Stringer = resolver
if s, ok := resolver.Interface().(fmt.Stringer); ok {
stringer = s
}
out.WriteByte('"')
out.WriteString(resolver.String())
out.WriteString(stringer.String())
out.WriteByte('"')

default:
Expand Down