-
Notifications
You must be signed in to change notification settings - Fork 47
/
dataLoaderPerBatchRequest.js
75 lines (65 loc) · 2.42 KB
/
dataLoaderPerBatchRequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Example of how you can use a single DataLoader for all (batched) queries
within a single HTTP-request.
Tuned for performance, to avoid creating unnecessary functions per request.
*/
import express from 'express';
import graphqlHTTP from 'express-graphql';
import { graphqlBatchHTTPWrapper } from 'react-relay-network-layer';
import bodyParser from 'body-parser';
import myGraphqlSchema from './graphqlSchema';
import DataLoader from 'dataloader';
const port = 3000;
const server = express();
// Define DataLoaders implementation
const initDataLoaders = () => {
const authors = new DataLoader(keys => new Promise((resolve, reject) => {
// somehow found authors by keys, call resolve or reject
}));
const articles = new DataLoader(keys => new Promise((resolve, reject) => {
// somehow found articles by keys, call resolve or reject
}));
return {
authors,
articles,
};
};
// for performance reasons, create a single error function converter (rather than every request)
const formatError = (error) => ({
// better errors for development. `stack` used in `gqErrors` middleware
message: error.message,
stack: process.env.NODE_ENV === 'development' ? error.stack.split('\n') : null,
});
// prepare `graphqlHTTP` express-middleware per request settings
const graphqlSettingsPerRequest = (req) => ({
schema: myGraphqlSchema,
graphiql: true,
formatError,
pretty: true,
context: {
request: req, // just for example, pass request to context
dataLoaders: initDataLoaders(),
},
});
// Declare route for batch query
// `graphqlBatchHTTPWrapper` and `graphqlHTTP` return arrow functions.
// For performance reasons, declare it once outside (req, res, next) => {} block,
// rather than for every request. Otherwise, it will result in the garbage collector
// being invoked way more than necessary.
const graphqlBatchMiddleware = graphqlBatchHTTPWrapper(
graphqlHTTP(req => req.graphqlServerSettings)
);
server.use('/graphql/batch', // NB: should be before `server.use('/graphql', ...)`
bodyParser.json(),
(req, res, next) => {
req.graphqlServerSettings = graphqlSettingsPerRequest(req); // eslint-disable-line
graphqlBatchMiddleware(req, res, next);
}
);
// Declare standard graphql route
server.use('/graphql', // NB: should be after `server.use('/graphql/batch', ...)`)
graphqlHTTP(graphqlSettingsPerRequest)
);
server.listen(port, () => {
console.log(`The server is running at http://localhost:${port}/`);
});