• Stars
    star
    1,025
  • Rank 43,021 (Top 0.9 %)
  • Language
    TypeScript
  • License
    Other
  • Created over 5 years ago
  • Updated 28 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GraphQL execution using a JIT compiler

GraphQL JIT

npm 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

GraphQL-JS 16 on Node 16.13.0

$ yarn benchmark skip-json
Starting introspection
graphql-js x 1,941 ops/sec ±2.50% (225 runs sampled)
graphql-jit x 6,158 ops/sec ±2.38% (222 runs sampled)
Starting fewResolvers
graphql-js x 26,620 ops/sec ±2.41% (225 runs sampled)
graphql-jit x 339,223 ops/sec ±2.94% (215 runs sampled)
Starting manyResolvers
graphql-js x 16,415 ops/sec ±2.36% (220 runs sampled)
graphql-jit x 178,331 ops/sec ±2.73% (221 runs sampled)
Starting nestedArrays
graphql-js x 127 ops/sec ±1.43% (220 runs sampled)
graphql-jit x 1,316 ops/sec ±2.58% (219 runs sampled)
Done in 141.25s.

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.

More details here - GraphQL-JS.md

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-tools/schema");
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

More Repositories

1

kopf

A Python framework to write Kubernetes operators in just few lines of code.
Python
971
star
2

kubernetes-on-aws

Deploying Kubernetes on AWS with CloudFormation and Ubuntu
Go
614
star
3

kube-metrics-adapter

General purpose metrics adapter for Kubernetes HPA metrics
Go
482
star
4

kube-ingress-aws-controller

Configures AWS Load Balancers according to Kubernetes Ingress resources
Go
374
star
5

es-operator

Kubernetes Operator for Elasticsearch
Go
351
star
6

hexo-theme-doc

A documentation theme for the Hexo blog framework
JavaScript
243
star
7

cluster-lifecycle-manager

Cluster Lifecycle Manager (CLM) to provision and update multiple Kubernetes clusters
Go
227
star
8

docker-locust

Docker image for the Locust.io open source load testing tool
Python
201
star
9

remora

Kafka consumer lag-checking application for monitoring, written in Scala and Akka HTTP; a wrap around the Kafka consumer group command. Integrations with Cloudwatch and Datadog. Authentication recently added
Scala
197
star
10

stackset-controller

Opinionated StackSet resource for managing application life cycle and traffic switching in Kubernetes
Go
168
star
11

tessellate

Server-side React render service.
JavaScript
152
star
12

kube-aws-iam-controller

Distribute different AWS IAM credentials to different pods in Kubernetes via secrets.
Go
152
star
13

transformer

A tool to transform/convert web browser sessions (HAR files) into Locust load testing scenarios (locustfile).
Python
98
star
14

bro-q

Chrome Extension for JSON formatting and jq filtering in your browser.
TypeScript
83
star
15

spark-json-schema

JSON schema parser for Apache Spark
Scala
79
star
16

catwatch

A metrics dashboard for GitHub organizations, with results accessible via REST API
Java
59
star
17

authmosphere

A library to support OAuth2 workflows in JavaScript projects
TypeScript
54
star
18

flatjson

A fast JSON parser (and builder)
Java
45
star
19

banknote

A simple JavaScript libary for formatting currency amounts according to Unicode CLDR standards
JavaScript
45
star
20

perron

A sane node.js client for web services
JavaScript
43
star
21

zelt

A command-line tool for orchestrating the deployment of Locust in Kubernetes.
Python
36
star
22

hexo-theme-doc-seed

skeleton structure for a documentation website using Hexo and the hexo-doc-theme
29
star
23

kubernetes-log-watcher

Kubernetes log watcher for Scalyr and AppDynamics
Python
27
star
24

new-project

Template to use when creating a new open source project. It comes with all the standard files which there is expected to be in an open source project on Github.
23
star
25

darty

Data dependency manager
Python
22
star
26

chisel

⚒️ collection of awesome practices for putting things on pedestal
Clojure
20
star
27

fabric-gateway

An API Gateway built on the Skipper Ingress Controller https://github.com/zalando/skipper
Scala
17
star
28

roadblock

A node.js application for pulling github organisation statistics into a database.
JavaScript
16
star
29

ember-dressy-table

An ember addon for dynamic tables
JavaScript
10
star
30

zalando.github.io-dev

The zalando.github.io open-source metrics dashboard
JavaScript
10
star
31

atlas-js-core

JavaScript SDK Core for Zalando Checkout, Guest Checkout, and Catalog APIs
JavaScript
9
star
32

opentracing-sqs-java

An attempt at a simple SQS helper library for OpenTracing support.
Java
8
star
33

clin

Cli for Nakadi for event types and subscriptions management
Python
7
star
34

play-etcd-watcher

Instantaneous etcd directory listener for Scala Play
Scala
6
star
35

Zincr

Zincr is a Github bot built with Probot to enforce approvals, specification and licensing checks
TypeScript
5
star
36

jzon

Apis for working with json
Java
5
star
37

Trafficlight

Node.js CLI for creating and migrating Github projects, ensuring that it follows a consistent model for permissions, teams and boilerplate files.
JavaScript
1
star