graphql/utilities/operations

Functions

getOperationAST()

Returns an operation AST given a document AST and optionally an operation name. If a name is not provided, an operation is only returned if only one is provided in the document.

Signature:

getOperationAST(documentAST: DocumentNode, operationName?: null | undefined | string): null | undefined | OperationDefinitionNode

Arguments

NameTypeDescription
documentASTDocumentNodeThe parsed GraphQL document AST.
operationName?null | undefined | stringThe optional operation name to select.

Returns

TypeDescription
null | undefined | OperationDefinitionNodeThe resolved operation ast.

Example

import { parse } from 'graphql/language';
import { getOperationAST } from 'graphql/utilities';
 
const document = parse('query GetName { name }');
const operation = getOperationAST(document, 'GetName');
 
// operation?.name?.value: 'GetName'

getOperationRootType()

Deprecated in v16

Extracts the root type of the operation from the schema. This helper is retained for backwards compatibility; call GraphQLSchema.getRootType instead because getOperationRootType will be removed in v17.

Signature:

getOperationRootType(schema: GraphQLSchema, operation: OperationDefinitionNode | OperationTypeDefinitionNode): GraphQLObjectType

Arguments

NameTypeDescription
schemaGraphQLSchemaThe GraphQL schema to use.
operationOperationDefinitionNode | OperationTypeDefinitionNodeThe operation definition to inspect.

Returns

TypeDescription
GraphQLObjectTypeThe resolved operation root type.

Example

import { buildSchema, getOperationRootType } from 'graphql/utilities';
import { parse } from 'graphql/language';
 
const schema = buildSchema('type Query { name: String }');
const operation = parse('{ name }').definitions[0];
const rootType = getOperationRootType(schema, operation);
 
// rootType.name: 'Query'