Skip to content

Commit

Permalink
codegen: allow binding methods with optional variadic arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Mar 23, 2022
1 parent fba5edd commit b079cff
Show file tree
Hide file tree
Showing 15 changed files with 501 additions and 6 deletions.
12 changes: 8 additions & 4 deletions codegen/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ func (b *builder) buildArg(obj *Object, arg *ast.ArgumentDefinition) (*FieldArgu
return &newArg, nil
}

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

func (b *builder) bindArgs(field *Field, sig *types.Signature, params *types.Tuple) ([]*FieldArgument, error) {
n := params.Len()
newArgs := make([]*FieldArgument, 0, len(field.Args))
// Accept variadic methods (i.e. have optional parameters).
if params.Len() > len(field.Args) && sig.Variadic(){
n = len(field.Args)
}
nextArg:
for j := 0; j < params.Len(); j++ {
for j := 0; j < n; j++ {
param := params.At(j)
for _, oldArg := range field.Args {
if strings.EqualFold(oldArg.Name, param.Name()) {
Expand Down
4 changes: 2 additions & 2 deletions codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ func (b *builder) bindField(obj *Object, f *Field) (errret error) {
params = types.NewTuple(vars...)
}

// Try to match target function's arguments with GraphQL field arguments
newArgs, err := b.bindArgs(f, params)
// Try to match target function's arguments with GraphQL field arguments.
newArgs, err := b.bindArgs(f, sig, params)
if err != nil {
return fmt.Errorf("%s:%d: %w", pos.Filename, pos.Line, err)
}
Expand Down
4 changes: 4 additions & 0 deletions codegen/testserver/followschema/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ func (r *queryResolver) ValidType(ctx context.Context) (*ValidType, error) {
panic("not implemented")
}

func (r *queryResolver) VariadicModel(ctx context.Context) (*VariadicModel, error) {
panic("not implemented")
}

func (r *queryResolver) WrappedStruct(ctx context.Context) (*WrappedStruct, error) {
panic("not implemented")
}
Expand Down
32 changes: 32 additions & 0 deletions codegen/testserver/followschema/root_.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions codegen/testserver/followschema/schema.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions codegen/testserver/followschema/stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

139 changes: 139 additions & 0 deletions codegen/testserver/followschema/variadic.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions codegen/testserver/followschema/variadic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package followschema

import (
"context"
"strconv"
)

type VariadicModel struct{}

type VariadicModelOption func(*VariadicModel)

func (v VariadicModel) Value(ctx context.Context, rank int, opts ...VariadicModelOption) (string, error) {
return strconv.Itoa(rank), nil
}
7 changes: 7 additions & 0 deletions codegen/testserver/followschema/variadic.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extend type Query {
variadicModel: VariadicModel
}

type VariadicModel {
value(rank: Int!): String
}
Loading

0 comments on commit b079cff

Please sign in to comment.