Name: graphql-middleware
Owner: Prisma
Description: ??All in one solution to manage middleware in your GraphQL projects
Created: 2017-11-09 09:21:44.0
Updated: 2018-05-24 15:27:01.0
Pushed: 2018-05-24 15:27:49.0
Size: 334
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
All in one solution to manage middleware in your GraphQL projects.
GraphQL Middleware is a schema wrapper which allows you to manage additional functionality across multiple resolvers efficiently.
add graphql-middleware
rt { applyMiddleware } from 'graphql-middleware'
rt { makeExecutableSchema } from 'graphql-tools'
rt { authMiddleware, metricsMiddleware } from './middleware'
inimal example middleware (before & after)
t beepMiddleware = {
ery: {
hello: async (resolve, parent, args, context, info) => {
// You can you middleware to override arguments
const argsWithDefault = { name: 'Bob', ...args }
const result = await resolve(parent, argsWithDefault, context, info)
// Or change the returned values of resolvers
return result.replace(/Trump/g, 'beep')
},
t typeDefs = `
pe Query {
hello(name: String): String
t resolvers = {
ery: {
hello: (parent, { name }, context) => `Hello ${name ? name : 'world'}!`,
t schema = makeExecutableSchema({ typeDefs, resolvers })
t schemaWithMiddleware = applyMiddleware(
hema,
tricsMiddleware,
thMiddleware,
epMiddleware,
graphql-yoga
graphql-yoga
has built-in support forgraphql-middleware
!
rt { GraphQLServer } from 'graphql-yoga'
rt { authMiddleware, metricsMiddleware } from './middleware'
t typeDefs = `
pe Query {
hello(name: String): String
t resolvers = {
ery: {
hello: (parent, { name }, context) => `Hello ${name ? name : 'world'}!`,
t server = new GraphQLServer({
peDefs,
solvers,
eldMiddleware: [authMiddleware, metricsMiddleware],
cumentMiddleware: [],
er.start(() => console.log('Server is running on localhost:4000'))
A middleware is a resolver function that wraps another resolver function.
IMiddlewareFunction = (
solve: Function,
rent: any,
gs: any,
ntext: any,
fo: GraphQLResolveInfo,
Promise<any>
rface IMiddlewareTypeMap {
ey: string]: IMiddlewareFunction | IMiddlewareFieldMap
rface IMiddlewareFieldMap {
IMiddleware = IMiddlewareFunction | IMiddlewareTypeMap
tion applyMiddleware(
hema: GraphQLSchema,
.middleware: IMiddleware[]
raphQLSchema
Yes. Nevertheless, we encourage you to use it in combination with Yoga. Combining the power of fieldMiddleware
that GraphQL Middleware offers, with documentMiddleware
which Yoga exposes, gives you unparalleled control over the execution of your queries.
directives
?GraphQL Middleware and directives
tackle the same problem in a completely different way. GraphQL Middleware allows you to implement all your middleware logic in your code, whereas directives encourage you to mix schema with your functionality.
GraphQL Middleware allows you to modify the context of your resolvers, but we encourage you to use GraphQL Yoga's documentMiddleware
for this functionality instead.
Join our Slack community if you run into issues or have questions. We love talking to you!