Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Result field to ResolverContext #301

Merged
merged 8 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions codegen/import_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go/build"
"sort"
"strconv"

// Import and ignore the ambient imports listed below so dependency managers
// don't prune unused code for us. Both lists should be kept in sync.
_ "github.com/99designs/gqlgen/graphql"
Expand Down
30 changes: 22 additions & 8 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,31 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ
}
var arr = "arr" + strconv.Itoa(depth)
var index = "idx" + strconv.Itoa(depth)
var usePtr bool
if len(remainingMods) == 1 && !isPtr {
usePtr = true
}

return tpl(`{{.arr}} := graphql.Array{}
for {{.index}} := range {{.val}} {
{{- if not .isScalar }}
rctx := &graphql.ResolverContext{
Index: &{{.index}},
Result: {{ if .usePtr }}&{{end}}{{.val}}[{{.index}}],
}
ctx := graphql.WithResolverContext(ctx, rctx)
{{- end}}
{{.arr}} = append({{.arr}}, func() graphql.Marshaler {
rctx := graphql.GetResolverContext(ctx)
rctx.PushIndex({{.index}})
defer rctx.Pop()
{{ .next }}
}())
}
return {{.arr}}`, map[string]interface{}{
"val": val,
"arr": arr,
"index": index,
"next": f.doWriteJson(val+"["+index+"]", remainingMods[1:], astType.Elem, false, depth+1),
"val": val,
"arr": arr,
"index": index,
"isScalar": f.IsScalar,
"usePtr": usePtr,
"next": f.doWriteJson(val+"["+index+"]", remainingMods[1:], astType.Elem, false, depth+1),
})

case f.IsScalar:
Expand All @@ -239,7 +249,11 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ
if !isPtr {
val = "&" + val
}
return fmt.Sprintf("return ec._%s(ctx, field.Selections, %s)", f.GQLType, val)
return tpl(`
return ec._{{.type}}(ctx, field.Selections, {{.val}})`, map[string]interface{}{
"type": f.GQLType,
"val": val,
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/data.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion codegen/templates/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
rawArgs := field.ArgumentMap(ec.Variables)
{{ template "args.gotpl" $field.Args }}
{{- end }}
ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{Field: field})
ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{
Field: field,
})
results, err := ec.resolvers.{{ $field.ShortInvocation }}
if err != nil {
ec.Error(ctx, err)
Expand All @@ -24,6 +26,7 @@
}
}
{{ else }}
// nolint: vetshadow
func (ec *executionContext) _{{$object.GQLType}}_{{$field.GQLName}}(ctx context.Context, field graphql.CollectedField, {{if not $object.Root}}obj *{{$object.FullName}}{{end}}) graphql.Marshaler {
{{- if $field.Args }}
rawArgs := field.ArgumentMap(ec.Variables)
Expand Down Expand Up @@ -57,6 +60,7 @@
return graphql.Null
}
res := resTmp.({{$field.Signature}})
rctx.Result = res
{{ $field.WriteJson }}
}
{{ end }}
Loading