You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
schema {
query: Query
mutation: Mutation
}
type Query {
project(id: ID!): Project
}
type Mutation {
updateProject(name: String!, metadata: [String]): Project
}
type Project {
id: ID
name: String
metadata: [String]
}
When I invoke command gqlgen -out generated.go with empty types.json {}
I will get the result:
var arg1 []string
if tmp, ok := field.Args["metadata"]; ok {
tmp2, err := coerceString(tmp)
if err != nil {
ec.Error(err)
continue
}
arg1 = tmp2 //this is wrong
}
The full code is here
// This file was generated by github.com/vektah/gqlgen, DO NOT EDIT
package graphql1
import (
context "context"
fmt "fmt"
io "io"
reflect "reflect"
strconv "strconv"
strings "strings"
sync "sync"
time "time"
mapstructure "github.com/mitchellh/mapstructure"
jsonw "github.com/vektah/gqlgen/jsonw"
errors "github.com/vektah/gqlgen/neelance/errors"
introspection "github.com/vektah/gqlgen/neelance/introspection"
query "github.com/vektah/gqlgen/neelance/query"
schema "github.com/vektah/gqlgen/neelance/schema"
validation "github.com/vektah/gqlgen/neelance/validation"
)
type Resolvers interface {
Mutation_updateProject(ctx context.Context, name string, metadata []string) (*interface{}, error)
Project_id(ctx context.Context, it *interface{}) (*string, error)
Project_name(ctx context.Context, it *interface{}) (*string, error)
Project_metadata(ctx context.Context, it *interface{}) ([]string, error)
Query_project(ctx context.Context, id string) (*interface{}, error)
}
func NewExecutor(resolvers Resolvers) func(context.Context, string, string, map[string]interface{}, io.Writer) []*errors.QueryError {
return func(ctx context.Context, document string, operationName string, variables map[string]interface{}, w io.Writer) []*errors.QueryError {
doc, qErr := query.Parse(document)
if qErr != nil {
return []*errors.QueryError{qErr}
}
errs := validation.Validate(parsedSchema, doc)
if len(errs) != 0 {
return errs
}
op, err := doc.GetOperation(operationName)
if err != nil {
return []*errors.QueryError{errors.Errorf("%s", err)}
}
c := executionContext{
resolvers: resolvers,
variables: variables,
doc: doc,
ctx: ctx,
}
var data jsonw.Writer
if op.Type == query.Query {
data = c._query(op.Selections, nil)
} else if op.Type == query.Mutation {
data = c._mutation(op.Selections, nil)
} else {
return []*errors.QueryError{errors.Errorf("unsupported operation type")}
}
c.wg.Wait()
result := &jsonw.OrderedMap{}
result.Add("data", data)
if len(c.Errors) > 0 {
result.Add("errors", errors.ErrorWriter(c.Errors))
}
result.WriteJson(w)
return nil
}
}
type executionContext struct {
errors.Builder
resolvers Resolvers
variables map[string]interface{}
doc *query.Document
ctx context.Context
wg sync.WaitGroup
}
var mutationImplementors = []string{"Mutation"}
// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) _mutation(sel []query.Selection, it *interface{}) jsonw.Writer {
fields := ec.collectFields(sel, mutationImplementors, map[string]bool{})
out := jsonw.NewOrderedMap(len(fields))
for i, field := range fields {
out.Keys[i] = field.Alias
out.Values[i] = jsonw.Null
switch field.Name {
case "updateProject":
var arg0 string
if tmp, ok := field.Args["name"]; ok {
tmp2, err := coerceString(tmp)
if err != nil {
ec.Error(err)
continue
}
arg0 = tmp2
}
var arg1 []string
if tmp, ok := field.Args["metadata"]; ok {
tmp2, err := coerceString(tmp)
if err != nil {
ec.Error(err)
continue
}
arg1 = tmp2
}
res, err := ec.resolvers.Mutation_updateProject(ec.ctx, arg0, arg1)
if err != nil {
ec.Error(err)
continue
}
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = ec._project(field.Selections, res)
}
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}
return out
}
var projectImplementors = []string{"Project"}
// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) _project(sel []query.Selection, it *interface{}) jsonw.Writer {
fields := ec.collectFields(sel, projectImplementors, map[string]bool{})
out := jsonw.NewOrderedMap(len(fields))
for i, field := range fields {
out.Keys[i] = field.Alias
out.Values[i] = jsonw.Null
switch field.Name {
case "id":
ec.wg.Add(1)
go func(i int, field collectedField) {
defer ec.wg.Done()
res, err := ec.resolvers.Project_id(ec.ctx, it)
if err != nil {
ec.Error(err)
return
}
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = jsonw.String(*res)
}
}(i, field)
case "name":
ec.wg.Add(1)
go func(i int, field collectedField) {
defer ec.wg.Done()
res, err := ec.resolvers.Project_name(ec.ctx, it)
if err != nil {
ec.Error(err)
return
}
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = jsonw.String(*res)
}
}(i, field)
case "metadata":
ec.wg.Add(1)
go func(i int, field collectedField) {
defer ec.wg.Done()
res, err := ec.resolvers.Project_metadata(ec.ctx, it)
if err != nil {
ec.Error(err)
return
}
arr1 := jsonw.Array{}
for idx1 := range res {
var tmp1 jsonw.Writer
tmp1 = jsonw.String(res[idx1])
arr1 = append(arr1, tmp1)
}
out.Values[i] = arr1
}(i, field)
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}
return out
}
var queryImplementors = []string{"Query"}
// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) _query(sel []query.Selection, it *interface{}) jsonw.Writer {
fields := ec.collectFields(sel, queryImplementors, map[string]bool{})
out := jsonw.NewOrderedMap(len(fields))
for i, field := range fields {
out.Keys[i] = field.Alias
out.Values[i] = jsonw.Null
switch field.Name {
case "project":
var arg0 string
if tmp, ok := field.Args["id"]; ok {
tmp2, err := coerceString(tmp)
if err != nil {
ec.Error(err)
continue
}
arg0 = tmp2
}
ec.wg.Add(1)
go func(i int, field collectedField) {
defer ec.wg.Done()
res, err := ec.resolvers.Query_project(ec.ctx, arg0)
if err != nil {
ec.Error(err)
return
}
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = ec._project(field.Selections, res)
}
}(i, field)
case "__schema":
res := ec.introspectSchema()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = ec.___Schema(field.Selections, res)
}
case "__type":
var arg0 string
if tmp, ok := field.Args["name"]; ok {
tmp2, err := coerceString(tmp)
if err != nil {
ec.Error(err)
continue
}
arg0 = tmp2
}
res := ec.introspectType(arg0)
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = ec.___Type(field.Selections, res)
}
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}
return out
}
var __DirectiveImplementors = []string{"__Directive"}
// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) ___Directive(sel []query.Selection, it *introspection.Directive) jsonw.Writer {
fields := ec.collectFields(sel, __DirectiveImplementors, map[string]bool{})
out := jsonw.NewOrderedMap(len(fields))
for i, field := range fields {
out.Keys[i] = field.Alias
out.Values[i] = jsonw.Null
switch field.Name {
case "name":
res := it.Name()
out.Values[i] = jsonw.String(res)
case "description":
res := it.Description()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = jsonw.String(*res)
}
case "locations":
res := it.Locations()
arr1 := jsonw.Array{}
for idx1 := range res {
var tmp1 jsonw.Writer
tmp1 = jsonw.String(res[idx1])
arr1 = append(arr1, tmp1)
}
out.Values[i] = arr1
case "args":
res := it.Args()
arr1 := jsonw.Array{}
for idx1 := range res {
var tmp1 jsonw.Writer
if res[idx1] == nil {
tmp1 = jsonw.Null
} else {
tmp1 = ec.___InputValue(field.Selections, res[idx1])
}
arr1 = append(arr1, tmp1)
}
out.Values[i] = arr1
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}
return out
}
var __EnumValueImplementors = []string{"__EnumValue"}
// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) ___EnumValue(sel []query.Selection, it *introspection.EnumValue) jsonw.Writer {
fields := ec.collectFields(sel, __EnumValueImplementors, map[string]bool{})
out := jsonw.NewOrderedMap(len(fields))
for i, field := range fields {
out.Keys[i] = field.Alias
out.Values[i] = jsonw.Null
switch field.Name {
case "name":
res := it.Name()
out.Values[i] = jsonw.String(res)
case "description":
res := it.Description()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = jsonw.String(*res)
}
case "isDeprecated":
res := it.IsDeprecated()
out.Values[i] = jsonw.Bool(res)
case "deprecationReason":
res := it.DeprecationReason()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = jsonw.String(*res)
}
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}
return out
}
var __FieldImplementors = []string{"__Field"}
// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) ___Field(sel []query.Selection, it *introspection.Field) jsonw.Writer {
fields := ec.collectFields(sel, __FieldImplementors, map[string]bool{})
out := jsonw.NewOrderedMap(len(fields))
for i, field := range fields {
out.Keys[i] = field.Alias
out.Values[i] = jsonw.Null
switch field.Name {
case "name":
res := it.Name()
out.Values[i] = jsonw.String(res)
case "description":
res := it.Description()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = jsonw.String(*res)
}
case "args":
res := it.Args()
arr1 := jsonw.Array{}
for idx1 := range res {
var tmp1 jsonw.Writer
if res[idx1] == nil {
tmp1 = jsonw.Null
} else {
tmp1 = ec.___InputValue(field.Selections, res[idx1])
}
arr1 = append(arr1, tmp1)
}
out.Values[i] = arr1
case "type":
res := it.Type()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = ec.___Type(field.Selections, res)
}
case "isDeprecated":
res := it.IsDeprecated()
out.Values[i] = jsonw.Bool(res)
case "deprecationReason":
res := it.DeprecationReason()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = jsonw.String(*res)
}
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}
return out
}
var __InputValueImplementors = []string{"__InputValue"}
// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) ___InputValue(sel []query.Selection, it *introspection.InputValue) jsonw.Writer {
fields := ec.collectFields(sel, __InputValueImplementors, map[string]bool{})
out := jsonw.NewOrderedMap(len(fields))
for i, field := range fields {
out.Keys[i] = field.Alias
out.Values[i] = jsonw.Null
switch field.Name {
case "name":
res := it.Name()
out.Values[i] = jsonw.String(res)
case "description":
res := it.Description()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = jsonw.String(*res)
}
case "type":
res := it.Type()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = ec.___Type(field.Selections, res)
}
case "defaultValue":
res := it.DefaultValue()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = jsonw.String(*res)
}
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}
return out
}
var __SchemaImplementors = []string{"__Schema"}
// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) ___Schema(sel []query.Selection, it *introspection.Schema) jsonw.Writer {
fields := ec.collectFields(sel, __SchemaImplementors, map[string]bool{})
out := jsonw.NewOrderedMap(len(fields))
for i, field := range fields {
out.Keys[i] = field.Alias
out.Values[i] = jsonw.Null
switch field.Name {
case "types":
res := it.Types()
arr1 := jsonw.Array{}
for idx1 := range res {
var tmp1 jsonw.Writer
if res[idx1] == nil {
tmp1 = jsonw.Null
} else {
tmp1 = ec.___Type(field.Selections, res[idx1])
}
arr1 = append(arr1, tmp1)
}
out.Values[i] = arr1
case "queryType":
res := it.QueryType()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = ec.___Type(field.Selections, res)
}
case "mutationType":
res := it.MutationType()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = ec.___Type(field.Selections, res)
}
case "subscriptionType":
res := it.SubscriptionType()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = ec.___Type(field.Selections, res)
}
case "directives":
res := it.Directives()
arr1 := jsonw.Array{}
for idx1 := range res {
var tmp1 jsonw.Writer
if res[idx1] == nil {
tmp1 = jsonw.Null
} else {
tmp1 = ec.___Directive(field.Selections, res[idx1])
}
arr1 = append(arr1, tmp1)
}
out.Values[i] = arr1
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}
return out
}
var __TypeImplementors = []string{"__Type"}
// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Type) jsonw.Writer {
fields := ec.collectFields(sel, __TypeImplementors, map[string]bool{})
out := jsonw.NewOrderedMap(len(fields))
for i, field := range fields {
out.Keys[i] = field.Alias
out.Values[i] = jsonw.Null
switch field.Name {
case "kind":
res := it.Kind()
out.Values[i] = jsonw.String(res)
case "name":
res := it.Name()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = jsonw.String(*res)
}
case "description":
res := it.Description()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = jsonw.String(*res)
}
case "fields":
var arg0 bool
if tmp, ok := field.Args["includeDeprecated"]; ok {
tmp2, err := coerceBool(tmp)
if err != nil {
ec.Error(err)
continue
}
arg0 = tmp2
}
res := it.Fields(arg0)
arr1 := jsonw.Array{}
for idx1 := range res {
var tmp1 jsonw.Writer
if res[idx1] == nil {
tmp1 = jsonw.Null
} else {
tmp1 = ec.___Field(field.Selections, res[idx1])
}
arr1 = append(arr1, tmp1)
}
out.Values[i] = arr1
case "interfaces":
res := it.Interfaces()
arr1 := jsonw.Array{}
for idx1 := range res {
var tmp1 jsonw.Writer
if res[idx1] == nil {
tmp1 = jsonw.Null
} else {
tmp1 = ec.___Type(field.Selections, res[idx1])
}
arr1 = append(arr1, tmp1)
}
out.Values[i] = arr1
case "possibleTypes":
res := it.PossibleTypes()
arr1 := jsonw.Array{}
for idx1 := range res {
var tmp1 jsonw.Writer
if res[idx1] == nil {
tmp1 = jsonw.Null
} else {
tmp1 = ec.___Type(field.Selections, res[idx1])
}
arr1 = append(arr1, tmp1)
}
out.Values[i] = arr1
case "enumValues":
var arg0 bool
if tmp, ok := field.Args["includeDeprecated"]; ok {
tmp2, err := coerceBool(tmp)
if err != nil {
ec.Error(err)
continue
}
arg0 = tmp2
}
res := it.EnumValues(arg0)
arr1 := jsonw.Array{}
for idx1 := range res {
var tmp1 jsonw.Writer
if res[idx1] == nil {
tmp1 = jsonw.Null
} else {
tmp1 = ec.___EnumValue(field.Selections, res[idx1])
}
arr1 = append(arr1, tmp1)
}
out.Values[i] = arr1
case "inputFields":
res := it.InputFields()
arr1 := jsonw.Array{}
for idx1 := range res {
var tmp1 jsonw.Writer
if res[idx1] == nil {
tmp1 = jsonw.Null
} else {
tmp1 = ec.___InputValue(field.Selections, res[idx1])
}
arr1 = append(arr1, tmp1)
}
out.Values[i] = arr1
case "ofType":
res := it.OfType()
if res == nil {
out.Values[i] = jsonw.Null
} else {
out.Values[i] = ec.___Type(field.Selections, res)
}
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}
return out
}
var parsedSchema = schema.MustParse("schema {\n\tquery: Query\n\tmutation: Mutation\n}\n\n\ntype Query {\n project(id: ID!): Project\n}\n\ntype Mutation {\n updateProject(name: String!, metadata: [String]): Project\n}\n\n\ntype Project {\n id: ID\n name: String\n metadata: [String]\n}\n\n")
func (ec *executionContext) introspectSchema() *introspection.Schema {
return introspection.WrapSchema(parsedSchema)
}
func (ec *executionContext) introspectType(name string) *introspection.Type {
t := parsedSchema.Resolve(name)
if t == nil {
return nil
}
return introspection.WrapType(t)
}
func instanceOf(val string, satisfies []string) bool {
for _, s := range satisfies {
if val == s {
return true
}
}
return false
}
func (ec *executionContext) collectFields(selSet []query.Selection, satisfies []string, visited map[string]bool) []collectedField {
var groupedFields []collectedField
for _, sel := range selSet {
switch sel := sel.(type) {
case *query.Field:
f := getOrCreateField(&groupedFields, sel.Name.Name, func() collectedField {
f := collectedField{
Alias: sel.Alias.Name,
Name: sel.Name.Name,
}
if len(sel.Arguments) > 0 {
f.Args = map[string]interface{}{}
for _, arg := range sel.Arguments {
f.Args[arg.Name.Name] = arg.Value.Value(ec.variables)
}
}
return f
})
f.Selections = append(f.Selections, sel.Selections...)
case *query.InlineFragment:
if !instanceOf(sel.On.Ident.Name, satisfies) {
continue
}
for _, childField := range ec.collectFields(sel.Selections, satisfies, visited) {
f := getOrCreateField(&groupedFields, childField.Name, func() collectedField { return childField })
f.Selections = append(f.Selections, childField.Selections...)
}
case *query.FragmentSpread:
fragmentName := sel.Name.Name
if _, seen := visited[fragmentName]; seen {
continue
}
visited[fragmentName] = true
fragment := ec.doc.Fragments.Get(fragmentName)
if fragment == nil {
ec.Errorf("missing fragment %s", fragmentName)
continue
}
if !instanceOf(fragment.On.Ident.Name, satisfies) {
continue
}
for _, childField := range ec.collectFields(fragment.Selections, satisfies, visited) {
f := getOrCreateField(&groupedFields, childField.Name, func() collectedField { return childField })
f.Selections = append(f.Selections, childField.Selections...)
}
default:
panic(fmt.Errorf("unsupported %T", sel))
}
}
return groupedFields
}
type collectedField struct {
Alias string
Name string
Args map[string]interface{}
Selections []query.Selection
}
func decodeHook(sourceType reflect.Type, destType reflect.Type, value interface{}) (interface{}, error) {
if destType.PkgPath() == "time" && destType.Name() == "Time" {
if dateStr, ok := value.(string); ok {
return time.Parse(time.RFC3339, dateStr)
}
return nil, errors.Errorf("time should be an RFC3339 formatted string")
}
return value, nil
}
// nolint: deadcode, megacheck
func unpackComplexArg(result interface{}, data interface{}) error {
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "graphql",
ErrorUnused: true,
Result: result,
DecodeHook: decodeHook,
})
if err != nil {
panic(err)
}
return decoder.Decode(data)
}
func getOrCreateField(c *[]collectedField, name string, creator func() collectedField) *collectedField {
for i, cf := range *c {
if cf.Alias == name {
return &(*c)[i]
}
}
f := creator()
*c = append(*c, f)
return &(*c)[len(*c)-1]
}
// nolint: deadcode, megacheck
func coerceString(v interface{}) (string, error) {
switch v := v.(type) {
case string:
return v, nil
case int:
return strconv.Itoa(v), nil
case float64:
return fmt.Sprintf("%f", v), nil
case bool:
if v {
return "true", nil
} else {
return "false", nil
}
case nil:
return "null", nil
default:
return "", fmt.Errorf("%T is not a string", v)
}
}
// nolint: deadcode, megacheck
func coerceBool(v interface{}) (bool, error) {
switch v := v.(type) {
case string:
return "true" == strings.ToLower(v), nil
case int:
return v != 0, nil
case bool:
return v, nil
default:
return false, fmt.Errorf("%T is not a bool", v)
}
}
// nolint: deadcode, megacheck
func coerceInt(v interface{}) (int, error) {
switch v := v.(type) {
case string:
return strconv.Atoi(v)
case int:
return v, nil
case float64:
return int(v), nil
default:
return 0, fmt.Errorf("%T is not an int", v)
}
}
// nolint: deadcode, megacheck
func coercefloat64(v interface{}) (float64, error) {
switch v := v.(type) {
case string:
return strconv.ParseFloat(v, 64)
case int:
return float64(v), nil
case float64:
return v, nil
default:
return 0, fmt.Errorf("%T is not an float", v)
}
}
The text was updated successfully, but these errors were encountered:
schema.graphql
When I invoke command
gqlgen -out generated.go
with empty types.json {}I will get the result:
The full code is here
The text was updated successfully, but these errors were encountered: