prismagraphql/graphql-middleware

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

Homepage:

Size: 334

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

graphql-middleware

CircleCI npm version

All in one solution to manage middleware in your GraphQL projects.

Overview

GraphQL Middleware is a schema wrapper which allows you to manage additional functionality across multiple resolvers efficiently.

Install
 add graphql-middleware
Usage
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,

Usage with graphql-yoga

graphql-yoga has built-in support for graphql-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'))
Examples
API

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
GraphQL Middleware Use Cases
FAQ
Can I use GraphQL Middleware without GraphQL Yoga?

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.

How does GraphQL Middleware compare to 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.

Should I modify the context using GraphQL Middleware?

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.

Help & Community Slack Status

Join our Slack community if you run into issues or have questions. We love talking to you!

Prisma


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.