GraphQL JIT
This is a fork of zalando-incubator/graphql-jit that features the following improvements:
- Support for GraphQL Subscription
- Custom JSON Stringify factory function
- Minimal dependencies for lightweight bundle
If you do not have the above use cases, it is better to stick with the original repo. Check out the their README to learn more about graphql-jit
.
Install
yarn add @hoangvvo/graphql-jit
// or
npm i @hoangvvo/graphql-jit
Usage
import { compileQuery, isCompiledQuery } from "graphql-jit";
const query = `{ hello }`;
const document = parse(query);
const compiledQuery = compileQuery(schema, document);
if (!isCompiledQuery(compiledQuery)) {
console.error(compiledQuery);
throw new Error("Error compiling query");
}
#### Execute the Query
```js
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 serializerscustomJSONSerializer
{function, default: undefined} - A function to be called with CompilationContext
to produce also a JSON serializer function. 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 a function.
The value argument should be the return of the compiled GraphQL function.
LICENSE
MIT