UNPKG

flight-path

Version:

Express style router for Fastly Compute@Edge

56 lines (47 loc) 1.67 kB
import { Router } from "../../../dist/index.js"; import { getGraphQLParameters, processRequest, renderGraphiQL, shouldRenderGraphiQL, sendResult, } from "graphql-helix"; import { schema } from "./schema"; import FPRequest from "../../../dist/lib/routing/request.js"; const router = new Router(); router.use("*", async (req, res) => { let body = {}; try { body = await req.json(); } catch (e) {} // Create a generic Request object that can be consumed by Graphql Helix's API const request = { body: body, headers: req.headers, method: req.method, query: req.query, }; // Determine whether we should render GraphiQL instead of returning an API response if (shouldRenderGraphiQL(request)) { res.send(renderGraphiQL()); } else { // Extract the Graphql parameters from the request const { operationName, query, variables } = getGraphQLParameters(request); // Validate and execute the query const result = await processRequest({ operationName, query, variables, request, schema, }); // processRequest returns one of three types of results depending on how the server should respond // 1) RESPONSE: a regular JSON payload // 2) MULTIPART RESPONSE: a multipart response (when @stream or @defer directives are used) // 3) PUSH: a stream of events to push back down the client for a subscription // The "sendResult" is a NodeJS-only shortcut for handling all possible types of Graphql responses, // See "Advanced Usage" below for more details and customizations available on that layer. sendResult(result, res); } }); router.listen();