Giter Club home page Giter Club logo

graphql-jit's Introduction

GraphQL JIT

Build Status codecov

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

$ yarn benchmark skip-json
Starting introspection
graphql-js x 1,155 ops/sec ±1.55% (215 runs sampled)
graphql-jit x 5,961 ops/sec ±5.34% (216 runs sampled)
Starting fewResolvers
graphql-js x 14,313 ops/sec ±1.43% (224 runs sampled)
graphql-jit x 409,587 ops/sec ±1.08% (216 runs sampled)
Starting manyResolvers
graphql-js x 13,201 ops/sec ±1.50% (216 runs sampled)
graphql-jit x 229,025 ops/sec ±1.18% (216 runs sampled)
Starting nestedArrays
graphql-js x 108 ops/sec ±1.30% (216 runs sampled)
graphql-jit x 1,317 ops/sec ±2.38% (213 runs sampled)
Done in 141.94s.

Support for GraphQL spec

The goal is to support the June 2018 version of the GraphQL spec.

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-jit

Example

For complete working examples, check the examples/ directory

Create a schema

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 });

Compile a Query

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");
}

Execute the Query

const executionResult = await compiledQuery.query(root, context, variables);
console.log(executionResult);

Subscribe to the Query

const result = await compiledQuery.subscribe(root, context, variables);
for await (const value of result) {
  console.log(value);
}

API

compiledQuery = compileQuery(schema, document, operationName, compilerOptions)

Compiles the document AST, using an optional operationName and compiler options.

  • schema {GraphQLSchema} - graphql schema object

  • document {DocumentNode} - document query AST ,can be obtained by parse from graphql

  • operationName {string} - optional operation name in case the document contains multiple operations(queries/mutations/subscription).

  • compilerOptions {Object} - Configurable options for the compiler

    • disableLeafSerialization {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 serializers
    • customJSONSerializer {boolean, default: false} - Whether to produce also a JSON serializer function using fast-json-stringify. The default stringifier function is JSON.stringify

compiledQuery.query(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.subscribe(root: any, context: any, variables: Maybe<{ [key: string]: any }>)

(available for GraphQL Subscription only) the compiled function that can be called with a root value, a context and the required variables to produce either an AsyncIterator (if successful) or an ExecutionResult (error).

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 be the return of the compiled GraphQL function.

LICENSE

MIT

graphql-jit's People

Contributors

addityasingh avatar airhorns avatar ajordanow avatar aldis-ameriks avatar ardatan avatar boopathi avatar dependabot[bot] avatar fjmoreno avatar frikille avatar hoangvvo avatar ivangoncharov avatar karihe avatar mikkeloscar avatar perploug avatar ponimas avatar ruiaraujo avatar topicus avatar vigneshshanmugam avatar wojtkiewicz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.