Releases: graphql/graphql-http
Releases · graphql/graphql-http
v1.22.4
v1.22.3
v1.22.2
v1.22.1
v1.22.0
1.22.0 (2023-08-28)
Features
- handler: Expose parseRequestParamsfrom the core and each of the adapters (#111) (2caae00), closes #106
Examples
Request params parser usage with http
import http from 'http';
import { parseRequestParams } from 'graphql-http/lib/use/http';
const server = http.createServer(async (req, res) => {
  if (req.url.startsWith('/graphql')) {
    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);
    }
  } else {
    res.writeHead(404).end();
  }
});
server.listen(4000);
console.log('Listening to port 4000');Request params parser usage with http2
$ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
  -keyout localhost-privkey.pem -out localhost-cert.pemimport fs from 'fs';
import http2 from 'http2';
import { parseRequestParams } from 'graphql-http/lib/use/http2';
const server = http2.createSecureServer(
  {
    key: fs.readFileSync('localhost-privkey.pem'),
    cert: fs.readFileSync('localhost-cert.pem'),
  },
  async (req, res) => {
    if (req.url.startsWith('/graphql')) {
      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);
      }
    } else {
      res.writeHead(404).end();
    }
  },
);
server.listen(4000);
console.log('Listening to port 4000');Request params parser usage with express
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');Request params parser usage with fastify
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');Request params parser usage with Koa
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');Request params parser usage with uWebSockets.js
import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version>
import { parseRequestParams } from 'graphql-http/lib/use/uWebSockets';
uWS
  .App()
  .any('/graphql', async (res, req) => {
    const abortedRef = { current: false };
    res.onAborted(() => (abortedRef.current = true));
    try {
      const maybeParams = await parseRequestParams(req, res, abortedRef);
      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
      if (!abortedRef.current) {
        res.writeStatus('200 OK');
        res.end(JSON.stringify(maybeParams, null, '  '));
      }
    } catch (err) {
      // well-formatted GraphQL over HTTP request,
      // but with invalid parameters
      if (!abortedRef.current) {
        res.writeStatus('400 Bad Request');
        res.end(err.message);
      }
    }
  })
  .listen(4000, () => {
    console.log('Listening to port 4000');
  });Request params parser usage with Deno
import { serve } from 'https://deno.land/[email protected]/http/server.ts';
import { parseRequestParams } from 'https://esm.sh/graphql-http/lib/use/fetch';
await serve(
  async (req: Request) => {
    const [path, _search] = req.url.split('?');
    if (path.endsWith('/graphql')) {
      try {
        const paramsOrResponse = await parseRequestParams(req);
        if (paramsOrResponse instanceof Response) {
          // not a well-formatted GraphQL over HTTP request,
          // parser created a response object to use
          return paramsOrResponse;
        }
        // well-formatted GraphQL over HTTP request,
        // with valid parameters
        return new Response(JSON.stringify(paramsOrResponse, null, '  '), {
          status: 200,
        });
      } catch (err) {
        // well-formatted GraphQL over HTTP request,
        // but with invalid parameters
        return new Response(err.message, { status: 400 });
      }
    } else {
      return new Response(null, { status: 404 });
    }
  },
  {
    port: 4000, // Listening to port 4000
  },
);Request params parser usage with Bun
import { parseRequestParams } from 'graphql-http/lib/use/fetch'; // bun install graphql-http
export default {
  port: 4000, // Listening to port 4000
  async fetch(req) {
    const [path, _search] = req.url.split('?');
    if (path.endsWith('/graphql')) {
      try {
        const paramsOrResponse = await parseRequestParams(req);
        if (paramsOrResponse instanceof Response) {
          // not a well-formatted GraphQL over HTTP request,
          // parser created a response object to use
          return paramsOrResponse;
        }
        // well-formatted GraphQL over HTTP request,
        // with valid parameters
        return new Response(JSON.stringify(paramsOrResponse, null, '  '), {
          status: 200,
        });
      } catch (err) {
        // well-formatted GraphQL over HTTP request,
        // but with invalid parameters
        return new Response(err.message, { status: 400 });
      }
    } else {
      return new Response(null, { status: 404 });
    }
  },
};v1.21.0
1.21.0 (2023-07-17)
Bug Fixes
Features
Examples
Start the server with Netlify Functions
import { createHandler } from 'graphql-http/lib/use/@netlify/functions'; // yarn add @netlify/functions
import { schema } from './previous-step';
// Create the GraphQL over HTTP native fetch handler
export const handler = createHandler({ schema });v1.20.0
1.20.0 (2023-07-08)
Bug Fixes
- handler: Don't export makeResponse,getAcceptableMediaTypeorisResponse(#98) (a638cb4)
- handler: Request params optional properties can also be null (10a6f06)
Features
Examples
Server handler usage with graphql-upload and http
import http from 'http';
import { createHandler } from 'graphql-http/lib/use/http';
import processRequest from 'graphql-upload/processRequest.mjs'; // yarn add graphql-upload
import { schema } from './my-graphql';
const handler = createHandler({
  schema,
  async parseRequestParams(req) {
    const params = await processRequest(req.raw, req.context.res);
    if (Array.isArray(params)) {
      throw new Error('Batching is not supported');
    }
    return {
      ...params,
      // variables must be an object as per the GraphQL over HTTP spec
      variables: Object(params.variables),
    };
  },
});
const server = http.createServer((req, res) => {
  if (req.url.startsWith('/graphql')) {
    handler(req, res);
  } else {
    res.writeHead(404).end();
  }
});
server.listen(4000);
console.log('Listening to port 4000');Server handler usage with graphql-upload and express
import express from 'express'; // yarn add express
import { createHandler } from 'graphql-http/lib/use/express';
import processRequest from 'graphql-upload/processRequest.mjs'; // yarn add graphql-upload
import { schema } from './my-graphql';
const app = express();
app.all(
  '/graphql',
  createHandler({
    schema,
    async parseRequestParams(req) {
      const params = await processRequest(req.raw, req.context.res);
      if (Array.isArray(params)) {
        throw new Error('Batching is not supported');
      }
      return {
        ...params,
        // variables must be an object as per the GraphQL over HTTP spec
        variables: Object(params.variables),
      };
    },
  }),
);
app.listen({ port: 4000 });
console.log('Listening to port 4000');v1.19.0
1.19.0 (2023-06-05)
Bug Fixes
- use: processglobal is not available in all environments andNODE_ENVdoesn't necessarily depict production vs. development (d08ead3)
Features
Examples
Start the server with uWebSockets.js
import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version>
import { createHandler } from 'graphql-http/lib/use/uWebSockets';
import { schema } from './my-graphql-schema';
uWS
  .App()
  .any('/graphql', createHandler({ schema }))
  .listen(4000, () => {
    console.log('Listening to port 4000');
  });