Skip to content

Commit

Permalink
Move all build steps back into file containing defs
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Feb 4, 2019
1 parent 4e49d48 commit 44aabbd
Show file tree
Hide file tree
Showing 16 changed files with 694 additions and 700 deletions.
64 changes: 64 additions & 0 deletions codegen/args.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,74 @@
package codegen

import (
"fmt"
"go/types"
"strings"

"github.com/99designs/gqlgen/codegen/templates"
"github.com/pkg/errors"
"github.com/vektah/gqlparser/ast"
)

type ArgSet struct {
Args []*FieldArgument
FuncDecl string
}

func (b *builder) buildArg(obj *Object, arg *ast.ArgumentDefinition) (*FieldArgument, error) {
argDirs, err := b.getDirectives(arg.Directives)
if err != nil {
return nil, err
}
newArg := FieldArgument{
GQLName: arg.Name,
TypeReference: b.NamedTypes.getType(arg.Type),
Object: obj,
GoVarName: templates.ToGoPrivate(arg.Name),
Directives: argDirs,
}

if !newArg.TypeReference.Definition.GQLDefinition.IsInputType() {
return nil, errors.Errorf(
"cannot use %s as argument %s because %s is not a valid input type",
newArg.Definition.GQLDefinition.Name,
arg.Name,
newArg.TypeReference.Definition.GQLDefinition.Kind,
)
}

if arg.DefaultValue != nil {
newArg.Default, err = arg.DefaultValue.Value(nil)
if err != nil {
return nil, errors.Errorf("default value is not valid: %s", err.Error())
}
}

return &newArg, nil
}

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

nextArg:
for j := 0; j < params.Len(); j++ {
param := params.At(j)
for _, oldArg := range field.Args {
if strings.EqualFold(oldArg.GQLName, param.Name()) {
oldArg.TypeReference.GoType = param.Type()
newArgs = append(newArgs, oldArg)
continue nextArg
}
}

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

field.Args = newArgs
return nil
}

func (a *Data) Args() map[string][]*FieldArgument {
ret := map[string][]*FieldArgument{}
for _, o := range a.Objects {
Expand Down
Loading

0 comments on commit 44aabbd

Please sign in to comment.