The core execution functions provide the main entry points for executing GraphQL operations against schemas. These functions orchestrate the entire GraphQL execution process including parsing, validation, and execution.
import {
graphql,
graphqlSync,
GraphQLSchema,
GraphQLError,
type GraphQLArgs,
type ExecutionResult,
type GraphQLFieldResolver,
type GraphQLTypeResolver
} from "graphql";Executes a GraphQL operation against a schema with full support for variables, context, and custom resolvers.
/**
* Execute a GraphQL operation against a schema
* @param args - Execution configuration
* @returns Promise resolving to execution result
*/
function graphql(args: GraphQLArgs): Promise<ExecutionResult>;
interface GraphQLArgs {
/** The GraphQL schema to execute against */
schema: GraphQLSchema;
/** GraphQL document string or Source object to execute */
source: string | Source;
/** Root value passed to top-level resolvers */
rootValue?: unknown;
/** Context value passed to all resolvers */
contextValue?: unknown;
/** Variable values for the operation */
variableValues?: Maybe<{ readonly [variable: string]: unknown }>;
/** Name of operation to execute (if document contains multiple operations) */
operationName?: Maybe<string>;
/** Custom field resolver function */
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
/** Custom type resolver function for abstract types */
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>;
}
interface ExecutionResult {
/** Array of errors that occurred during execution */
errors?: ReadonlyArray<GraphQLError>;
/** The data returned by the GraphQL operation */
data?: { [key: string]: unknown } | null;
/** Extension data provided by resolvers or directives */
extensions?: { [key: string]: unknown };
}Usage Examples:
import { graphql, buildSchema } from "graphql";
// Basic execution
const schema = buildSchema(`
type Query {
hello: String
user(id: ID!): User
}
type User {
id: ID!
name: String!
}
`);
const rootValue = {
hello: () => "Hello world!",
user: ({ id }) => ({ id, name: `User ${id}` })
};
const result = await graphql({
schema,
source: '{ hello, user(id: "123") { id, name } }',
rootValue
});
// With variables
const resultWithVars = await graphql({
schema,
source: 'query GetUser($userId: ID!) { user(id: $userId) { id, name } }',
rootValue,
variableValues: { userId: "456" }
});
// With context
const resultWithContext = await graphql({
schema,
source: '{ hello }',
rootValue,
contextValue: { userId: "current-user", permissions: ["read"] }
});Executes a GraphQL operation synchronously. All resolvers must return non-Promise values.
/**
* Execute a GraphQL operation synchronously
* @param args - Execution configuration
* @returns Execution result (no Promise)
* @throws If any resolver returns a Promise
*/
function graphqlSync(args: GraphQLArgs): ExecutionResult;Usage Examples:
import { graphqlSync, buildSchema } from "graphql";
const schema = buildSchema(`
type Query {
timestamp: String
random: Float
}
`);
const rootValue = {
timestamp: () => new Date().toISOString(),
random: () => Math.random()
};
// Synchronous execution - no await needed
const result = graphqlSync({
schema,
source: '{ timestamp, random }',
rootValue
});
console.log(result.data);
// { timestamp: "2023-01-01T12:00:00.000Z", random: 0.123456 }type GraphQLFieldResolver<TSource, TContext, TArgs = any, TReturn = any> = (
source: TSource,
args: TArgs,
context: TContext,
info: GraphQLResolveInfo
) => TReturn;
type GraphQLTypeResolver<TSource, TContext, TReturn = PromiseOrValue<string | undefined>> = (
value: TSource,
context: TContext,
info: GraphQLResolveInfo,
abstractType: GraphQLAbstractType
) => TReturn;
interface GraphQLResolveInfo {
/** The name of the field being resolved */
readonly fieldName: string;
/** The AST nodes for the field being resolved */
readonly fieldNodes: ReadonlyArray<FieldNode>;
/** The return GraphQL type for the field */
readonly returnType: GraphQLOutputType;
/** The parent GraphQL type for the field */
readonly parentType: GraphQLObjectType;
/** The response path for this field */
readonly path: ResponsePath;
/** The GraphQL schema */
readonly schema: GraphQLSchema;
/** Fragment definitions from the document */
readonly fragments: { [fragmentName: string]: FragmentDefinitionNode };
/** The root value */
readonly rootValue: unknown;
/** The operation being executed */
readonly operation: OperationDefinitionNode;
/** Variable values for the operation */
readonly variableValues: { [variableName: string]: unknown };
}
type ResponsePath = {
readonly prev: ResponsePath | undefined;
readonly key: string | number;
readonly typename: string | undefined;
};type Maybe<T> = T | null | undefined;
type PromiseOrValue<T> = Promise<T> | T;
type ObjMap<T> = { [key: string]: T };
interface Source {
readonly body: string;
readonly name: string;
readonly locationOffset: Location;
}
class Location {
readonly start: number;
readonly end: number;
readonly startToken: Token;
readonly endToken: Token;
readonly source: Source;
}