v16 APIgraphql

graphql

GraphQL.js provides a reference implementation for the GraphQL specification but is also a useful utility for operating on GraphQL files and building sophisticated tools.

This primary module exports a general purpose function for fulfilling all steps of the GraphQL specification in a single operation, but also includes utilities for every part of the GraphQL specification:

  • Parsing the GraphQL language.
  • Building a GraphQL type schema.
  • Validating a GraphQL request against a type schema.
  • Executing a GraphQL request against a type schema.

This also includes utility functions for operating on GraphQL types and GraphQL documents to facilitate building tools.

Most exports are also available from package submodules. For example, these two references resolve to the same parse function:

import { parse } from 'graphql';
import { parse } from 'graphql/language';

Functions

graphql()

Parses, validates, and executes a GraphQL document against a schema.

This is the primary entry point for fulfilling GraphQL operations. Use this when you want a single-call request lifecycle that returns a promise in all cases.

More sophisticated GraphQL servers, such as those which persist queries, may wish to separate the validation and execution phases to a static-time tooling step and a server runtime step.

Signature:

graphql(args: GraphQLArgs): Promise<ExecutionResult>

Arguments

NameTypeDescription
argsGraphQLArgsRequest execution arguments, including schema and source.

Returns

TypeDescription
Promise<ExecutionResult>A promise that resolves to an execution result or validation errors.

Example

import { graphql, buildSchema } from 'graphql';
 
const schema = buildSchema('type Query { hello: String }');
 
const result = await graphql({
  schema,
  source: '{ hello }',
  rootValue: { hello: 'world' },
});
 
// result: { data: { hello: 'world' } }

graphqlSync()

Parses, validates, and executes a GraphQL document synchronously.

This function guarantees that execution completes synchronously, or throws an error, assuming that all field resolvers are also synchronous. It throws when any resolver returns a promise.

Signature:

graphqlSync(args: GraphQLArgs): ExecutionResult

Arguments

NameTypeDescription
argsGraphQLArgsRequest execution arguments, including schema and source.

Returns

TypeDescription
ExecutionResultThe execution result.

Example

import { graphqlSync, buildSchema } from 'graphql';
 
const schema = buildSchema('type Query { hello: String }');
 
const result = graphqlSync({
  schema,
  source: '{ hello }',
  rootValue: { hello: 'world' },
});
 
// result: { data: { hello: 'world' } }

Constants

version

A string containing the version of the GraphQL.js library

string

versionInfo

An object containing the components of the GraphQL.js version string

Readonly<{ major: number; minor: number; patch: number; preReleaseTag: string | null }>

Types

GraphQLArgs

Interface. Describes the input object accepted by graphql and graphqlSync.

These arguments describe the full parse, validate, and execute lifecycle for a GraphQL request.

Members

NameTypeDescription
schemaGraphQLSchemaThe GraphQL type system to use when validating and executing a query.
sourcestring | SourceA GraphQL language formatted string or source object representing the requested operation.
rootValue?unknownThe value provided as the first argument to resolver functions on the top
level type, such as the query object type.
contextValue?unknownThe context value provided to resolver functions after field arguments.
This is used to pass shared information useful at any point during
execution, for example the currently logged in user and connections to
databases or other services.
variableValues?null | undefined | objectA mapping of variable name to runtime value for variables defined by the operation.
operationName?null | undefined | stringThe operation to execute when the source contains multiple possible
operations. This can be omitted when the source contains only one operation.
fieldResolver?null | undefined | GraphQLFieldResolver<any, any>A resolver function to use when one is not provided by the schema.
If not provided, the default field resolver is used, which looks for a value
or method on the source value with the field’s name.
typeResolver?null | undefined | GraphQLTypeResolver<any, any>A type resolver function to use when none is provided by the schema.
If not provided, the default type resolver is used, which looks for a
__typename field or alternatively calls the isTypeOf method.