Skip to content

Commit

Permalink
Merge pull request #301 from 99designs/feat-directive-parent
Browse files Browse the repository at this point in the history
add Result field to ResolverContext
  • Loading branch information
vektah authored Aug 24, 2018
2 parents 50588a8 + 6ef2035 commit 9b24710
Show file tree
Hide file tree
Showing 14 changed files with 1,644 additions and 394 deletions.
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

0 comments on commit 9b24710

Please sign in to comment.