GraphQL-JS is a very well written runtime implementation of the latest GraphQL spec. However, by compiling to JS, V8 is able to create optimized code which yields much better performance.
$ ts-node -T ./src/__benchmarks__/schema.benchmark.ts
graphql-js x 3,873 ops/sec ±3.89% (74 runs sampled)
graphql-jit x 128,301 ops/sec ±1.79% (75 runs sampled)
Fastest is graphql-jit
$ ts-node -T ./src/__benchmarks__/schema-many-resolvers.benchmark.ts
graphql-js x 5,458 ops/sec ±5.62% (70 runs sampled)
graphql-jit x 52,345 ops/sec ±3.95% (74 runs sampled)
Fastest is graphql-jit
The goal is to support the June 2018 version of the GraphQL spec. At this moment, the only missing feature is support for the @skip and @include directives.
In order to achieve better performance, the compiler introduces some limitations. The main one is that all computed properties must have a resolver and only these can return a Promise.
yarn add graphql-jit
For complete working examples, check the examples/ directory
const typedefs = `
type Query {
hello: string
}
`;
const resolvers = {
Query: {
hello() {
return new Promise(resolve => setTimeout(() => resolve("World!"), 200));
}
}
};
const { makeExecutableSchema } = require("graphql");
const schema = makeExecutableSchema({ typedefs, resolvers });
const query = `
{
hello
}
`;
const { parse } = require("graphql");
const document = parse(query);
const { compileQuery, isCompiledQuery } = require("graphql-jit");
const compiledQuery = compileQuery(schema, document);
// check if the compilation is successful
if (!isCompiledQuery(compiledQuery)) {
console.error(compiledQuery);
throw new Error("Error compiling query");
}
const executionResult = await compiledQuery.query();
console.log(executionResult);
Compiles the document
AST, using an optional operationName and compiler options.
-
schema
{GraphQLSchema} -graphql-js
schema object -
document
{DocumentNode} - document query AST ,can be obtained byparse
fromgraphql-js
-
operationName
{string} - optional operation name in case the document contains multiple queries/operations. -
compilerOptions
{Object} - Configurable options on the agent pooldisableLeafSerialization
{boolean, default: false} - disables leaf node serializers. The serializers validate the content of the field so this option should only be set to true if there are strong assurances that the values are valid.customSerializers
{Object as Map, default: {}} - Replace serializer functions for specific types. Can be used as a safer alternative for overly expensivecustomJSONSerializer
{boolean, default: false} - Whether to produce also a JSON serializer function usingfast-json-stringify
, otherwise the stringify function is justJSON.stringify
the compiled function that can be called with a root value, a context and the required variables.
the compiled function for producing a JSON string. It will be JSON.stringify
unless compilerOptions.customJSONSerializer
is true.
The value argument should the return of the compiled GraphQL function.
MIT