Skip to content

Commit

Permalink
accept an optional ctx parameter on model methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gracenoah committed Nov 13, 2018
1 parent 02a1935 commit 4975512
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
25 changes: 15 additions & 10 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ type Object struct {

type Field struct {
*Type
Description string // Description of a field
GQLName string // The name of the field in graphql
GoFieldType GoFieldType // The field type in go, if any
GoReceiverName string // The name of method & var receiver in go, if any
GoFieldName string // The name of the method or var in go, if any
Args []FieldArgument // A list of arguments to be passed to this field
ForceResolver bool // Should be emit Resolver method
NoErr bool // If this is bound to a go method, does that method have an error as the second argument
Object *Object // A link back to the parent object
Default interface{} // The default value
Description string // Description of a field
GQLName string // The name of the field in graphql
GoFieldType GoFieldType // The field type in go, if any
GoReceiverName string // The name of method & var receiver in go, if any
GoFieldName string // The name of the method or var in go, if any
Args []FieldArgument // A list of arguments to be passed to this field
ForceResolver bool // Should be emit Resolver method
MethodHasContext bool // If this is bound to a go method, does the method also take a context
NoErr bool // If this is bound to a go method, does that method have an error as the second argument
Object *Object // A link back to the parent object
Default interface{} // The default value
}

type FieldArgument struct {
Expand Down Expand Up @@ -209,6 +210,10 @@ func (f *Field) CallArgs() string {
if !f.Object.Root {
args = append(args, "obj")
}
} else {
if f.MethodHasContext {
args = append(args, "ctx")
}
}

for _, arg := range f.Args {
Expand Down
14 changes: 13 additions & 1 deletion codegen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,19 @@ func bindMethod(imports *Imports, t types.Type, field *Field) error {
} else if sig.Results().Len() != 2 {
return fmt.Errorf("method has wrong number of args")
}
newArgs, err := matchArgs(field, sig.Params())
params := sig.Params()
// If the first argument is the context, remove it from the comparison and set
// the MethodHasContext flag so that the context will be passed to this model's method
if params.Len() > 0 && params.At(0).Type().String() == "context.Context" {
field.MethodHasContext = true
vars := make([]*types.Var, params.Len()-1)
for i := 1; i < params.Len(); i++ {
vars[i-1] = params.At(i)
}
params = types.NewTuple(vars...)
}

newArgs, err := matchArgs(field, params)
if err != nil {
return err
}
Expand Down

0 comments on commit 4975512

Please sign in to comment.