Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(graphql): allow to register additional OperationMiddlewares #51

Merged
merged 2 commits into from Dec 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions module.go
Expand Up @@ -40,16 +40,18 @@ type routes struct {
origins flamingoConfig.Slice
introspectionEnabled bool
uploadMaxSize int64
operationMiddlewares []graphql.OperationMiddleware
}

// Inject executable schema
func (r *routes) Inject(
reverseRouter web.ReverseRouter,
config *struct {
Exec graphql.ExecutableSchema `inject:",optional"`
Origins flamingoConfig.Slice `inject:"config:graphql.cors.origins"`
IntrospectionEnabled bool `inject:"config:graphql.introspectionEnabled,optional"`
UploadMaxSize int64 `inject:"config:graphql.multipartForm.uploadMaxSize,optional"`
Exec graphql.ExecutableSchema `inject:",optional"`
OperationMiddlewares []graphql.OperationMiddleware `inject:",optional"`
Origins flamingoConfig.Slice `inject:"config:graphql.cors.origins"`
IntrospectionEnabled bool `inject:"config:graphql.introspectionEnabled,optional"`
UploadMaxSize int64 `inject:"config:graphql.multipartForm.uploadMaxSize,optional"`
},
) {
r.reverseRouter = reverseRouter
Expand All @@ -58,6 +60,7 @@ func (r *routes) Inject(
r.origins = config.Origins
r.introspectionEnabled = config.IntrospectionEnabled
r.uploadMaxSize = config.UploadMaxSize
r.operationMiddlewares = config.OperationMiddlewares
}
}

Expand Down Expand Up @@ -101,6 +104,10 @@ func (r *routes) Routes(registry *web.RouterRegistry) {
return next(ctx)
})

for _, middleware := range r.operationMiddlewares {
gqlHandler.AroundOperations(middleware)
}

registry.MustRoute("/graphql", "graphql")
registry.HandleOptions("graphql", web.WrapHTTPHandler(corsHandler.preflightHandler()))
registry.HandleAny("graphql", wrapGqlHandler(corsHandler.gqlMiddleware(gqlHandler)))
Expand Down