Skip to content

Commit

Permalink
migrate middleware handlers to v0.11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
marwan-at-work committed Mar 7, 2020
1 parent 79e45ef commit 22e4e6b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 95 deletions.
76 changes: 29 additions & 47 deletions e2e/twirpql/server.go

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

79 changes: 31 additions & 48 deletions internal/genserver/template.go
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
}
`

0 comments on commit 22e4e6b

Please sign in to comment.