graphql/language/source
Classes
Source
A representation of source input to GraphQL. The name and locationOffset parameters are
optional, but they are useful for clients who store GraphQL documents in source files.
For example, if the GraphQL input starts at line 40 in a file named Foo.graphql, it might
be useful for name to be "Foo.graphql" and location to be { line: 40, column: 1 }.
The line and column properties in locationOffset are 1-indexed.
Constructor
Creates a Source instance.
Signature:
new Source(body: string, name: string, locationOffset: { line: number; column: number })
Arguments
| Name | Type | Default | Description |
|---|---|---|---|
| body | string | The GraphQL source text. | |
| name | string | 'GraphQL request' | Name used in diagnostics for this source. |
| locationOffset | { line: number; column: number } | { line: 1, column: 1 } | One-indexed line and column where this source begins. |
Returns
| Type | Description |
|---|---|
Source |
Members
| Name | Type | Description |
|---|---|---|
| body | string | The GraphQL source text. |
| name | string | Name used in diagnostics for this source, such as a file path or request name. |
| locationOffset | { line: number; column: number } | One-indexed line and column where this source begins. |
Functions
getLocation()
Takes a Source and a UTF-8 character offset, and returns the corresponding line and column as a SourceLocation.
Signature:
getLocation(source: Source, position: number): SourceLocation
Arguments
| Name | Type | Description |
|---|---|---|
| source | Source | The source document that contains the position. |
| position | number | The UTF-8 character offset in the source body. |
Returns
| Type | Description |
|---|---|
SourceLocation | The 1-indexed line and column for the given source position. |
Example
import { Source, getLocation } from 'graphql/language';
const source = new Source('type Query { hello: String }');
const location = getLocation(source, 13);
// location: { line: 1, column: 14 }printLocation()
Render a helpful description of the location in the GraphQL Source document.
Signature:
printLocation(location: Location): string
Arguments
| Name | Type | Description |
|---|---|---|
| location | Location | The AST location to print. |
Returns
| Type | Description |
|---|---|
string | A formatted source excerpt with line and column information. |
Example
import { parse, printLocation } from 'graphql/language';
const document = parse('type Query { hello: String }');
const location = document.definitions[0].loc;
if (location) {
const printed = printLocation(location);
// printed:
// GraphQL request:1:1
// 1 | type Query { hello: String }
// | ^
}printSourceLocation()
Render a helpful description of the location in the GraphQL Source document.
Signature:
printSourceLocation(source: Source, sourceLocation: SourceLocation): string
Arguments
| Name | Type | Description |
|---|---|---|
| source | Source | The source document that contains the location. |
| sourceLocation | SourceLocation | The 1-indexed line and column to print. |
Returns
| Type | Description |
|---|---|
string | A formatted source excerpt with line and column information. |
Example
import { Source, printSourceLocation } from 'graphql/language';
const source = new Source('type Query { hello: String }');
const printed = printSourceLocation(source, { line: 1, column: 14 });
// printed:
// GraphQL request:1:14
// 1 | type Query { hello: String }
// | ^Types
SourceLocation
Interface. Represents a location in a Source.
Members
| Name | Type | Description |
|---|---|---|
| line | number | One-indexed line number in the source document. |
| column | number | One-indexed column number in the source document. |