From 7f5b8512696f5cfcf3e1ebff438375bde4eb7787 Mon Sep 17 00:00:00 2001 From: enisdenjo Date: Sat, 8 Jul 2023 12:27:46 +0300 Subject: [PATCH] recipes --- README.md | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/README.md b/README.md index c6f5c3ca..c5eb7959 100644 --- a/README.md +++ b/README.md @@ -725,6 +725,78 @@ console.log('Listening to port 4000'); +
+🔗 Server handler usage with graphql-upload and http + +```js +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 + +```js +import express from 'express'; // yarn add express +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 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'); +``` + +
+
🔗 Audit for servers usage in Jest environment