Skip to content

Commit

Permalink
Don't loose field arguments when none match (#1725)
Browse files Browse the repository at this point in the history
  • Loading branch information
edigaryev authored Nov 22, 2021
1 parent 213ecd9 commit 4581fcc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 4 additions & 5 deletions codegen/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (b *builder) buildArg(obj *Object, arg *ast.ArgumentDefinition) (*FieldArgu
return &newArg, nil
}

func (b *builder) bindArgs(field *Field, params *types.Tuple) error {
func (b *builder) bindArgs(field *Field, params *types.Tuple) ([]*FieldArgument, error) {
var newArgs []*FieldArgument

nextArg:
Expand All @@ -83,7 +83,7 @@ nextArg:
if strings.EqualFold(oldArg.Name, param.Name()) {
tr, err := b.Binder.TypeReference(oldArg.Type, param.Type())
if err != nil {
return err
return nil, err
}
oldArg.TypeReference = tr

Expand All @@ -93,11 +93,10 @@ nextArg:
}

// no matching arg found, abort
return fmt.Errorf("arg %s not in schema", param.Name())
return nil, fmt.Errorf("arg %s not in schema", param.Name())
}

field.Args = newArgs
return nil
return newArgs, nil
}

func (a *Data) Args() map[string][]*FieldArgument {
Expand Down
6 changes: 5 additions & 1 deletion codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,13 @@ func (b *builder) bindField(obj *Object, f *Field) (errret error) {
params = types.NewTuple(vars...)
}

if err = b.bindArgs(f, params); err != nil {
// Try to match target function's arguments with GraphQL field arguments
newArgs, err := b.bindArgs(f, params)
if err != nil {
return fmt.Errorf("%s:%d: %w", pos.Filename, pos.Line, err)
}

// Try to match target function's return types with GraphQL field return type
result := sig.Results().At(0)
tr, err := b.Binder.TypeReference(f.Type, result.Type())
if err != nil {
Expand All @@ -193,6 +196,7 @@ func (b *builder) bindField(obj *Object, f *Field) (errret error) {
f.GoFieldType = GoFieldMethod
f.GoReceiverName = "obj"
f.GoFieldName = target.Name()
f.Args = newArgs
f.TypeReference = tr

return nil
Expand Down

0 comments on commit 4581fcc

Please sign in to comment.