graphql/language/parsing
Functions
parse()
Given a GraphQL source, parses it into a Document. Throws GraphQLError if a syntax error is encountered.
Signature:
parse(source: string | Source, options?: ParseOptions): DocumentNode
Arguments
| Name | Type | Description |
|---|---|---|
| source | string | Source | A GraphQL source string or source object. |
| options? | ParseOptions | Optional parser configuration. |
Returns
| Type | Description |
|---|---|
DocumentNode | The parsed GraphQL document AST. |
Example
import { parse } from 'graphql/language';
const document = parse('{ hero { name } }');
// document.kind: 'Document'parseValue()
Given a string containing a GraphQL value (ex. [42]), parse the AST for
that value.
Throws GraphQLError if a syntax error is encountered.
This is useful within tools that operate upon GraphQL Values directly and in isolation of complete GraphQL documents.
Consider providing the results to the utility function: valueFromAST().
Signature:
parseValue(source: string | Source, options?: ParseOptions): ValueNode
Arguments
| Name | Type | Description |
|---|---|---|
| source | string | Source | A GraphQL source string or source object containing a value. |
| options? | ParseOptions | Optional parser configuration. |
Returns
| Type | Description |
|---|---|
ValueNode | The parsed GraphQL value AST. |
Example
import { parseValue } from 'graphql/language';
const value = parseValue('[42]');
// value.kind: 'ListValue'parseConstValue()
Similar to parseValue(), but raises a parse error if it encounters a variable. The return type will be a constant value.
Signature:
parseConstValue(source: string | Source, options?: ParseOptions): ConstValueNode
Arguments
| Name | Type | Description |
|---|---|---|
| source | string | Source | A GraphQL source string or source object containing a constant value. |
| options? | ParseOptions | Optional parser configuration. |
Returns
| Type | Description |
|---|---|
ConstValueNode | The parsed GraphQL constant value AST. |
Example
import { parseConstValue } from 'graphql/language';
const value = parseConstValue('{ enabled: true }');
// value.kind: 'ObjectValue'parseType()
Given a string containing a GraphQL Type (ex. [Int!]), parse the AST for
that type.
Throws GraphQLError if a syntax error is encountered.
This is useful within tools that operate upon GraphQL Types directly and in isolation of complete GraphQL documents.
Consider providing the results to the utility function: typeFromAST().
Signature:
parseType(source: string | Source, options?: ParseOptions): TypeNode
Arguments
| Name | Type | Description |
|---|---|---|
| source | string | Source | A GraphQL source string or source object containing a type reference. |
| options? | ParseOptions | Optional parser configuration. |
Returns
| Type | Description |
|---|---|
TypeNode | The parsed GraphQL type AST. |
Example
import { parseType } from 'graphql/language';
const type = parseType('[String!]');
// type.kind: 'ListType'parseSchemaCoordinate()
Given a string containing a GraphQL Schema Coordinate (ex. Type.field),
parse the AST for that schema coordinate.
Throws GraphQLError if a syntax error is encountered.
Consider providing the results to the utility function: resolveASTSchemaCoordinate(). Or calling resolveSchemaCoordinate() directly with an unparsed source.
Signature:
parseSchemaCoordinate(source: string | Source): SchemaCoordinateNode
Arguments
| Name | Type | Description |
|---|---|---|
| source | string | Source | A GraphQL source string or source object containing a schema coordinate. |
Returns
| Type | Description |
|---|---|
SchemaCoordinateNode | The parsed GraphQL schema coordinate AST. |
Example
import { parseSchemaCoordinate } from 'graphql/language';
const coordinate = parseSchemaCoordinate('Query.hero');
// coordinate.kind: 'MemberCoordinate'Types
ParseOptions
Interface. Configuration options to control parser behavior
Members
| Name | Type | Description |
|---|---|---|
| noLocation? | boolean | By default, the parser creates AST nodes that know the location in the source that they correspond to. This configuration flag disables that behavior for performance or testing. |
| maxTokens? | number | Parser CPU and memory usage is linear to the number of tokens in a document however in extreme cases it becomes quadratic due to memory exhaustion. Parsing happens before validation so even invalid queries can burn lots of CPU time and memory. To prevent this you can set a maximum number of tokens allowed within a document. |
| allowLegacyFragmentVariables? | boolean | Allows legacy fragment variable definitions to be parsed. |
| experimentalDirectivesOnDirectiveDefinitions? | boolean | EXPERIMENTAL: If enabled, the parser will parse directives on directive definitions. This syntax is not part of the GraphQL specification and may change. graphql directive @foo @bar on FIELD |
| lexer? | { source: Source; lastToken: Token; token: Token; line: number; lineStart: number; advance: () => Token; lookahead: () => Token } | You may override the Lexer class used to lex the source; this is used by schema coordinates to introduce a lexer with a restricted syntax. |