Skip to content

Commit 7f5b851

Browse files
committed
recipes
1 parent f059941 commit 7f5b851

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

README.md

+72
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,78 @@ console.log('Listening to port 4000');
725725

726726
</details>
727727

728+
<details id="graphql-upload-http">
729+
<summary><a href="#graphql-upload-http">🔗</a> Server handler usage with <a href="https://github.com/jaydenseric/graphql-upload">graphql-upload</a> and <a href="https://nodejs.org/api/http.html">http</a></summary>
730+
731+
```js
732+
import http from 'http';
733+
import { createHandler } from 'graphql-http/lib/use/http';
734+
import processRequest from 'graphql-upload/processRequest.mjs'; // yarn add graphql-upload
735+
import { schema } from './my-graphql';
736+
737+
const handler = createHandler({
738+
schema,
739+
async parseRequestParams(req) {
740+
const params = await processRequest(req.raw, req.context.res);
741+
if (Array.isArray(params)) {
742+
throw new Error('Batching is not supported');
743+
}
744+
return {
745+
...params,
746+
// variables must be an object as per the GraphQL over HTTP spec
747+
variables: Object(params.variables),
748+
};
749+
},
750+
});
751+
752+
const server = http.createServer((req, res) => {
753+
if (req.url.startsWith('/graphql')) {
754+
handler(req, res);
755+
} else {
756+
res.writeHead(404).end();
757+
}
758+
});
759+
760+
server.listen(4000);
761+
console.log('Listening to port 4000');
762+
```
763+
764+
</details>
765+
766+
<details id="graphql-upload-express">
767+
<summary><a href="#graphql-upload-express">🔗</a> Server handler usage with <a href="https://github.com/jaydenseric/graphql-upload">graphql-upload</a> and <a href="https://expressjs.com/">express</a></summary>
768+
769+
```js
770+
import express from 'express'; // yarn add express
771+
import { createHandler } from 'graphql-http/lib/use/http';
772+
import processRequest from 'graphql-upload/processRequest.mjs'; // yarn add graphql-upload
773+
import { schema } from './my-graphql';
774+
775+
const app = express();
776+
app.all(
777+
'/graphql',
778+
createHandler({
779+
schema,
780+
async parseRequestParams(req) {
781+
const params = await processRequest(req.raw, req.context.res);
782+
if (Array.isArray(params)) {
783+
throw new Error('Batching is not supported');
784+
}
785+
return {
786+
...params,
787+
// variables must be an object as per the GraphQL over HTTP spec
788+
variables: Object(params.variables),
789+
};
790+
},
791+
}),
792+
);
793+
794+
app.listen({ port: 4000 });
795+
console.log('Listening to port 4000');
796+
```
797+
798+
</details>
799+
728800
<details id="audit-jest">
729801
<summary><a href="#audit-jest">🔗</a> Audit for servers usage in <a href="https://jestjs.io">Jest</a> environment</summary>
730802

0 commit comments

Comments
 (0)