Skip to content

Latest commit

 

History

History
127 lines (86 loc) · 3.29 KB

use_koa.md

File metadata and controls

127 lines (86 loc) · 3.29 KB

graphql-http / use/koa

Module: use/koa

Table of contents

Interfaces

Type Aliases

Functions

Server/koa

HandlerOptions

Ƭ HandlerOptions<Context>: HandlerOptions<IncomingMessage, RequestContext, Context>

Handler options when using the koa adapter.

Type parameters

Name Type
Context extends OperationContext = undefined

createHandler

createHandler<Context>(options): Middleware

Create a GraphQL over HTTP spec compliant request handler for the Koa framework.

import Koa from 'koa'; // yarn add koa
import mount from 'koa-mount'; // yarn add koa-mount
import { createHandler } from 'graphql-http/lib/use/koa';
import { schema } from './my-graphql-schema';

const app = new Koa();
app.use(mount('/', createHandler({ schema })));

app.listen({ port: 4000 });
console.log('Listening to port 4000');

Type parameters

Name Type
Context extends OperationContext = undefined

Parameters

Name Type
options HandlerOptions<Context>

Returns

Middleware


parseRequestParams

parseRequestParams(ctx): Promise<RequestParams | null>

The GraphQL over HTTP spec compliant request parser for an incoming GraphQL request.

If the HTTP request is not a well-formatted GraphQL over HTTP request, the function will respond on Koa's ParameterizedContext response and return null.

If the HTTP request is a well-formatted GraphQL over HTTP request, but is invalid or malformed, the function will throw an error and it is up to the user to handle and respond as they see fit.

import Koa from 'koa'; // yarn add koa
import mount from 'koa-mount'; // yarn add koa-mount
import { parseRequestParams } from 'graphql-http/lib/use/koa';

const app = new Koa();
app.use(
  mount('/', async (ctx) => {
    try {
      const maybeParams = await parseRequestParams(ctx);
      if (!maybeParams) {
        // not a well-formatted GraphQL over HTTP request,
        // parser responded and there's nothing else to do
        return;
      }

      // well-formatted GraphQL over HTTP request,
      // with valid parameters
      ctx.response.status = 200;
      ctx.body = JSON.stringify(maybeParams, null, '  ');
    } catch (err) {
      // well-formatted GraphQL over HTTP request,
      // but with invalid parameters
      ctx.response.status = 400;
      ctx.body = err.message;
    }
  }),
);

app.listen({ port: 4000 });
console.log('Listening to port 4000');

Parameters

Name Type
ctx ParameterizedContext

Returns

Promise<RequestParams | null>