Skip to content

Commit aba0960

Browse files
Version Packages (alpha) (#13297)
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to release-4.3, this PR will be updated. ⚠️⚠️⚠️⚠️⚠️⚠️ `release-4.3` is currently in **pre mode** so this branch has prereleases rather than normal releases. If you want to exit prereleases, run `changeset pre exit` on `release-4.3`. ⚠️⚠️⚠️⚠️⚠️⚠️ # Releases ## @apollo/client@4.3.0-alpha.2 ### Minor Changes - [#13274](#13274) [`7b10078`](7b10078) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Adds `Scalar.fromGraphQLScalarType` helper to create a `Scalar` instance from an existing graphql.js `GraphQLScalarType`. ```ts 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](#13252) [`ed86234`](ed86234) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Adds the plumbing and types implementation for declaring custom scalars and configuring custom scalars in `InMemoryCache`. You can declare custom scalar types with declaration merging on the `ApolloCache.Scalars` interface: ```ts // apollo.d.ts import "@apollo/client"; declare module "@apollo/client" { namespace ApolloCache { interface Scalars { Date: { serialized: string; parsed: Date }; } } } ``` This enables the `scalars` option in `InMemoryCache`: ```ts 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](#13259) [`ccaf686`](ccaf686) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Adds a `scalar` option to `InMemoryCache` field policies that tells the cache which scalar to use when parsing or serializing the field value. ```ts 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()` and `cache.restore()`. - [#13273](#13273) [`0886de1`](0886de1) Thanks [@jerelmiller](https://github.com/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 `inputObjects` option is available to `InMemoryCache` that specifies where nested scalar fields are found. ```ts 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](#13252) [`ed86234`](ed86234) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Adds the `getScalar` abstract method to `ApolloCache` that cache subclasses override to provide scalar behavior to Apollo Client. Defaults to unconditionally return `undefined` if not specified. --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
1 parent 126aef8 commit aba0960

4 files changed

Lines changed: 133 additions & 4 deletions

File tree

.changeset/pre.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
"@apollo/client-codemod-migrate-3-to-4": "1.0.2"
88
},
99
"changesets": [
10+
"cold-comics-add",
11+
"eighty-files-sort",
1012
"fuzzy-hairs-tie",
11-
"tidy-cache-type-constraints"
13+
"strong-shoes-sell",
14+
"thin-tips-fold",
15+
"tidy-cache-type-constraints",
16+
"wet-experts-end"
1217
]
1318
}

CHANGELOG.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,129 @@
11
# @apollo/client
22

3+
## 4.3.0-alpha.2
4+
5+
### Minor Changes
6+
7+
- [#13274](https://github.com/apollographql/apollo-client/pull/13274) [`7b10078`](https://github.com/apollographql/apollo-client/commit/7b10078f4bcd8d82890ca438bf7355677fe2f841) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Adds `Scalar.fromGraphQLScalarType` helper to create a `Scalar` instance from an existing graphql.js `GraphQLScalarType`.
8+
9+
```ts
10+
import { GraphQLScalarType } from "graphql";
11+
import { Scalar } from "@apollo/client";
12+
13+
const dateTimeScalarType = new GraphQLScalarType<Date, string>({
14+
// ...
15+
});
16+
17+
const dateTimeScalar = Scalar.fromGraphQLScalarType(dateTimeScalarType, {
18+
is: (value) => value instanceof Date,
19+
});
20+
```
21+
22+
- [#13252](https://github.com/apollographql/apollo-client/pull/13252) [`ed86234`](https://github.com/apollographql/apollo-client/commit/ed8623485683c38982c87278d1381412ef39a9db) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Adds the plumbing and types implementation for declaring custom scalars and configuring custom scalars in `InMemoryCache`.
23+
24+
You can declare custom scalar types with declaration merging on the `ApolloCache.Scalars` interface:
25+
26+
```ts
27+
// apollo.d.ts
28+
import "@apollo/client";
29+
30+
declare module "@apollo/client" {
31+
namespace ApolloCache {
32+
interface Scalars {
33+
Date: { serialized: string; parsed: Date };
34+
}
35+
}
36+
}
37+
```
38+
39+
This enables the `scalars` option in `InMemoryCache`:
40+
41+
```ts
42+
import { Scalar } from "@apollo/client";
43+
44+
const cache = new InMemoryCache({
45+
scalars: {
46+
Date: new Scalar({
47+
parse: (dateString) => new Date(dateString),
48+
serialize: (date) => date.toISOString(),
49+
is: (value) => value instanceof Date,
50+
}),
51+
},
52+
});
53+
```
54+
55+
- [#13259](https://github.com/apollographql/apollo-client/pull/13259) [`ccaf686`](https://github.com/apollographql/apollo-client/commit/ccaf6867be15e413f08594b54b3516003e28c108) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Adds a `scalar` option to `InMemoryCache` field policies that tells the cache which scalar to use when parsing or serializing the field value.
56+
57+
```ts
58+
import { Scalar } from "@apollo/client";
59+
60+
new InMemoryCache({
61+
scalars: {
62+
DateTime: new Scalar({
63+
parse: (dateString) => new Date(dateString),
64+
serialize: (date) => date.toISOString(),
65+
}),
66+
},
67+
typePolicies: {
68+
Event: {
69+
fields: {
70+
startTime: {
71+
// Parse this field using the DateTime scalar
72+
scalar: "DateTime",
73+
},
74+
},
75+
},
76+
},
77+
});
78+
```
79+
80+
This scalar definition is now used to properly parse or serialize the field value for cache reads and writes as well as `cache.extract()` and `cache.restore()`.
81+
82+
- [#13273](https://github.com/apollographql/apollo-client/pull/13273) [`0886de1`](https://github.com/apollographql/apollo-client/commit/0886de19ed67ca24bbcc075dcf5a94ba01589902) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Automatically serialize variables that include custom scalar values. This includes cache reads and writes as well as requests to the network.
83+
84+
For more complex input objects, a new `inputObjects` option is available to `InMemoryCache` that specifies where nested scalar fields are found.
85+
86+
```ts
87+
const cache = new InMemoryCache({
88+
scalars: {
89+
DateTime: new Scalar({
90+
parse: (value) => new Date(value),
91+
serialize: (value) => value.toISOString(),
92+
is: (value) => value instanceof Date,
93+
}),
94+
},
95+
inputObjects: {
96+
EventFilter: {
97+
fields: {
98+
date: "DateTime",
99+
},
100+
},
101+
},
102+
});
103+
104+
const client = new ApolloClient({ cache, link });
105+
106+
await client.query({
107+
query: gql`
108+
query Event($filter: EventFilter!) {
109+
event(filter: $filter) {
110+
name
111+
}
112+
}
113+
`,
114+
variables: {
115+
filter: {
116+
date: new Date("2026-01-01T00:00:00.000Z"),
117+
},
118+
},
119+
});
120+
121+
// The link receives:
122+
// { filter: { date: "2026-01-01T00:00:00.000Z" } }
123+
```
124+
125+
- [#13252](https://github.com/apollographql/apollo-client/pull/13252) [`ed86234`](https://github.com/apollographql/apollo-client/commit/ed8623485683c38982c87278d1381412ef39a9db) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Adds the `getScalar` abstract method to `ApolloCache` that cache subclasses override to provide scalar behavior to Apollo Client. Defaults to unconditionally return `undefined` if not specified.
126+
3127
## 4.3.0-alpha.1
4128

5129
### Patch Changes

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@apollo/client",
3-
"version": "4.3.0-alpha.1",
3+
"version": "4.3.0-alpha.2",
44
"description": "A fully-featured caching GraphQL client.",
55
"private": true,
66
"keywords": [

0 commit comments

Comments
 (0)