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
NameTypeDefaultDescription
bodystringThe GraphQL source text.
namestring'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
TypeDescription
Source

Members

NameTypeDescription
bodystringThe GraphQL source text.
namestringName 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

NameTypeDescription
sourceSourceThe source document that contains the position.
positionnumberThe UTF-8 character offset in the source body.

Returns

TypeDescription
SourceLocationThe 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

NameTypeDescription
locationLocationThe AST location to print.

Returns

TypeDescription
stringA 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

NameTypeDescription
sourceSourceThe source document that contains the location.
sourceLocationSourceLocationThe 1-indexed line and column to print.

Returns

TypeDescription
stringA 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

NameTypeDescription
linenumberOne-indexed line number in the source document.
columnnumberOne-indexed column number in the source document.