Skip to content

Commit

Permalink
Generate FieldMiddleware
Browse files Browse the repository at this point in the history
Moves it off of RequestContext and into generated land.  This change
has a basic implementation of how directive middlewares might work.
  • Loading branch information
Mathew Byrne committed Jul 26, 2018
1 parent 2748a19 commit 0e16f1f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
6 changes: 3 additions & 3 deletions codegen/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package codegen
import "strings"

type Directive struct {
name string
Name string
}

func (d *Directive) Name() string {
return strings.Title(d.name)
func (d *Directive) GoName() string {
return strings.Title(d.Name)
}
2 changes: 1 addition & 1 deletion codegen/directive_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ func (cfg *Config) buildDirectives() (directives []*Directive) {
continue
}
directives = append(directives, &Directive{
name: name,
Name: name,
})
}
return directives
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.

35 changes: 29 additions & 6 deletions codegen/templates/generated.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ResolverRoot interface {

type DirectiveRoot struct {
{{ range $directive := .Directives }}
{{$directive.Name}} graphql.FieldMiddleware
{{$directive.GoName}} graphql.FieldMiddleware
{{ end }}
}

Expand All @@ -56,7 +56,7 @@ func (e *executableSchema) Schema() *ast.Schema {

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

buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
data := ec._{{.QueryRoot.GQLType}}(ctx, op.SelectionSet)
Expand All @@ -76,7 +76,7 @@ func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinitio

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

buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
data := ec._{{.MutationRoot.GQLType}}(ctx, op.SelectionSet)
Expand All @@ -96,7 +96,7 @@ func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefini

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

next := ec._{{.SubscriptionRoot.GQLType}}(ctx, op.SelectionSet)
if ec.Errors != nil {
Expand Down Expand Up @@ -128,8 +128,7 @@ func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDe

type executionContext struct {
*graphql.RequestContext

resolvers ResolverRoot
*executableSchema
}

{{- range $object := .Objects }}
Expand All @@ -148,6 +147,30 @@ type executionContext struct {
{{ template "input.gotpl" $input }}
{{- end }}

func (ec *executionContext) FieldMiddleware(ctx context.Context, next graphql.Resolver) interface{} {
{{- if .Directives }}
rctx := graphql.GetResolverContext(ctx)
if len(rctx.Field.Directives) != 0 {
for _, d := range rctx.Field.Directives {
switch d.Name {
{{- range $directive := .Directives }}
case "{{$directive.Name}}":
next = func(ctx context.Context) (interface{}, error) {
return ec.directives.{{$directive.GoName}}(ctx, next)
}
{{- end }}
}
}
}
{{- end }}
res, err := ec.ResolverMiddleware(ctx, next)
if err != nil {
ec.Error(ctx, err)
return nil
}
return res
}

func (ec *executionContext) introspectSchema() *introspection.Schema {
return introspection.WrapSchema(parsedSchema)
}
Expand Down
9 changes: 0 additions & 9 deletions graphql/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@ func DefaultRequestMiddleware(ctx context.Context, next func(ctx context.Context
return next(ctx)
}

func (c *RequestContext) FieldMiddleware(ctx context.Context, next Resolver) interface{} {
res, err := c.ResolverMiddleware(ctx, next)
if err != nil {
c.Error(ctx, err)
return nil
}
return res
}

func NewRequestContext(doc *ast.QueryDocument, query string, variables map[string]interface{}) *RequestContext {
return &RequestContext{
Doc: doc,
Expand Down

0 comments on commit 0e16f1f

Please sign in to comment.