Create a performant graphql server with micro and graphql-js.
// server.js
import createHandler from 'micro-graphql'
import schema from './schema'
export default createHandler({ schema })
$ micro -p 3000 server.js
Install from NPM:
$ npm install micro-graphql --save
createHandler({ schema, context = null, root = null, formatError = defaultFormatError })
schema
is a non-optional instance ofGraphQLSchema
from graphql-jscontext
can be anything, and is passed to allresolve
functions in your schemaroot
can be anything, and is the root value of your executed queryformatError
is a function which allows custom error formatting; defaults to graphql-js#formatError- This function is exposed as the
default
export. - Returns a
micro request handler
which executes GraphQL queries and responds with JSON - Tip: To configure the handler per-request, simply wrap it:
export default async (req, res) => {
const handler = createHandler({
schema,
context: { req }
})
return handler(req, res)
}