Skip to content

Commit

Permalink
Combine root handlers in ExecutableSchema into a single Exec method
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Nov 5, 2019
1 parent 473a0d2 commit bc98156
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 483 deletions.
7 changes: 2 additions & 5 deletions codegen/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ func (ec *executionContext) _{{$object.Name}}_{{$field.Name}}(ctx context.Contex
{{- if $object.Stream }}
{{- $null = "nil" }}
{{- end }}
ctx = ec.Tracer.StartFieldExecution(ctx, field)

defer func () {
if r := recover(); r != nil {
ec.Error(ctx, ec.Recover(ctx, r))
ret = {{ $null }}
}
ec.Tracer.EndFieldExecution(ctx)
}()
rctx := &graphql.ResolverContext{
Object: {{$object.Name|quote}},
Expand All @@ -29,7 +28,6 @@ func (ec *executionContext) _{{$object.Name}}_{{$field.Name}}(ctx context.Contex
}
rctx.Args = args
{{- end }}
ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx)
{{- if $.Directives.LocationDirectives "FIELD" }}
resTmp := ec._fieldMiddleware(ctx, {{if $object.Root}}nil{{else}}obj{{end}}, func(rctx context.Context) (interface{}, error) {
{{ template "field" $field }}
Expand All @@ -45,7 +43,7 @@ func (ec *executionContext) _{{$object.Name}}_{{$field.Name}}(ctx context.Contex
{{- end }}
if resTmp == nil {
{{- if $field.TypeReference.GQL.NonNull }}
if !ec.HasError(rctx) {
if !graphql.HasFieldError(ctx, rctx) {
ec.Errorf(ctx, "must not be null")
}
{{- end }}
Expand All @@ -68,7 +66,6 @@ func (ec *executionContext) _{{$object.Name}}_{{$field.Name}}(ctx context.Contex
{{- else }}
res := resTmp.({{$field.TypeReference.GO | ref}})
rctx.Result = res
ctx = ec.Tracer.StartFieldChildExecution(ctx)
return ec.{{ $field.TypeReference.MarshalFunc }}(ctx, field.Selections, res)
{{- end }}
}
Expand Down
122 changes: 50 additions & 72 deletions codegen/generated!.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -112,101 +112,79 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
return 0, false
}

func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {
{{- if .QueryRoot }}
ec := executionContext{graphql.GetRequestContext(ctx), e}

buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
{{ if .Directives.LocationDirectives "QUERY" -}}
data := ec._queryMiddleware(ctx, op, func(ctx context.Context) (interface{}, error){
return ec._{{.QueryRoot.Name}}(ctx, op.SelectionSet), nil
})
{{- else -}}
data := ec._{{.QueryRoot.Name}}(ctx, op.SelectionSet)
{{- end }}
func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler {
rc := graphql.GetRequestContext(ctx)
ec := executionContext{rc, e}
first := true

switch rc.Operation.Operation {
{{- if .QueryRoot }} case ast.Query:
return func(ctx context.Context) *graphql.Response {
if !first { return nil }
first = false
{{ if .Directives.LocationDirectives "QUERY" -}}
data := ec._queryMiddleware(ctx, rc.Operation, func(ctx context.Context) (interface{}, error){
return ec._{{.QueryRoot.Name}}(ctx, rc.Operation.SelectionSet), nil
})
{{- else -}}
data := ec._{{.QueryRoot.Name}}(ctx, rc.Operation.SelectionSet)
{{- end }}
var buf bytes.Buffer
data.MarshalGQL(&buf)
return buf.Bytes()
})

return &graphql.Response{
Data: buf,
Errors: ec.Errors,
Extensions: ec.Extensions,
return &graphql.Response{
Data: buf.Bytes(),
}
}
{{- else }}
return graphql.ErrorResponse(ctx, "queries are not supported")
{{- end }}
}

func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {
{{- if .MutationRoot }}
ec := executionContext{graphql.GetRequestContext(ctx), e}
{{ end }}

buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
{{ if .Directives.LocationDirectives "MUTATION" -}}
data := ec._mutationMiddleware(ctx, op, func(ctx context.Context) (interface{}, error){
return ec._{{.MutationRoot.Name}}(ctx, op.SelectionSet), nil
})
{{- else -}}
data := ec._{{.MutationRoot.Name}}(ctx, op.SelectionSet)
{{- end }}
{{- if .MutationRoot }} case ast.Mutation:
return func(ctx context.Context) *graphql.Response {
if !first { return nil }
first = false
{{ if .Directives.LocationDirectives "MUTATION" -}}
data := ec._mutationMiddleware(ctx, rc.Operation, func(ctx context.Context) (interface{}, error){
return ec._{{.MutationRoot.Name}}(ctx, rc.Operation.SelectionSet), nil
})
{{- else -}}
data := ec._{{.MutationRoot.Name}}(ctx, rc.Operation.SelectionSet)
{{- end }}
var buf bytes.Buffer
data.MarshalGQL(&buf)
return buf.Bytes()
})

return &graphql.Response{
Data: buf,
Errors: ec.Errors,
Extensions: ec.Extensions,
return &graphql.Response{
Data: buf.Bytes(),
}
}
{{- else }}
return graphql.ErrorResponse(ctx, "mutations are not supported")
{{- end }}
}

func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDefinition) func() *graphql.Response {
{{- if .SubscriptionRoot }}
ec := executionContext{graphql.GetRequestContext(ctx), e}
{{ end }}

{{- if .SubscriptionRoot }} case ast.Subscription:
{{ if .Directives.LocationDirectives "SUBSCRIPTION" -}}
next := ec._subscriptionMiddleware(ctx, op, func(ctx context.Context) (interface{}, error){
return ec._{{.SubscriptionRoot.Name}}(ctx, op.SelectionSet),nil
next := ec._subscriptionMiddleware(ctx, rc.Operation, func(ctx context.Context) (interface{}, error){
return ec._{{.SubscriptionRoot.Name}}(ctx, rc.Operation.SelectionSet),nil
})
{{- else -}}
next := ec._{{.SubscriptionRoot.Name}}(ctx, op.SelectionSet)
next := ec._{{.SubscriptionRoot.Name}}(ctx, rc.Operation.SelectionSet)
{{- end }}
if ec.Errors != nil {
return graphql.OneShot(&graphql.Response{Data: []byte("null"), Errors: ec.Errors})
}

var buf bytes.Buffer
return func() *graphql.Response {
buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
buf.Reset()
data := next()

if data == nil {
return nil
}
data.MarshalGQL(&buf)
return buf.Bytes()
})
return func(ctx context.Context) *graphql.Response {
buf.Reset()
data := next()

if buf == nil {
if data == nil {
return nil
}
data.MarshalGQL(&buf)

return &graphql.Response{
Data: buf,
Errors: ec.Errors,
Extensions: ec.Extensions,
Data: buf.Bytes(),
}
}
{{- else }}
return graphql.OneShot(graphql.ErrorResponse(ctx, "subscriptions are not supported"))
{{- end }}
{{ end }}
default:
return graphql.OneShot(graphql.ErrorResponse(ctx, "unsupported GraphQL operation"))
}
}

type executionContext struct {
Expand Down
2 changes: 1 addition & 1 deletion codegen/testserver/introspection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestIntrospection(t *testing.T) {

c := client.New(handler.GraphQL(
NewExecutableSchema(Config{Resolvers: resolvers}),
handler.RequestMiddleware(func(ctx context.Context, next func(ctx context.Context) []byte) []byte {
handler.RequestMiddleware(func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
graphql.GetRequestContext(ctx).DisableIntrospection = true

return next(ctx)
Expand Down
3 changes: 1 addition & 2 deletions codegen/testserver/panics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/handler"
"github.com/stretchr/testify/require"
)

func TestPanics(t *testing.T) {
Expand Down
8 changes: 3 additions & 5 deletions codegen/testserver/response_extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ func TestResponseExtension(t *testing.T) {

srv := handler.GraphQL(
NewExecutableSchema(Config{Resolvers: resolvers}),
handler.RequestMiddleware(func(ctx context.Context, next func(ctx context.Context) []byte) []byte {
rctx := graphql.GetRequestContext(ctx)
if err := rctx.RegisterExtension("example", "value"); err != nil {
panic(err)
}
handler.RequestMiddleware(func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
graphql.RegisterExtension(ctx, "example", "value")

return next(ctx)
}),
)
Expand Down
Loading

0 comments on commit bc98156

Please sign in to comment.