From de3d54cd1eb2d3499cbf2f381b545a8258c3c05f Mon Sep 17 00:00:00 2001 From: Alif Rachmawadi Date: Thu, 6 Apr 2017 15:39:16 +0700 Subject: [PATCH] Add supports for snake case (#77) --- graphql_test.go | 116 +++++++++++++++++++++++++ internal/exec/resolvable/packer.go | 5 +- internal/exec/resolvable/resolvable.go | 6 +- 3 files changed, 125 insertions(+), 2 deletions(-) diff --git a/graphql_test.go b/graphql_test.go index 24d42195bb3..8c1e1422833 100644 --- a/graphql_test.go +++ b/graphql_test.go @@ -22,6 +22,26 @@ func (r *helloWorldResolver2) Hello(ctx context.Context) (string, error) { return "Hello world!", nil } +type helloSnakeResolver1 struct{} + +func (r *helloSnakeResolver1) HelloHTML() string { + return "Hello snake!" +} + +func (r *helloSnakeResolver1) SayHello(args *struct{ FullName string }) string { + return "Hello " + args.FullName + "!" +} + +type helloSnakeResolver2 struct{} + +func (r *helloSnakeResolver2) HelloHTML(ctx context.Context) (string, error) { + return "Hello snake!", nil +} + +func (r *helloSnakeResolver2) SayHello(ctx context.Context, args *struct{ FullName string }) (string, error) { + return "Hello " + args.FullName + "!", nil +} + type theNumberResolver struct { number int32 } @@ -91,6 +111,102 @@ func TestHelloWorld(t *testing.T) { }) } +func TestHelloSnake(t *testing.T) { + gqltesting.RunTests(t, []*gqltesting.Test{ + { + Schema: graphql.MustParseSchema(` + schema { + query: Query + } + + type Query { + hello_html: String! + } + `, &helloSnakeResolver1{}), + Query: ` + { + hello_html + } + `, + ExpectedResult: ` + { + "hello_html": "Hello snake!" + } + `, + }, + + { + Schema: graphql.MustParseSchema(` + schema { + query: Query + } + + type Query { + hello_html: String! + } + `, &helloSnakeResolver2{}), + Query: ` + { + hello_html + } + `, + ExpectedResult: ` + { + "hello_html": "Hello snake!" + } + `, + }, + }) +} + +func TestHelloSnakeArguments(t *testing.T) { + gqltesting.RunTests(t, []*gqltesting.Test{ + { + Schema: graphql.MustParseSchema(` + schema { + query: Query + } + + type Query { + say_hello(full_name: String!): String! + } + `, &helloSnakeResolver1{}), + Query: ` + { + say_hello(full_name: "Rob Pike") + } + `, + ExpectedResult: ` + { + "say_hello": "Hello Rob Pike!" + } + `, + }, + + { + Schema: graphql.MustParseSchema(` + schema { + query: Query + } + + type Query { + say_hello(full_name: String!): String! + } + `, &helloSnakeResolver2{}), + Query: ` + { + say_hello(full_name: "Rob Pike") + } + `, + ExpectedResult: ` + { + "say_hello": "Hello Rob Pike!" + } + `, + }, + }) +} + func TestBasic(t *testing.T) { gqltesting.RunTests(t, []*gqltesting.Test{ { diff --git a/internal/exec/resolvable/packer.go b/internal/exec/resolvable/packer.go index 105d1894572..c21d672b081 100644 --- a/internal/exec/resolvable/packer.go +++ b/internal/exec/resolvable/packer.go @@ -130,8 +130,11 @@ func (b *execBuilder) makeStructPacker(values common.InputValueList, typ reflect var fields []*structPackerField for _, v := range values { fe := &structPackerField{field: v} + fx := func(n string) bool { + return strings.EqualFold(stripUnderscore(n), stripUnderscore(v.Name.Name)) + } - sf, ok := structType.FieldByNameFunc(func(n string) bool { return strings.EqualFold(n, v.Name.Name) }) + sf, ok := structType.FieldByNameFunc(fx) if !ok { return nil, fmt.Errorf("missing argument %q", v.Name) } diff --git a/internal/exec/resolvable/resolvable.go b/internal/exec/resolvable/resolvable.go index 63d1dfde78d..23298a93cf4 100644 --- a/internal/exec/resolvable/resolvable.go +++ b/internal/exec/resolvable/resolvable.go @@ -333,7 +333,7 @@ func (b *execBuilder) makeFieldExec(typeName string, f *schema.Field, m reflect. func findMethod(t reflect.Type, name string) int { for i := 0; i < t.NumMethod(); i++ { - if strings.EqualFold(name, t.Method(i).Name) { + if strings.EqualFold(stripUnderscore(name), stripUnderscore(t.Method(i).Name)) { return i } } @@ -346,3 +346,7 @@ func unwrapNonNull(t common.Type) (common.Type, bool) { } return t, false } + +func stripUnderscore(s string) string { + return strings.Replace(s, "_", "", -1) +}