-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add graphql-jit * chore: add graphql_jit in run_benchmarks * chore: bump changes * chore: cache
- Loading branch information
1 parent
64288a5
commit 825b87d
Showing
7 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "graphql_jit", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"description": "", | ||
"dependencies": { | ||
"@graphql-tools/schema": "^10.0.4", | ||
"axios": "^1.7.2", | ||
"dataloader": "^2.2.2", | ||
"express": "^4.19.2", | ||
"graphql": "^15.9.0", | ||
"graphql-jit": "^0.8.6" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
pwd | ||
cd graphql/graphql_jit | ||
node server.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
const express = require('express'); | ||
const { parse } = require('graphql'); | ||
const { compileQuery, isCompiledQuery } = require('graphql-jit'); | ||
const bodyParser = require('body-parser'); | ||
const axios = require('axios'); | ||
const DataLoader = require('dataloader'); | ||
const { Agent } = require('http'); | ||
const { makeExecutableSchema } = require('@graphql-tools/schema'); | ||
|
||
const httpAgent = new Agent({ keepAlive: true }); | ||
const axiosInstance = axios.create({ | ||
httpAgent, | ||
proxy: { | ||
protocol: 'http', | ||
host: '127.0.0.1', | ||
port: 3000, | ||
} | ||
}); | ||
|
||
const typeDefs = ` | ||
type Post { | ||
userId: Int | ||
id: Int | ||
title: String | ||
body: String | ||
user: User | ||
} | ||
type User { | ||
id: Int | ||
name: String | ||
username: String | ||
email: String | ||
phone: String | ||
website: String | ||
} | ||
type Query { | ||
greet: String | ||
posts: [Post] | ||
} | ||
`; | ||
|
||
const resolvers = { | ||
Query: { | ||
greet: () => 'Hello World!', | ||
posts: async () => { | ||
try { | ||
const response = await axiosInstance.get('http://jsonplaceholder.typicode.com/posts'); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error('Failed to fetch posts'); | ||
} | ||
}, | ||
}, | ||
}; | ||
|
||
const schema = makeExecutableSchema({ typeDefs, resolvers }); | ||
|
||
async function batchUsers(userIds) { | ||
const requests = userIds.map(async (id) => { | ||
const response = await axiosInstance.get(`http://jsonplaceholder.typicode.com/users/${id}`); | ||
return response.data; | ||
}); | ||
return await Promise.all(requests); | ||
} | ||
|
||
const app = express(); | ||
|
||
app.use(bodyParser.json()); | ||
|
||
// In-memory store for compiled queries | ||
const queryCache = new Map(); | ||
|
||
app.use('/graphql', async (req, res) => { | ||
const query = req.body.query || req.query.query; | ||
if (!query) { | ||
res.status(400).send('Query not provided'); | ||
return; | ||
} | ||
|
||
try { | ||
let compiledQuery; | ||
if (queryCache.has(query)) { | ||
compiledQuery = queryCache.get(query); | ||
} else { | ||
const document = parse(query); | ||
compiledQuery = compileQuery(schema, document); | ||
if (!isCompiledQuery(compiledQuery)) { | ||
throw new Error('Error compiling query'); | ||
} | ||
queryCache.set(query, compiledQuery); | ||
} | ||
|
||
const userLoader = new DataLoader(batchUsers, { | ||
batchScheduleFn: callback => setTimeout(callback, 1), | ||
}); | ||
|
||
const contextValue = { userLoader }; | ||
|
||
const result = await compiledQuery.query( | ||
undefined, | ||
contextValue, | ||
req.body.variables, | ||
req.body.operationName | ||
); | ||
|
||
res.json(result); | ||
} catch (error) { | ||
res.status(500).send(error.message); | ||
} | ||
}); | ||
|
||
app.listen(8000, () => { | ||
console.log('Running a GraphQL API server at http://localhost:8000/graphql'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters