forked from arsmn/fastgql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request 99designs#239 from vektah/directive-args
Directive args
- Loading branch information
Showing
21 changed files
with
268 additions
and
148 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,33 @@ | ||
package codegen | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type Directive struct { | ||
Name string | ||
Args []FieldArgument | ||
} | ||
|
||
func (d *Directive) CallArgs() string { | ||
args := []string{"ctx", "n"} | ||
|
||
for _, arg := range d.Args { | ||
args = append(args, "args["+strconv.Quote(arg.GQLName)+"].("+arg.Signature()+")") | ||
} | ||
|
||
return strings.Join(args, ", ") | ||
} | ||
|
||
func (d *Directive) Declaration() string { | ||
res := ucFirst(d.Name) + " func(ctx context.Context, next graphql.Resolver" | ||
|
||
for _, arg := range d.Args { | ||
res += fmt.Sprintf(", %s %s", arg.GoVarName, arg.Signature()) | ||
} | ||
|
||
res += ") (res interface{}, err error)" | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,42 @@ | ||
package codegen | ||
|
||
func (cfg *Config) buildDirectives() (directives []*Directive) { | ||
for name := range cfg.schema.Directives { | ||
import "github.com/pkg/errors" | ||
|
||
func (cfg *Config) buildDirectives(types NamedTypes) ([]*Directive, error) { | ||
var directives []*Directive | ||
|
||
for name, dir := range cfg.schema.Directives { | ||
if name == "skip" || name == "include" || name == "deprecated" { | ||
continue | ||
} | ||
|
||
var args []FieldArgument | ||
for _, arg := range dir.Arguments { | ||
newArg := FieldArgument{ | ||
GQLName: arg.Name, | ||
Type: types.getType(arg.Type), | ||
GoVarName: sanitizeGoName(arg.Name), | ||
} | ||
|
||
if !newArg.Type.IsInput && !newArg.Type.IsScalar { | ||
return nil, errors.Errorf("%s cannot be used as argument of directive %s(%s) only input and scalar types are allowed", arg.Type, dir.Name, arg.Name) | ||
} | ||
|
||
if arg.DefaultValue != nil { | ||
var err error | ||
newArg.Default, err = arg.DefaultValue.Value(nil) | ||
if err != nil { | ||
return nil, errors.Errorf("default value for directive argument %s(%s) is not valid: %s", dir.Name, arg.Name, err.Error()) | ||
} | ||
newArg.StripPtr() | ||
} | ||
args = append(args, newArg) | ||
} | ||
|
||
directives = append(directives, &Directive{ | ||
Name: name, | ||
Args: args, | ||
}) | ||
} | ||
return directives | ||
return directives, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,17 @@ | ||
{{- if . }}args := map[string]interface{}{} {{end}} | ||
{{- range $i, $arg := . }} | ||
var arg{{$i}} {{$arg.Signature }} | ||
if tmp, ok := field.Args[{{$arg.GQLName|quote}}]; ok { | ||
if tmp, ok := rawArgs[{{$arg.GQLName|quote}}]; ok { | ||
var err error | ||
{{$arg.Unmarshal (print "arg" $i) "tmp" }} | ||
if err != nil { | ||
ec.Error(ctx, err) | ||
{{- if $arg.Object.Stream }} | ||
return nil | ||
{{- else }} | ||
return graphql.Null | ||
{{- end }} | ||
} | ||
} {{ if $arg.Default }} else { | ||
var tmp interface{} = {{ $arg.Default | dump }} | ||
var err error | ||
{{$arg.Unmarshal (print "arg" $i) "tmp" }} | ||
if err != nil { | ||
ec.Error(ctx, err) | ||
{{- if $arg.Object.Stream }} | ||
{{- if $arg.Stream }} | ||
return nil | ||
{{- else }} | ||
return graphql.Null | ||
{{- end }} | ||
} | ||
} | ||
{{end }} | ||
args[{{$arg.GQLName|quote}}] = arg{{$i}} | ||
{{- end -}} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.