Skip to content

Commit

Permalink
feat: Add graphql-jit (#305)
Browse files Browse the repository at this point in the history
* feat: Add graphql-jit

* chore: add graphql_jit in run_benchmarks

* chore: bump changes

* chore: cache
  • Loading branch information
Dhanus3133 authored Jul 9, 2024
1 parent 64288a5 commit 825b87d
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Explore and compare the performance of the fastest GraphQL frameworks through ou
[Caliban]: https://github.com/ghostdogpr/caliban
[async-graphql]: https://github.com/async-graphql/async-graphql
[Hasura]: https://github.com/hasura/graphql-engine
[GraphQL JIT]: https://github.com/zalando-incubator/graphql-jit

## Introduction

Expand Down
3 changes: 2 additions & 1 deletion analyze.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ formattedServerNames=(
["caliban"]="Caliban"
["async_graphql"]="async-graphql"
["hasura"]="Hasura"
["graphql_jit"]="GraphQL JIT"
)

servers=("apollo" "caliban" "netflixdgs" "gqlgen" "tailcall" "async_graphql" "hasura")
servers=("apollo" "caliban" "netflixdgs" "gqlgen" "tailcall" "async_graphql" "hasura" "graphql_jit")
resultFiles=("$@")
declare -A avgReqSecs
declare -A avgLatencies
Expand Down
19 changes: 19 additions & 0 deletions graphql/graphql_jit/package.json
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"
}
}
4 changes: 4 additions & 0 deletions graphql/graphql_jit/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
pwd
cd graphql/graphql_jit
node server.js
116 changes: 116 additions & 0 deletions graphql/graphql_jit/server.js
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');
});
2 changes: 1 addition & 1 deletion run_benchmarks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function runBenchmark() {

rm "results.md"

for service in "apollo_server" "caliban" "netflix_dgs" "gqlgen" "tailcall" "async_graphql" "hasura"; do
for service in "apollo_server" "caliban" "netflix_dgs" "gqlgen" "tailcall" "async_graphql" "hasura" "graphql_jit"; do
runBenchmark "graphql/${service}/run.sh"
if [ "$service" == "apollo_server" ]; then
cd graphql/apollo_server/
Expand Down
5 changes: 5 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ cd ../../
cd graphql/hasura
npm install
cd ../../

# For graphql_jit
cd graphql/graphql_jit
npm install
cd ../../

0 comments on commit 825b87d

Please sign in to comment.