Skip to content

types: enum pointer values #220

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

Merged
merged 2 commits into from
Jul 23, 2017
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
18 changes: 14 additions & 4 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,17 +988,27 @@ func (gt *Enum) Values() []*EnumValueDefinition {
return gt.values
}
func (gt *Enum) Serialize(value interface{}) interface{} {
if enumValue, ok := gt.getValueLookup()[value]; ok {
v := value
if reflect.ValueOf(v).Kind() == reflect.Ptr {
v = reflect.Indirect(reflect.ValueOf(v)).Interface()
}
if enumValue, ok := gt.getValueLookup()[v]; ok {
return enumValue.Name
}
return nil
}
func (gt *Enum) ParseValue(value interface{}) interface{} {
valueStr, ok := value.(string)
if !ok {
var v string

switch value := value.(type) {
case string:
v = value
case *string:
v = *value
default:
return nil
}
if enumValue, ok := gt.getNameLookup()[valueStr]; ok {
if enumValue, ok := gt.getNameLookup()[v]; ok {
return enumValue.Value
}
return nil
Expand Down
44 changes: 44 additions & 0 deletions enum_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,47 @@ func TestTypeSystem_EnumValues_EnumValueMayBeNullable(t *testing.T) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}

func TestTypeSystem_EnumValues_EnumValueMayBePointer(t *testing.T) {
var enumTypeTestSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"query": &graphql.Field{
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "query",
Fields: graphql.Fields{
"color": &graphql.Field{
Type: enumTypeTestColorType,
},
"foo": &graphql.Field{
Description: "foo field",
Type: graphql.Int,
},
},
}),
Resolve: func(_ graphql.ResolveParams) (interface{}, error) {
one := 1
return struct {
Color *int `graphql:"color"`
Foo *int `graphql:"foo"`
}{&one, &one}, nil
},
},
},
}),
})
query := "{ query { color foo } }"
expected := &graphql.Result{
Data: map[string]interface{}{
"query": map[string]interface{}{
"color": "GREEN",
"foo": 1}}}
result := g(t, graphql.Params{
Schema: enumTypeTestSchema,
RequestString: query,
})
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}