Release Notes
Minor Changes
#13274
7b10078Thanks @jerelmiller! - AddsScalar.fromGraphQLScalarTypehelper to create aScalarinstance from an existing graphql.jsGraphQLScalarType.import { GraphQLScalarType } from "graphql"; import { Scalar } from "@apollo/client"; const dateTimeScalarType = new GraphQLScalarType<Date, string>({ // ... }); const dateTimeScalar = Scalar.fromGraphQLScalarType(dateTimeScalarType, { is: (value) => value instanceof Date, });#13252
ed86234Thanks @jerelmiller! - Adds the plumbing and types implementation for declaring custom scalars and configuring custom scalars inInMemoryCache.You can declare custom scalar types with declaration merging on the
ApolloCache.Scalarsinterface:// apollo.d.ts import "@apollo/client"; declare module "@apollo/client" { namespace ApolloCache { interface Scalars { Date: { serialized: string; parsed: Date }; } } }This enables the
scalarsoption inInMemoryCache:import { Scalar } from "@apollo/client"; const cache = new InMemoryCache({ scalars: { Date: new Scalar({ parse: (dateString) => new Date(dateString), serialize: (date) => date.toISOString(), is: (value) => value instanceof Date, }), }, });#13259
ccaf686Thanks @jerelmiller! - Adds ascalaroption toInMemoryCachefield policies that tells the cache which scalar to use when parsing or serializing the field value.import { Scalar } from "@apollo/client"; new InMemoryCache({ scalars: { DateTime: new Scalar({ parse: (dateString) => new Date(dateString), serialize: (date) => date.toISOString(), }), }, typePolicies: { Event: { fields: { startTime: { // Parse this field using the DateTime scalar scalar: "DateTime", }, }, }, }, });This scalar definition is now used to properly parse or serialize the field value for cache reads and writes as well as
cache.extract()andcache.restore().#13273
0886de1Thanks @jerelmiller! - Automatically serialize variables that include custom scalar values. This includes cache reads and writes as well as requests to the network.For more complex input objects, a new
inputObjectsoption is available toInMemoryCachethat specifies where nested scalar fields are found.const cache = new InMemoryCache({ scalars: { DateTime: new Scalar({ parse: (value) => new Date(value), serialize: (value) => value.toISOString(), is: (value) => value instanceof Date, }), }, inputObjects: { EventFilter: { fields: { date: "DateTime", }, }, }, }); const client = new ApolloClient({ cache, link }); await client.query({ query: gql` query Event($filter: EventFilter!) { event(filter: $filter) { name } } `, variables: { filter: { date: new Date("2026-01-01T00:00:00.000Z"), }, }, }); // The link receives: // { filter: { date: "2026-01-01T00:00:00.000Z" } }#13252
ed86234Thanks @jerelmiller! - Adds thegetScalarabstract method toApolloCachethat cache subclasses override to provide scalar behavior to Apollo Client. Defaults to unconditionally returnundefinedif not specified.