github.com/galaxy-book/gqlgen@v0.7.2/codegen/directive.go (about)

     1  package codegen
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  type Directive struct {
    10  	Name string
    11  	Args []FieldArgument
    12  }
    13  
    14  func (d *Directive) ArgsFunc() string {
    15  	if len(d.Args) == 0 {
    16  		return ""
    17  	}
    18  
    19  	return "dir_" + d.Name + "_args"
    20  }
    21  
    22  func (d *Directive) CallArgs() string {
    23  	args := []string{"ctx", "obj", "n"}
    24  
    25  	for _, arg := range d.Args {
    26  		args = append(args, "args["+strconv.Quote(arg.GQLName)+"].("+arg.Signature()+")")
    27  	}
    28  
    29  	return strings.Join(args, ", ")
    30  }
    31  
    32  func (d *Directive) Declaration() string {
    33  	res := ucFirst(d.Name) + " func(ctx context.Context, obj interface{}, next graphql.Resolver"
    34  
    35  	for _, arg := range d.Args {
    36  		res += fmt.Sprintf(", %s %s", arg.GoVarName, arg.Signature())
    37  	}
    38  
    39  	res += ") (res interface{}, err error)"
    40  	return res
    41  }