Course outline

GraphQL / Intermediate: policy, sources and clients / Lesson 14

Custom scalars and coercion contracts

Document and test a custom scalar’s accepted inputs and serialized output across client and server boundaries.

About 3 min readContent reviewed
On this page

Lesson goals

  • Explain input and result coercion for a custom scalar.
  • Reference a scalar specification in a schema.
  • Test invalid literals, variables and serialized values.

Define the scalar meaning

A custom scalar represents a leaf value with application-specific coercion rules. Naming it DateTime or Money does not by itself define accepted syntax, precision, timezone behavior or error handling. Publish a stable specification and choose semantics that clients in different languages can implement. A scalar validates a value’s representation, not whether the caller is authorized to use it or whether it satisfies every domain rule.

Reference a specification

The schema fragment declares DateTime with a specifiedBy URL for a published scalar specification. The directive documents the contract; it does not automatically implement its parser or serializer. An actual server must register an implementation whose input and result behavior matches the referenced specification. Before adopting a specification, inspect its details, including required precision and offset rules, rather than assuming every date-time string is valid.

graphql Declare a scalar with a public specification
scalar DateTime @specifiedBy(url: "https://scalars.graphql.org/andimarek/date-time.html")

The directive documents the chosen specification. A compatible server implementation is still required.

Check variables and literals

The operation fragment sends a DateTime variable to an assumed schedule mutation. Test the same intended value as a variable and as a literal, plus invalid shapes and strings, according to the server implementation. Both input paths must conform to the declared scalar contract. A failed coercion can prevent execution; verify resolver invocation and durable state rather than guessing from the response status alone.

graphql Supply a custom scalar through a variable
mutation Schedule($at: DateTime!) {
  schedule(at: $at) { id }
}
# Variables: { "at": "2026-11-05T12:00:00.000Z" }

The schedule mutation and scalar implementation are assumed. The value must satisfy the chosen specification.

Validate result serialization

A server also needs a defined serialized representation for values returned by resolvers. Test valid internal values and a controlled invalid value, inspecting data, errors and null propagation under the schema’s nullability. Avoid silently rounding monetary or temporal data without a documented rule. Changing scalar semantics can break clients even when the schema name remains unchanged, so treat it as a contract change.

Build cross-client evidence

Use synthetic examples from the chosen specification to test valid boundaries, malformed inputs and round trips through the actual server. Exercise both literal and variable forms, and verify that clients in relevant languages interpret the serialized result consistently. Include authorization and domain checks separately. A schema containing specifiedBy is documentation evidence, not proof that the implementation follows it.

Common errors and fixes

A scalar name is assumed to specify its format

Why it happens
The application relied on an ambiguous name without a contract.
How to fix it
Publish or reference precise coercion semantics.
Prevent it next time
Test valid and invalid boundary examples.

Variables and literals behave differently

Why it happens
The implementation validates only one input path.
How to fix it
Implement and test both paths against the same specification.
Prevent it next time
Keep paired literal and variable fixtures.

An invalid result leaks internal details

Why it happens
A serializer exception was exposed directly.
How to fix it
Map errors safely and respect schema nullability.
Prevent it next time
Test controlled serialization failures and response fields.

2 practice exercises

1. Does adding @specifiedBy implement DateTime validation?

Show worked solution

No. It points to a specification. The server implementation must enforce matching input and result coercion, and tests should cover literals, variables and serialized output.

2. Why can changing date-time precision break clients without changing the scalar name?

Show worked solution

Clients may parse, compare or store values according to the previous precision contract. Treat the semantic change as an API change, test consumers and provide a migration path.

Key takeaways

  • A custom scalar needs precise input and output semantics.
  • A specification URL documents the contract; implementation and tests enforce it.

Official references