import{applyMiddleware}from'graphql-middleware'import{makeExecutableSchema}from'graphql-tools'importauthorizationMiddlewarefrom'foxer360-authorization'consttypeDefs=` type Query { hello(name: String): String }`constresolvers={Query: {hello: (parent,{ name },context)=>`Hello ${name ? name : 'world'}!`,},}constschema=makeExecutableSchema({ typeDefs, resolvers })constschemaWithMiddleware=applyMiddleware(schema,authorizationMiddleware('http://localhost:4001')// authorization api address)
Usage with graphql-yoga
graphql-yoga has built-in support for graphql-middleware!
import{GraphQLServer}from'graphql-yoga'importauthorizationMiddlewarefrom'foxer360-authorization';consttypeDefs=` type Query { hello(name: String): String }`constresolvers={Query: {hello: (parent,{ name },context)=>`Hello ${name ? name : 'world'}!`,},}constserver=newGraphQLServer({
typeDefs,
resolvers,middlewares: [authorizationMiddleware('http://localhost:4001')// authorization api address],documentMiddleware: [],})server.start(()=>console.log('Server is running on localhost:4000'))