graphql/utilities/values

Functions

astFromValue()

Produces a GraphQL Value AST given a JavaScript object. Function will match JavaScript/JSON values to GraphQL AST schema format by using suggested GraphQLInputType. For example:

astFromValue(“value”, GraphQLString)

A GraphQL type must be provided, which will be used to interpret different JavaScript values.

JSON ValueGraphQL Value
ObjectInput Object
ArrayList
BooleanBoolean
StringString / Enum Value
NumberInt / Float
UnknownEnum Value
nullNullValue

Signature:

astFromValue(value: unknown, type: GraphQLInputType): null | undefined | ValueNode

Arguments

NameTypeDescription
valueunknownThe runtime value to convert.
typeGraphQLInputTypeThe GraphQL type to inspect.

Returns

TypeDescription
null | undefined | ValueNodeA GraphQL value AST for the provided JavaScript value, or null when no literal can represent it.

Example

import { astFromValue } from 'graphql/utilities';
 
const result = astFromValue(value, type);
 
// result contains the astFromValue return value

coerceInputValue()

Coerces a JavaScript value given a GraphQL Input Type.

Signature:

coerceInputValue(inputValue: unknown, type: GraphQLInputType, onError: (path: ReadonlyArray<string | number>, invalidValue: unknown, error: GraphQLError) => void): unknown

Arguments

NameTypeDefaultDescription
inputValueunknownThe runtime input value to coerce.
typeGraphQLInputTypeThe GraphQL type to inspect.
onError(path: ReadonlyArray<string | number>, invalidValue: unknown, error: GraphQLError) => voiddefaultOnErrorThe callback invoked for each coercion error.

Returns

TypeDescription
unknownThe coerced value, or undefined if coercion failed and errors were reported.

Example

import { coerceInputValue } from 'graphql/utilities';
 
const result = coerceInputValue(inputValue, type);
 
// result contains the coerceInputValue return value

typeFromAST()

Overload 1

Given a Schema and an AST node describing a type, return a GraphQLType definition which applies to that type. For example, if provided the parsed AST node for [User], a GraphQLList instance will be returned, containing the type called “User” found in the schema. If a type called “User” is not found in the schema, then undefined will be returned.

Signature:

typeFromAST(schema: GraphQLSchema, typeNode: NamedTypeNode): GraphQLNamedType | undefined
Arguments
NameTypeDescription
schemaGraphQLSchemaThe GraphQL schema to use.
typeNodeNamedTypeNodeThe GraphQL type AST node to resolve.
Returns
TypeDescription
GraphQLNamedType | undefinedThe GraphQL type referenced by the AST node, or undefined if it cannot be resolved.
Example
import { GraphQLString, typeFromAST } from 'graphql/utilities';
import { parseType } from 'graphql/language';
 
typeFromAST(schema, parseType('String'));
 
// GraphQLString

Overload 2

Resolves a list type AST node against a schema.

Signature:

typeFromAST(schema: GraphQLSchema, typeNode: ListTypeNode): GraphQLList<any> | undefined
Arguments
NameTypeDescription
schemaGraphQLSchemaThe GraphQL schema to use.
typeNodeListTypeNodeThe list type AST node to resolve.
Returns
TypeDescription
GraphQLList<any> | undefinedThe GraphQL list type referenced by the AST node, or undefined if
it cannot be resolved.

Overload 3

Resolves a non-null type AST node against a schema.

Signature:

typeFromAST(schema: GraphQLSchema, typeNode: NonNullTypeNode): GraphQLNonNull<any> | undefined
Arguments
NameTypeDescription
schemaGraphQLSchemaThe GraphQL schema to use.
typeNodeNonNullTypeNodeThe non-null type AST node to resolve.
Returns
TypeDescription
GraphQLNonNull<any> | undefinedThe GraphQL non-null type referenced by the AST node, or undefined
if it cannot be resolved.

Overload 4

Resolves a type AST node against a schema.

Signature:

typeFromAST(schema: GraphQLSchema, typeNode: TypeNode): GraphQLType | undefined
Arguments
NameTypeDescription
schemaGraphQLSchemaThe GraphQL schema to use.
typeNodeTypeNodeThe GraphQL type AST node to resolve.
Returns
TypeDescription
GraphQLType | undefinedThe GraphQL type referenced by the AST node, or undefined if it
cannot be resolved.

valueFromAST()

Produces a JavaScript value given a GraphQL Value AST.

A GraphQL type must be provided, which will be used to interpret different GraphQL Value literals.

Returns undefined when the value could not be validly coerced according to the provided type.

GraphQL ValueJSON Value
Input ObjectObject
ListArray
BooleanBoolean
StringString
Int / FloatNumber
Enum ValueUnknown
NullValuenull

Signature:

valueFromAST(valueNode: null | undefined | ValueNode, type: GraphQLInputType, variables?: null | undefined | object): unknown

Arguments

NameTypeDescription
valueNodenull | undefined | ValueNodeThe GraphQL value AST node to convert.
typeGraphQLInputTypeThe GraphQL type to inspect.
variables?null | undefined | objectOptional runtime variable values keyed by variable name.

Returns

TypeDescription
unknownThe coerced JavaScript value, or undefined if the AST value cannot be coerced to the type.

Example

import { valueFromAST } from 'graphql/utilities';
 
const result = valueFromAST(valueNode, type);
 
// result contains the valueFromAST return value

valueFromASTUntyped()

Produces a JavaScript value given a GraphQL Value AST.

Unlike valueFromAST(), no type is provided. The resulting JavaScript value will reflect the provided GraphQL value AST.

GraphQL ValueJavaScript Value
Input ObjectObject
ListArray
BooleanBoolean
String / EnumString
Int / FloatNumber
Nullnull

Signature:

valueFromASTUntyped(valueNode: ValueNode, variables?: null | undefined | object): unknown

Arguments

NameTypeDescription
valueNodeValueNodeThe GraphQL value AST node to convert.
variables?null | undefined | objectOptional runtime variable values keyed by variable name.

Returns

TypeDescription
unknownThe JavaScript value represented by the GraphQL value AST.

Example

import { parseValue } from 'graphql/language';
import { valueFromASTUntyped } from 'graphql/utilities';
 
const value = valueFromASTUntyped(parseValue('[1, 2, 3]'));
 
// value: [1, 2, 3]