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 Value | GraphQL Value |
|---|---|
| Object | Input Object |
| Array | List |
| Boolean | Boolean |
| String | String / Enum Value |
| Number | Int / Float |
| Unknown | Enum Value |
| null | NullValue |
Signature:
astFromValue(value: unknown, type: GraphQLInputType): null | undefined | ValueNode
Arguments
| Name | Type | Description |
|---|---|---|
| value | unknown | The runtime value to convert. |
| type | GraphQLInputType | The GraphQL type to inspect. |
Returns
| Type | Description |
|---|---|
null | undefined | ValueNode | A 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 valuecoerceInputValue()
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
| Name | Type | Default | Description |
|---|---|---|---|
| inputValue | unknown | The runtime input value to coerce. | |
| type | GraphQLInputType | The GraphQL type to inspect. | |
| onError | (path: ReadonlyArray<string | number>, invalidValue: unknown, error: GraphQLError) => void | defaultOnError | The callback invoked for each coercion error. |
Returns
| Type | Description |
|---|---|
unknown | The 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 valuetypeFromAST()
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
| Name | Type | Description |
|---|---|---|
| schema | GraphQLSchema | The GraphQL schema to use. |
| typeNode | NamedTypeNode | The GraphQL type AST node to resolve. |
Returns
| Type | Description |
|---|---|
GraphQLNamedType | undefined | The 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'));
// GraphQLStringOverload 2
Resolves a list type AST node against a schema.
Signature:
typeFromAST(schema: GraphQLSchema, typeNode: ListTypeNode): GraphQLList<any> | undefined
Arguments
| Name | Type | Description |
|---|---|---|
| schema | GraphQLSchema | The GraphQL schema to use. |
| typeNode | ListTypeNode | The list type AST node to resolve. |
Returns
| Type | Description |
|---|---|
GraphQLList<any> | undefined | The 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
| Name | Type | Description |
|---|---|---|
| schema | GraphQLSchema | The GraphQL schema to use. |
| typeNode | NonNullTypeNode | The non-null type AST node to resolve. |
Returns
| Type | Description |
|---|---|
GraphQLNonNull<any> | undefined | The 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
| Name | Type | Description |
|---|---|---|
| schema | GraphQLSchema | The GraphQL schema to use. |
| typeNode | TypeNode | The GraphQL type AST node to resolve. |
Returns
| Type | Description |
|---|---|
GraphQLType | undefined | The 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 Value | JSON Value |
|---|---|
| Input Object | Object |
| List | Array |
| Boolean | Boolean |
| String | String |
| Int / Float | Number |
| Enum Value | Unknown |
| NullValue | null |
Signature:
valueFromAST(valueNode: null | undefined | ValueNode, type: GraphQLInputType, variables?: null | undefined | object): unknown
Arguments
| Name | Type | Description |
|---|---|---|
| valueNode | null | undefined | ValueNode | The GraphQL value AST node to convert. |
| type | GraphQLInputType | The GraphQL type to inspect. |
| variables? | null | undefined | object | Optional runtime variable values keyed by variable name. |
Returns
| Type | Description |
|---|---|
unknown | The 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 valuevalueFromASTUntyped()
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 Value | JavaScript Value |
|---|---|
| Input Object | Object |
| List | Array |
| Boolean | Boolean |
| String / Enum | String |
| Int / Float | Number |
| Null | null |
Signature:
valueFromASTUntyped(valueNode: ValueNode, variables?: null | undefined | object): unknown
Arguments
| Name | Type | Description |
|---|---|---|
| valueNode | ValueNode | The GraphQL value AST node to convert. |
| variables? | null | undefined | object | Optional runtime variable values keyed by variable name. |
Returns
| Type | Description |
|---|---|
unknown | The 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]