Skip to content

Commit

Permalink
Add supports for snake case (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alif Rachmawadi authored and neelance committed Apr 6, 2017
1 parent 67e6f91 commit 01ab512
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 2 deletions.
116 changes: 116 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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{
{
Expand Down
5 changes: 4 additions & 1 deletion internal/exec/resolvable/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion internal/exec/resolvable/resolvable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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)
}

0 comments on commit 01ab512

Please sign in to comment.