GraphQL JIT
Why?
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. graphql-jit leverages this behaviour of V8 optimization by compiling the queries into functions to significantly improve performance (See benchmarks below)
Benchmarks
$ NODE_ENV=production ts-node -T ./src/__benchmarks__/benchmarks.ts skip-jsonStarting introspectiongraphql-js x 1,155 ops/sec ±1.55% graphql-jit x 5,961 ops/sec ±5.34% Starting fewResolversgraphql-js x 14,313 ops/sec ±1.43% graphql-jit x 409,587 ops/sec ±1.08% Starting manyResolversgraphql-js x 13,201 ops/sec ±1.50% graphql-jit x 229,025 ops/sec ±1.18% Starting nestedArraysgraphql-js x 108 ops/sec ±1.30% graphql-jit x 1,317 ops/sec ±2.38% Done in 141.94s.Support for GraphQL spec
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.
Differences to graphql-js
In order to achieve better performance, the graphql-jit compiler introduces some limitations.
The primary limitation is that all computed properties must have a resolver and only these can return a Promise.
Install
yarn add graphql-jitExample
For complete working examples, check the examples/ directory
Create a schema
const typedefs = `type Query { hello: string}`;const resolvers = Query: { return ; } ; const makeExecutableSchema = ;const schema = ;Compile a Query
const query = `{ hello}`;const parse = ;const document = ; const compileQuery isCompiledQuery = ;const compiledQuery = ;// check if the compilation is successful if ! console; throw "Error compiling query";Execute the Query
const executionResult = await compiledQuery;console;API
compiledQuery = compileQuery(schema, document, operationName, compilerOptions)
Compiles the document AST, using an optional operationName and compiler options.
-
schema{GraphQLSchema} -graphqlschema object -
document{DocumentNode} - document query AST ,can be obtained byparsefromgraphql -
operationName{string} - optional operation name in case the document contains multiple operations(queries/mutations/subscription). -
compilerOptions{Object} - Configurable options for the compilerdisableLeafSerialization{boolean, default: false} - disables leaf node serializers. The serializers validate the content of the field at runtime 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 expensive serializerscustomJSONSerializer{boolean, default: false} - Whether to produce also a JSON serializer function usingfast-json-stringify. The default stringifier function isJSON.stringify
compiledQuery.compiled(root: any, context: any, variables: Maybe<{ [key: string]: any }>)
the compiled function that can be called with a root value, a context and the required variables.
compiledQuery.stringify(value: any)
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.
LICENSE
MIT

