Novelty Plastic Moustache
Have opinions about JavaScript? We want to hear them. Take the 2018 JavaScript Ecosystem Survey »

graphql-api-koa

2.0.0 • Public • Published

graphql-api-koa logo

graphql-api-koa

npm version Build status

GraphQL API Koa middleware; the first Node.js GraphQL server to support native ESM via .mjs.

Setup

To install graphql-api-koa and graphql from npm run:

npm install graphql-api-koa graphql

See the execute middleware examples to get started.

API

Table of contents

function errorHandler

Creates Koa middleware to handle errors. Use this as the first to catch all errors for a correctly formated GraphQL response. When intentionally throwing an error, create it with status and expose properties using http-errors or the response will be a generic 500 error for security.

Returns: function — Koa middleware.

Examples

How to throw an error determining the response.

import Koa from 'koa'
import bodyParser from 'koa-bodyparser'
import { errorHandler, execute } from 'graphql-api-koa'
import createError from 'http-errors'
import schema from './schema'
 
const app = new Koa()
  .use(errorHandler())
  .use(async (ctx, next) => {
    if (
      // It’s Saturday.
      new Date().getDay() === 6
    )
      throw createError(503, 'No work on the sabbath.', { expose: true })
 
    await next()
  })
  .use(bodyParser())
  .use(execute({ schema }))

function execute

Creates Koa middleware to execute GraphQL. Use after the errorHandler and body parser middleware.

Parameter Type Description
options ExecuteOptions Options.

Returns: function — Koa middleware.

Examples

A basic GraphQL API.

import Koa from 'koa'
import bodyParser from 'koa-bodyparser'
import { errorHandler, execute } from 'graphql-api-koa'
import schema from './schema'
 
const app = new Koa()
  .use(errorHandler())
  .use(bodyParser())
  .use(execute({ schema }))

type ExecuteOptions

GraphQL execute Koa middleware options.

Type: Object

Property Type Description
schema GraphQLSchema GraphQL schema.
rootValue *? Value passed to the first resolver.
contextValue *? Execution context (usually an object) passed to resolvers.
fieldResolver function? Custom default field resolver.
override MiddlewareOptionsOverride? Override any ExecuteOptions (except override) per request.

Examples

execute middleware options that sets the schema once but populates the user in the GraphQL context from the Koa context each request.

import schema from './schema'
 
const executeOptions = {
  schema,
  override: ctx => ({
    contextValue: {
      user: ctx.state.user
    }
  })
}

type MiddlewareOptionsOverride

Per-request Koa middleware options override.

Type: function

Parameter Type Description
context Object Koa context.

Returns: Object — Options.

Examples

An execute middleware options override that populates the user in the GraphQL context from the Koa request context.

const executeOptionsOverride = ctx => ({
  contextValue: {
    user: ctx.state.user
  }
})

install

npm i graphql-api-koa

Downloadsweekly downloads

49

version

2.0.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability