Skip to content

Latest commit

 

History

History
122 lines (81 loc) · 3.66 KB

use_fastify.md

File metadata and controls

122 lines (81 loc) · 3.66 KB

graphql-http / use/fastify

Module: use/fastify

Table of contents

Interfaces

Type Aliases

Functions

Server/fastify

HandlerOptions

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

Handler options when using the fastify adapter.

Type parameters

Name Type
Context extends OperationContext = undefined

createHandler

createHandler<Context>(options): RouteHandler

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

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

const fastify = Fastify();
fastify.all('/graphql', createHandler({ schema }));

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

Type parameters

Name Type
Context extends OperationContext = undefined

Parameters

Name Type
options HandlerOptions<Context>

Returns

RouteHandler


parseRequestParams

parseRequestParams(req, reply): 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 FastifyReply 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 Fastify from 'fastify'; // yarn add fastify
import { parseRequestParams } from 'graphql-http/lib/use/fastify';

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

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

Parameters

Name Type
req FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, FastifySchema, FastifyTypeProviderDefault, unknown, FastifyBaseLogger, ResolveFastifyRequestType<FastifyTypeProviderDefault, FastifySchema, RouteGenericInterface>>
reply FastifyReply<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, RouteGenericInterface, unknown, FastifySchema, FastifyTypeProviderDefault, unknown>

Returns

Promise<RequestParams | null>