|
| 1 | +const express = require('express'); |
| 2 | +const { parse } = require('graphql'); |
| 3 | +const { compileQuery, isCompiledQuery } = require('graphql-jit'); |
| 4 | +const bodyParser = require('body-parser'); |
| 5 | +const axios = require('axios'); |
| 6 | +const DataLoader = require('dataloader'); |
| 7 | +const { Agent } = require('http'); |
| 8 | +const { makeExecutableSchema } = require('@graphql-tools/schema'); |
| 9 | + |
| 10 | +const httpAgent = new Agent({ keepAlive: true }); |
| 11 | +const axiosInstance = axios.create({ |
| 12 | + httpAgent, |
| 13 | + proxy: { |
| 14 | + protocol: 'http', |
| 15 | + host: '127.0.0.1', |
| 16 | + port: 3000, |
| 17 | + } |
| 18 | +}); |
| 19 | + |
| 20 | +const typeDefs = ` |
| 21 | + type Post { |
| 22 | + userId: Int |
| 23 | + id: Int |
| 24 | + title: String |
| 25 | + body: String |
| 26 | + user: User |
| 27 | + } |
| 28 | +
|
| 29 | + type User { |
| 30 | + id: Int |
| 31 | + name: String |
| 32 | + username: String |
| 33 | + email: String |
| 34 | + phone: String |
| 35 | + website: String |
| 36 | + } |
| 37 | +
|
| 38 | + type Query { |
| 39 | + greet: String |
| 40 | + posts: [Post] |
| 41 | + } |
| 42 | +`; |
| 43 | + |
| 44 | +const resolvers = { |
| 45 | + Query: { |
| 46 | + greet: () => 'Hello World!', |
| 47 | + posts: async () => { |
| 48 | + try { |
| 49 | + const response = await axiosInstance.get('http://jsonplaceholder.typicode.com/posts'); |
| 50 | + return response.data; |
| 51 | + } catch (error) { |
| 52 | + throw new Error('Failed to fetch posts'); |
| 53 | + } |
| 54 | + }, |
| 55 | + }, |
| 56 | +}; |
| 57 | + |
| 58 | +const schema = makeExecutableSchema({ typeDefs, resolvers }); |
| 59 | + |
| 60 | +async function batchUsers(userIds) { |
| 61 | + const requests = userIds.map(async (id) => { |
| 62 | + const response = await axiosInstance.get(`http://jsonplaceholder.typicode.com/users/${id}`); |
| 63 | + return response.data; |
| 64 | + }); |
| 65 | + return await Promise.all(requests); |
| 66 | +} |
| 67 | + |
| 68 | +const app = express(); |
| 69 | + |
| 70 | +app.use(bodyParser.json()); |
| 71 | + |
| 72 | +// In-memory store for compiled queries |
| 73 | +const queryCache = new Map(); |
| 74 | + |
| 75 | +app.use('/graphql', async (req, res) => { |
| 76 | + const query = req.body.query || req.query.query; |
| 77 | + if (!query) { |
| 78 | + res.status(400).send('Query not provided'); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + try { |
| 83 | + let compiledQuery; |
| 84 | + if (queryCache.has(query)) { |
| 85 | + compiledQuery = queryCache.get(query); |
| 86 | + } else { |
| 87 | + const document = parse(query); |
| 88 | + compiledQuery = compileQuery(schema, document); |
| 89 | + if (!isCompiledQuery(compiledQuery)) { |
| 90 | + throw new Error('Error compiling query'); |
| 91 | + } |
| 92 | + queryCache.set(query, compiledQuery); |
| 93 | + } |
| 94 | + |
| 95 | + const userLoader = new DataLoader(batchUsers, { |
| 96 | + batchScheduleFn: callback => setTimeout(callback, 1), |
| 97 | + }); |
| 98 | + |
| 99 | + const contextValue = { userLoader }; |
| 100 | + |
| 101 | + const result = await compiledQuery.query( |
| 102 | + undefined, |
| 103 | + contextValue, |
| 104 | + req.body.variables, |
| 105 | + req.body.operationName |
| 106 | + ); |
| 107 | + |
| 108 | + res.json(result); |
| 109 | + } catch (error) { |
| 110 | + res.status(500).send(error.message); |
| 111 | + } |
| 112 | +}); |
| 113 | + |
| 114 | +app.listen(8000, () => { |
| 115 | + console.log('Running a GraphQL API server at http://localhost:8000/graphql'); |
| 116 | +}); |
0 commit comments