Skip to main content

GraphQLScalarType <TInternal, TExternal>

Scalar Type Definition

The leaf values of any request and input values to arguments are Scalars (or Enums) and are defined with a name and a series of functions used to parse input from ast or variables and to ensure validity.

If a type's coerceOutputValue function returns null or does not return a value (i.e. it returns undefined) then an error will be raised and a null value will be returned in the response. It is always better to validate.

Example:

function ensureOdd(value) {
if (!Number.isFinite(value)) {
throw new Error(
`Scalar "Odd" cannot represent "${value}" since it is not a finite number.`,
);
}

if (value % 2 === 0) {
throw new Error(`Scalar "Odd" cannot represent "${value}" since it is even.`);
}
}

const OddType = new GraphQLScalarType({
name: 'Odd',
coerceOutputValue(value) {
return ensureOdd(value);
},
coerceInputValue(value) {
return ensureOdd(value);
}
valueToLiteral(value) {
return parse(`${ensureOdd(value)`);
}
});

Custom scalars behavior is defined via the following functions:

  • coerceOutputValue(value): Implements "Result Coercion". Given an internal value, produces an external value valid for this type. Returns undefined or throws an error to indicate invalid values.
  • coerceInputValue(value): Implements "Input Coercion" for values. Given an external value (for example, variable values), produces an internal value valid for this type. Returns undefined or throws an error to indicate invalid values.
  • coerceInputLiteral(ast): Implements "Input Coercion" for constant literals. Given an GraphQL literal (AST) (for example, an argument value), produces an internal value valid for this type. Returns undefined or throws an error to indicate invalid values.
  • valueToLiteral(value): Converts an external value to a GraphQL literal (AST). Returns undefined or throws an error to indicate invalid values.

Deprecated, to be removed in v18:

  • serialize(value): Implements "Result Coercion". Renamed to coerceOutputValue().
  • parseValue(value): Implements "Input Coercion" for values. Renamed to coerceInputValue().
  • parseLiteral(ast): Implements "Input Coercion" for literals including non-specified replacement of variables embedded within complex scalars. Replaced by the combination of the replaceVariables() utility and the coerceInputLiteral() method.

Implements

  • GraphQLSchemaElement

Index

Constructors

constructor

Properties

astNode

astNode: Maybe<ScalarTypeDefinitionNode>

coerceInputLiteral

coerceInputLiteral: undefined | GraphQLScalarInputLiteralCoercer<TInternal>

coerceInputValue

coerceInputValue: GraphQLScalarInputValueCoercer<TInternal>

coerceOutputValue

coerceOutputValue: GraphQLScalarOutputValueCoercer<TExternal>

description

description: Maybe<string>

extensionASTNodes

extensionASTNodes: readonly ScalarTypeExtensionNode[]

extensions

extensions: Readonly<GraphQLScalarTypeExtensions>

name

name: string

parseLiteral

parseLiteral: GraphQLScalarLiteralParser<TInternal>
@deprecated

use replaceVariables() and coerceInputLiteral() instead, parseLiteral() will be removed in v18

parseValue

parseValue: GraphQLScalarValueParser<TInternal>
@deprecated

use coerceInputValue() instead, parseValue() will be removed in v18

serialize

serialize: GraphQLScalarSerializer<TExternal>
@deprecated

use coerceOutputValue() instead, serialize() will be removed in v18

specifiedByURL

specifiedByURL: Maybe<string>

valueToLiteral

valueToLiteral: undefined | GraphQLScalarValueToLiteral

Accessors

[toStringTag]

  • get [toStringTag](): string
  • Returns string

Methods

toConfig

  • toConfig(): GraphQLScalarTypeNormalizedConfig<TInternal, TExternal>
  • Returns GraphQLScalarTypeNormalizedConfig<TInternal, TExternal>

toJSON

  • toJSON(): string
  • Returns string

toString

  • toString(): string
  • Returns string