|
1 | 1 | # @apollo/client |
2 | 2 |
|
| 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 | + |
3 | 127 | ## 4.3.0-alpha.1 |
4 | 128 |
|
5 | 129 | ### Patch Changes |
|
0 commit comments