-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate middleware handlers to v0.11.x
- Loading branch information
1 parent
79e45ef
commit 22e4e6b
Showing
2 changed files
with
60 additions
and
95 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,55 @@ | ||
package genserver | ||
|
||
var tmpl = `{{ reserveImport "context" }} | ||
{{ reserveImport "fmt" }} | ||
{{ reserveImport "net/http" }} | ||
{{ reserveImport "strings" }} | ||
{{ reserveImport "github.com/99designs/gqlgen/handler" }} | ||
{{ reserveImport "github.com/99designs/gqlgen/graphql/handler" }} | ||
{{ reserveImport "github.com/twitchtv/twirp" }} | ||
{{ reserveImport "github.com/twitchtv/twirp/ctxsetters" }} | ||
{{ reserveImport "github.com/99designs/gqlgen/graphql" }} | ||
{{ reserveImport "github.com/99designs/gqlgen/graphql/handler/extension" }} | ||
{{ reserveImport "github.com/99designs/gqlgen/graphql/handler/transport" }} | ||
{{ reserveImport "github.com/99designs/gqlgen/graphql/playground" }} | ||
// Playground is a proxy to github.com/99designs/gqlgen/handler.Playground | ||
// All you need to do is provide a title and the URL Path to the GraphQL handler | ||
func Playground(title, endpoint string) http.Handler { | ||
return handler.Playground(title, endpoint) | ||
return playground.Handler(title, endpoint) | ||
} | ||
// Handler returns a handler to the GraphQL API. | ||
// Handler returns a handler to the GraphQL API. | ||
// Server Hooks are optional but if present, they will | ||
// be injected as GraphQL middleware. | ||
func Handler(service {{lookupImport .ModPath}}.{{.ServiceName}}, hooks *twirp.ServerHooks, opts ...handler.Option) http.Handler { | ||
func Handler(service {{lookupImport .ModPath}}.{{.ServiceName}}, hooks *twirp.ServerHooks) *handler.Server { | ||
es := NewExecutableSchema(Config{Resolvers: &Resolver{service}}) | ||
srv := handler.New(es) | ||
srv.AddTransport(transport.POST{}) | ||
srv.Use(extension.Introspection{}) | ||
if hooks == nil { | ||
return handler.GraphQL(NewExecutableSchema(Config{Resolvers: &Resolver{service}}), opts...) | ||
return srv | ||
} | ||
h := &middlewareHooks{hooks} | ||
opts = append([]handler.Option{handler.ResolverMiddleware(h.hook)}, opts...) | ||
return handler.GraphQL( | ||
NewExecutableSchema( | ||
Config{Resolvers: &Resolver{service}}, | ||
), | ||
opts... | ||
) | ||
} | ||
type middlewareHooks struct { | ||
hooks *twirp.ServerHooks | ||
} | ||
func (h *middlewareHooks) hook(ctx context.Context, next graphql.Resolver) (res interface{}, err error) { | ||
ifc := graphql.GetResolverContext(ctx).Path() | ||
if len(ifc) > 0 { | ||
queryName := ifc.String() | ||
if queryName != "" { | ||
ctx = ctxsetters.WithMethodName(ctx, strings.Title(queryName)) | ||
srv.AroundFields(func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) { | ||
f := graphql.GetFieldContext(ctx) | ||
parent := f.Parent.Path().String() | ||
if parent != "" { | ||
return next(ctx) | ||
} | ||
} | ||
if h.hooks.RequestRouted != nil { | ||
ctx, err = h.hooks.RequestRouted(ctx) | ||
if err != nil { | ||
if h.hooks.Error != nil { | ||
terr, ok := err.(twirp.Error) | ||
if ok { | ||
h.hooks.Error(ctx, terr) | ||
} else { | ||
fmt.Println("Twirp err does not implement twirp.Error:", err) | ||
ctx = ctxsetters.WithMethodName(ctx, f.Field.Name) | ||
if hooks.RequestRouted != nil { | ||
ctx, err = hooks.RequestRouted(ctx) | ||
if err != nil { | ||
if terr, ok := err.(twirp.Error); ok && hooks.Error != nil { | ||
ctx = hooks.Error(ctx, terr) | ||
} | ||
return nil, err | ||
} | ||
return nil, err | ||
} | ||
} | ||
res, err = next(ctx) | ||
if err != nil && h.hooks.Error != nil { | ||
terr, ok := err.(twirp.Error) | ||
if ok { | ||
h.hooks.Error(ctx, terr) | ||
} else { | ||
fmt.Println("Twirp err does not implement twirp.Error:", err) | ||
res, err = next(ctx) | ||
if terr, ok := err.(twirp.Error); ok && hooks.Error != nil { | ||
ctx = hooks.Error(ctx, terr) | ||
} | ||
} | ||
return res, err | ||
return res, err | ||
}) | ||
return srv | ||
} | ||
` |