Skip to content

Commit

Permalink
Merge pull request #105 from vektah/keyword-input-args
Browse files Browse the repository at this point in the history
Automatically add a _ suffix to reserved words
  • Loading branch information
vektah authored May 3, 2018
2 parents 26ac13f + 309e5c6 commit 2a437c2
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 7 deletions.
73 changes: 73 additions & 0 deletions codegen/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,76 @@ func TestComplexInputTypes(t *testing.T) {

require.NoError(t, err)
}

func TestKeywordInputFields(t *testing.T) {
err := generate("input_keywords_fields", `
input Object {
break: String!
default: String!
func: String!
interface: String!
select: String!
case: String!
defer: String!
go: String!
map: String!
struct: String!
chan: String!
else: String!
goto: String!
package: String!
switch: String!
const: String!
fallthrough: String!
if: String!
range: String!
type: String!
continue: String!
for: String!
import: String!
return: String!
var: String!
}
type Query {
test(input: Object): Boolean!
}
`)

require.NoError(t, err)
}

func TestInputKeywordArgs(t *testing.T) {
err := generate("input_keyword_args", `
type Query {
test(
break: String!,
default: String!,
func: String!,
interface: String!,
select: String!,
case: String!,
defer: String!,
go: String!,
map: String!,
struct: String!,
chan: String!,
else: String!,
goto: String!,
package: String!,
switch: String!,
const: String!,
fallthrough: String!,
if: String!,
range: String!,
type: String!,
continue: String!,
for: String!,
import: String!,
return: String!,
var: String!,
): Boolean!
}
`)

require.NoError(t, err)
}
9 changes: 5 additions & 4 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ type Field struct {
type FieldArgument struct {
*Type

GQLName string // The name of the argument in graphql
Object *Object // A link back to the parent object
Default interface{} // The default value
GQLName string // The name of the argument in graphql
GoVarName string // The name of the var in go
Object *Object // A link back to the parent object
Default interface{} // The default value
}

type Objects []*Object
Expand Down Expand Up @@ -67,7 +68,7 @@ func (f *Field) ResolverDeclaration() string {
res += fmt.Sprintf(", obj *%s", f.Object.FullName())
}
for _, arg := range f.Args {
res += fmt.Sprintf(", %s %s", arg.GQLName, arg.Signature())
res += fmt.Sprintf(", %s %s", arg.GoVarName, arg.Signature())
}

result := f.Signature()
Expand Down
44 changes: 41 additions & 3 deletions codegen/object_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,43 @@ func (cfg *Config) buildObjects(types NamedTypes, prog *loader.Program, imports
return objects, nil
}

var keywords = []string{
"break",
"default",
"func",
"interface",
"select",
"case",
"defer",
"go",
"map",
"struct",
"chan",
"else",
"goto",
"package",
"switch",
"const",
"fallthrough",
"if",
"range",
"type",
"continue",
"for",
"import",
"return",
"var",
}

func sanitizeGoName(name string) string {
for _, k := range keywords {
if name == k {
return name + "_"
}
}
return name
}

func (cfg *Config) buildObject(types NamedTypes, typ *schema.Object) (*Object, error) {
obj := &Object{NamedType: types[typ.TypeName()]}

Expand All @@ -53,9 +90,10 @@ func (cfg *Config) buildObject(types NamedTypes, typ *schema.Object) (*Object, e
var args []FieldArgument
for _, arg := range field.Args {
newArg := FieldArgument{
GQLName: arg.Name.Name,
Type: types.getType(arg.Type),
Object: obj,
GQLName: arg.Name.Name,
Type: types.getType(arg.Type),
Object: obj,
GoVarName: sanitizeGoName(arg.Name.Name),
}

if !newArg.Type.IsInput && !newArg.Type.IsScalar {
Expand Down

0 comments on commit 2a437c2

Please sign in to comment.