add omitempty

This commit is contained in:
Ben Kraft
2021-04-09 11:51:30 -07:00
parent 25a2b45ccc
commit 16523c1f81
8 changed files with 246 additions and 14 deletions
+23 -7
View File
@@ -61,6 +61,7 @@ type argument struct {
GoName string
GoType string
GraphQLName string
Omitempty bool
}
func newGenerator(config *Config, schema *ast.Schema) *generator {
@@ -107,7 +108,21 @@ func (g *generator) Types() string {
return strings.Join(defs, "\n\n")
}
func (g *generator) getArgument(opName string, arg *ast.VariableDefinition) (argument, error) {
func (g *generator) getArgument(
opName string,
arg *ast.VariableDefinition,
operationDirective *GenqlientDirective,
) (argument, error) {
_, directive, err := g.parsePrecedingComment(arg, arg.Position)
if err != nil {
return argument{}, err
}
directive = operationDirective.merge(directive)
omitempty := false
if directive != nil {
omitempty = directive.Omitempty
}
graphQLName := arg.Variable
goType, err := g.getTypeForInputType(opName, arg.Type)
if err != nil {
@@ -117,6 +132,7 @@ func (g *generator) getArgument(opName string, arg *ast.VariableDefinition) (arg
GraphQLName: graphQLName,
GoName: lowerFirst(graphQLName),
GoType: goType,
Omitempty: omitempty,
}, nil
}
@@ -133,10 +149,15 @@ func (g *generator) addOperation(op *ast.OperationDefinition) error {
// TODO: handle fragments
})
commentLines, directive, err := g.parsePrecedingComment(op, op.Position)
if err != nil {
return err
}
args := make([]argument, len(op.VariableDefinitions))
for i, arg := range op.VariableDefinitions {
var err error
args[i], err = g.getArgument(op.Name, arg)
args[i], err = g.getArgument(op.Name, arg, directive)
if err != nil {
return err
}
@@ -147,11 +168,6 @@ func (g *generator) addOperation(op *ast.OperationDefinition) error {
return err
}
commentLines, _, err := g.parsePrecedingComment(op.Position)
if err != nil {
return err
}
var docComment string
if len(commentLines) > 0 {
docComment = "// " + strings.ReplaceAll(commentLines, "\n", "\n// ")