Skip to content

Latest commit

 

History

History
122 lines (81 loc) · 3.3 KB

use_express.md

File metadata and controls

122 lines (81 loc) · 3.3 KB

graphql-http / use/express

Module: use/express

Table of contents

Interfaces

Type Aliases

Functions

Server/express

HandlerOptions

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

Handler options when using the express adapter.

Type parameters

Name Type
Context extends OperationContext = undefined

createHandler

createHandler<Context>(options): Handler

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

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

const app = express();
app.all('/graphql', 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

Handler


parseRequestParams

parseRequestParams(req, res): 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 the Response argument 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 express from 'express'; // yarn add express
import { parseRequestParams } from 'graphql-http/lib/use/express';

const app = express();
app.all('/graphql', async (req, res) => {
  try {
    const maybeParams = await parseRequestParams(req, res);
    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
    res.writeHead(200).end(JSON.stringify(maybeParams, null, '  '));
  } catch (err) {
    // well-formatted GraphQL over HTTP request,
    // but with invalid parameters
    res.writeHead(400).end(err.message);
  }
});

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

Parameters

Name Type
req Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
res Response<any, Record<string, any>>

Returns

Promise<RequestParams | null>