react-fate 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/README.md +354 -2
  2. package/docs/api/functions/FateClient.md +1 -1
  3. package/docs/api/functions/clientRoot.md +1 -1
  4. package/docs/api/functions/createClient.md +8 -4
  5. package/docs/api/functions/createGraphQLTransport.md +21 -0
  6. package/docs/api/functions/createHTTPTransport.md +1 -1
  7. package/docs/api/functions/createTRPCTransport.md +1 -1
  8. package/docs/api/functions/graphqlMutation.md +39 -0
  9. package/docs/api/functions/mutation.md +1 -1
  10. package/docs/api/functions/useFateClient.md +1 -1
  11. package/docs/api/functions/useListView.md +2 -2
  12. package/docs/api/functions/useLiveListView.md +2 -2
  13. package/docs/api/functions/useLiveView.md +1 -1
  14. package/docs/api/functions/useRequest.md +1 -1
  15. package/docs/api/functions/useView.md +1 -1
  16. package/docs/api/functions/view.md +1 -1
  17. package/docs/api/index.md +11 -0
  18. package/docs/api/type-aliases/ConnectionRef.md +2 -2
  19. package/docs/api/type-aliases/FateDehydratedState.md +11 -0
  20. package/docs/api/type-aliases/GraphQLMutationDefinition.md +19 -0
  21. package/docs/api/type-aliases/GraphQLMutationInput.md +11 -0
  22. package/docs/api/type-aliases/GraphQLMutationMap.md +11 -0
  23. package/docs/api/type-aliases/GraphQLMutationOutput.md +11 -0
  24. package/docs/api/type-aliases/GraphQLTransportOptions.md +119 -0
  25. package/docs/api/type-aliases/HydrateOptions.md +7 -0
  26. package/docs/api/type-aliases/HydrationLimits.md +7 -0
  27. package/docs/api/type-aliases/InferFateAPI.md +1 -1
  28. package/docs/api/type-aliases/Pagination.md +39 -0
  29. package/docs/api/type-aliases/ViewRef.md +1 -1
  30. package/docs/api/variables/toEntityId.md +1 -1
  31. package/docs/guide/getting-started.md +1 -1
  32. package/docs/guide/graphql-integration.md +298 -0
  33. package/docs/guide/requests.md +52 -0
  34. package/docs/guide/server-integration.md +2 -1
  35. package/lib/index.d.mts +7 -9
  36. package/lib/index.mjs +2 -2
  37. package/package.json +5 -5
@@ -0,0 +1,19 @@
1
+ # Type Alias: GraphQLMutationDefinition\<T, Input, Output\>
2
+
3
+ > **GraphQLMutationDefinition**\<`T`, `Input`, `Output`\> = `Readonly`\<\{ `__fateGraphQLMutation?`: \{ `input`: `Input`; `output`: `Output`; \}; `entity`: `T`\[`"__typename"`\]; `field`: `string`; `inputArg?`: `false` \| `string`; \}\>
4
+
5
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:20
6
+
7
+ ## Type Parameters
8
+
9
+ ### T
10
+
11
+ `T` *extends* `Entity` = `Entity`
12
+
13
+ ### Input
14
+
15
+ `Input` = `unknown`
16
+
17
+ ### Output
18
+
19
+ `Output` = `unknown`
@@ -0,0 +1,11 @@
1
+ # Type Alias: GraphQLMutationInput\<Definition\>
2
+
3
+ > **GraphQLMutationInput**\<`Definition`\> = `Definition` *extends* [`GraphQLMutationDefinition`](GraphQLMutationDefinition.md)\<`any`, infer Input, `any`\> ? `Input` : `never`
4
+
5
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:29
6
+
7
+ ## Type Parameters
8
+
9
+ ### Definition
10
+
11
+ `Definition`
@@ -0,0 +1,11 @@
1
+ # Type Alias: GraphQLMutationMap\<Mutations\>
2
+
3
+ > **GraphQLMutationMap**\<`Mutations`\> = `Mutations` *extends* `Record`\<`string`, [`GraphQLMutationDefinition`](GraphQLMutationDefinition.md)\> ? `{ [K in keyof Mutations]: { input: GraphQLMutationInput<Mutations[K]>; output: GraphQLMutationOutput<Mutations[K]> } }` : `EmptyTransportMutations`
4
+
5
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:31
6
+
7
+ ## Type Parameters
8
+
9
+ ### Mutations
10
+
11
+ `Mutations`
@@ -0,0 +1,11 @@
1
+ # Type Alias: GraphQLMutationOutput\<Definition\>
2
+
3
+ > **GraphQLMutationOutput**\<`Definition`\> = `Definition` *extends* [`GraphQLMutationDefinition`](GraphQLMutationDefinition.md)\<`any`, `any`, infer Output\> ? `Output` : `never`
4
+
5
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:30
6
+
7
+ ## Type Parameters
8
+
9
+ ### Definition
10
+
11
+ `Definition`
@@ -0,0 +1,119 @@
1
+ # Type Alias: GraphQLTransportOptions\<Mutations\>
2
+
3
+ > **GraphQLTransportOptions**\<`Mutations`\> = `object`
4
+
5
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:46
6
+
7
+ ## Type Parameters
8
+
9
+ ### Mutations
10
+
11
+ `Mutations` *extends* `TransportMutations` = `EmptyTransportMutations`
12
+
13
+ ## Properties
14
+
15
+ ### decodeNodeId?
16
+
17
+ > `optional` **decodeNodeId?**: (`type`, `id`) => `string` \| `number`
18
+
19
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:47
20
+
21
+ #### Parameters
22
+
23
+ ##### type
24
+
25
+ `string`
26
+
27
+ ##### id
28
+
29
+ `string` \| `number`
30
+
31
+ #### Returns
32
+
33
+ `string` \| `number`
34
+
35
+ ***
36
+
37
+ ### encodeNodeId?
38
+
39
+ > `optional` **encodeNodeId?**: (`type`, `id`) => `string` \| `number`
40
+
41
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:48
42
+
43
+ #### Parameters
44
+
45
+ ##### type
46
+
47
+ `string`
48
+
49
+ ##### id
50
+
51
+ `string` \| `number`
52
+
53
+ #### Returns
54
+
55
+ `string` \| `number`
56
+
57
+ ***
58
+
59
+ ### eventSource?
60
+
61
+ > `optional` **eventSource?**: `EventSourceConstructor`
62
+
63
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:49
64
+
65
+ ***
66
+
67
+ ### fetch?
68
+
69
+ > `optional` **fetch?**: `FetchLike`
70
+
71
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:50
72
+
73
+ ***
74
+
75
+ ### headers?
76
+
77
+ > `optional` **headers?**: `HeadersFactory`
78
+
79
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:51
80
+
81
+ ***
82
+
83
+ ### live?
84
+
85
+ > `optional` **live?**: `boolean` \| `GraphQLLiveOptions`
86
+
87
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:52
88
+
89
+ ***
90
+
91
+ ### mutations?
92
+
93
+ > `optional` **mutations?**: `Record`\<`Extract`\<keyof `Mutations`, `string`\>, `GraphQLMutationRuntimeConfig`\>
94
+
95
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:53
96
+
97
+ ***
98
+
99
+ ### roots?
100
+
101
+ > `optional` **roots?**: `Record`\<`string`, `GraphQLRootConfig`\>
102
+
103
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:54
104
+
105
+ ***
106
+
107
+ ### types
108
+
109
+ > **types**: `ReadonlyArray`\<`Omit`\<`TypeConfig`, `"getId"`\> & `Partial`\<`Pick`\<`TypeConfig`, `"getId"`\>\>\>
110
+
111
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:55
112
+
113
+ ***
114
+
115
+ ### url
116
+
117
+ > **url**: `string` \| `URL`
118
+
119
+ Defined in: packages/fate/lib/graphqlTransport-DbRHbYtb.d.mts:56
@@ -0,0 +1,7 @@
1
+ # Type Alias: HydrateOptions
2
+
3
+ > **HydrateOptions** = `Readonly`\<\{ `merge?`: `"preserve-existing"` \| `"replace"`; \}\>
4
+
5
+ Defined in: packages/fate/lib/transport-BTgtSwOM.d.mts:692
6
+
7
+ Controls how `FateClient.hydrate()` reconciles a snapshot with browser cache state.
@@ -0,0 +1,7 @@
1
+ # Type Alias: HydrationLimits
2
+
3
+ > **HydrationLimits** = `Readonly`\<\{ `maxCollectionLength`: `number`; `maxNodes`: `number`; `maxStringLength`: `number`; \}\>
4
+
5
+ Defined in: packages/fate/lib/transport-BTgtSwOM.d.mts:702
6
+
7
+ Resource limits applied while encoding and decoding hydration snapshots.
@@ -2,7 +2,7 @@
2
2
 
3
3
  > **InferFateAPI**\<`Server`\> = `Server` *extends* `object` ? `API` : `never`
4
4
 
5
- Defined in: packages/fate/lib/record-CirbfZWX.d.mts:207
5
+ Defined in: packages/fate/lib/record-BtdKzEGI.d.mts:207
6
6
 
7
7
  ## Type Parameters
8
8
 
@@ -0,0 +1,39 @@
1
+ # Type Alias: Pagination
2
+
3
+ > **Pagination** = `object`
4
+
5
+ Defined in: packages/fate/lib/transport-BTgtSwOM.d.mts:1086
6
+
7
+ Pagination state returned alongside connection lists.
8
+
9
+ ## Properties
10
+
11
+ ### hasNext
12
+
13
+ > **hasNext**: `boolean`
14
+
15
+ Defined in: packages/fate/lib/transport-BTgtSwOM.d.mts:1087
16
+
17
+ ***
18
+
19
+ ### hasPrevious
20
+
21
+ > **hasPrevious**: `boolean`
22
+
23
+ Defined in: packages/fate/lib/transport-BTgtSwOM.d.mts:1088
24
+
25
+ ***
26
+
27
+ ### nextCursor?
28
+
29
+ > `optional` **nextCursor?**: `string`
30
+
31
+ Defined in: packages/fate/lib/transport-BTgtSwOM.d.mts:1089
32
+
33
+ ***
34
+
35
+ ### previousCursor?
36
+
37
+ > `optional` **previousCursor?**: `string`
38
+
39
+ Defined in: packages/fate/lib/transport-BTgtSwOM.d.mts:1090
@@ -2,7 +2,7 @@
2
2
 
3
3
  > **ViewRef**\<`TName`\> = `Readonly`\<\{ `__typename`: `TName`; `[ViewsTag]`: `Set`\<`string`\>; `id`: `string` \| `number`; \}\>
4
4
 
5
- Defined in: packages/fate/lib/types-Dz46PXr3.d.mts:1107
5
+ Defined in: packages/fate/lib/transport-BTgtSwOM.d.mts:1068
6
6
 
7
7
  Reference to a normalized entity instance that can be resolved against one or more view tags.
8
8
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  > `const` **toEntityId**: (`type`, `rawId`) => `EntityId`
4
4
 
5
- Defined in: packages/fate/lib/index.d.mts:75
5
+ Defined in: packages/fate/lib/index.d.mts:76
6
6
 
7
7
  Builds the canonical cache ID for an entity.
8
8
 
@@ -8,7 +8,7 @@ Create a new fate app with Vite+:
8
8
  vp create fate my-app
9
9
  ```
10
10
 
11
- The template selector can create a Void app with Drizzle, a tRPC app with Drizzle, or a tRPC app with Prisma. The template sources live in the fate repo under [`packages/create-fate/templates/fate`](https://github.com/nkzw-tech/fate/tree/main/packages/create-fate/templates/fate). They feature modern tools to deliver an incredibly fast development experience.
11
+ The template selector can create a Void app with Drizzle, a tRPC app with Drizzle or Prisma, a GraphQL app with Prisma, or a fate client for an existing GraphQL server. The template sources live in the fate repo under [`packages/create-fate/templates/fate`](https://github.com/nkzw-tech/fate/tree/main/packages/create-fate/templates/fate). They feature modern tools to deliver an incredibly fast development experience.
12
12
 
13
13
  ## Manual Installation
14
14
 
@@ -0,0 +1,298 @@
1
+ # GraphQL Integration
2
+
3
+ _fate_ can use an existing GraphQL API as its transport. This keeps the React APIs, view composition, normalized cache, masking, requests, list views, live views, and actions the same while replacing the native or tRPC backend with GraphQL operations.
4
+
5
+ Use the GraphQL transport when your backend already exposes GraphQL and you want fate's client model without adding fate's native server protocol.
6
+
7
+ ## Template
8
+
9
+ Create a client for an existing GraphQL server with:
10
+
11
+ ```bash
12
+ vp create fate my-app --template graphql-client
13
+ ```
14
+
15
+ Create a full GraphQL + Prisma example app with:
16
+
17
+ ```bash
18
+ vp create fate my-app --template graphql
19
+ ```
20
+
21
+ The client-only template is the smallest reference for the integration. It contains a `src/fate/graphql.ts` file that maps your GraphQL schema to fate views and roots.
22
+
23
+ ## GraphQL Schema Shape
24
+
25
+ The GraphQL transport expects a schema with Relay-style object identity and pagination:
26
+
27
+ - Entity objects include `id` and `__typename`.
28
+ - Object fetches go through a `nodes(ids:)` field.
29
+ - List fields return Relay connections with `edges`, `cursor`, `node`, and `pageInfo`.
30
+ - Root queries and mutations return the entity type selected by the fate view.
31
+
32
+ For example, a `Post` list can be exposed as a normal GraphQL connection:
33
+
34
+ ```graphql
35
+ type Query {
36
+ posts(first: Int, after: String): PostConnection!
37
+ viewer: User
38
+ nodes(ids: [ID!]!): [Node]!
39
+ }
40
+
41
+ type PostConnection {
42
+ edges: [PostEdge!]!
43
+ pageInfo: PageInfo!
44
+ }
45
+ ```
46
+
47
+ If your schema uses different root field names, keep the fate names you want on the client and map them with `fateGraphQL.roots`.
48
+
49
+ ## Mapping Your Schema
50
+
51
+ Create a module that exports data views, `Root`, and an optional `fateGraphQL` config. The Vite plugin reads this module during development and build time, generates the client wiring, and leaves your runtime GraphQL server unchanged.
52
+
53
+ ```tsx
54
+ import { graphqlMutation } from '@nkzw/fate';
55
+ import { dataView, list, type Entity } from '@nkzw/fate/server';
56
+
57
+ type GraphQLUser = {
58
+ id: string;
59
+ name?: string | null;
60
+ username?: string | null;
61
+ };
62
+
63
+ type GraphQLPost = {
64
+ author?: GraphQLUser | null;
65
+ id: string;
66
+ title: string;
67
+ };
68
+
69
+ export const userDataView = dataView<GraphQLUser>('User')({
70
+ id: true,
71
+ name: true,
72
+ username: true,
73
+ });
74
+
75
+ export const postDataView = dataView<GraphQLPost>('Post')({
76
+ author: userDataView,
77
+ id: true,
78
+ title: true,
79
+ });
80
+
81
+ export type User = Entity<typeof userDataView, 'User'>;
82
+ export type Post = Entity<
83
+ typeof postDataView,
84
+ 'Post',
85
+ {
86
+ author: User | null;
87
+ }
88
+ >;
89
+
90
+ export const Root = {
91
+ posts: list(postDataView),
92
+ viewer: userDataView,
93
+ };
94
+
95
+ export const fateGraphQL = {
96
+ roots: {
97
+ posts: { field: 'posts' },
98
+ viewer: { field: 'viewer' },
99
+ },
100
+ } as const;
101
+ ```
102
+
103
+ The data views describe the fields React components are allowed to select. `Root` describes the root operations available to `useRequest`. `fateGraphQL.roots` maps those root names to actual GraphQL fields. If the GraphQL field has the same name as the fate root, the `field` entry can be omitted.
104
+
105
+ ## Vite Plugin
106
+
107
+ Configure the fate Vite plugin with the GraphQL transport and point it at the mapping module:
108
+
109
+ ```tsx
110
+ import { fate } from 'react-fate/vite';
111
+ import { defineConfig } from 'vite';
112
+
113
+ export default defineConfig({
114
+ plugins: [
115
+ fate({
116
+ module: './src/fate/graphql.ts',
117
+ transport: 'graphql',
118
+ }),
119
+ ],
120
+ });
121
+ ```
122
+
123
+ The plugin generates a typed `createFateClient` helper from your views, roots, and GraphQL mapping. It also watches the mapping module and the files it imports during development.
124
+
125
+ ## Creating a Client
126
+
127
+ Create the client with your GraphQL endpoint and provide it through the `FateClient` provider:
128
+
129
+ ```tsx
130
+ import { FateClient } from 'react-fate';
131
+ import { createFateClient } from 'react-fate/client';
132
+
133
+ const fate = createFateClient({
134
+ headers: () => ({
135
+ authorization: `Bearer ${token}`,
136
+ }),
137
+ url: 'https://api.example.com/graphql',
138
+ });
139
+
140
+ export function App() {
141
+ return <FateClient client={fate}>{/* Components go here */}</FateClient>;
142
+ }
143
+ ```
144
+
145
+ Use `fetch` when you need to customize credentials or reuse an application fetch wrapper:
146
+
147
+ ```tsx
148
+ const fate = createFateClient({
149
+ fetch: (input, init) =>
150
+ fetch(input, {
151
+ ...init,
152
+ credentials: 'include',
153
+ }),
154
+ url: `${env('SERVER_URL')}/graphql`,
155
+ });
156
+ ```
157
+
158
+ GraphQL operations issued in the same microtask are batched into a single GraphQL query or mutation document with aliased fields.
159
+
160
+ ## Object IDs
161
+
162
+ The transport converts between fate entity IDs and GraphQL node IDs. By default, it sends IDs as `${type}-${id}` and strips that prefix from returned IDs. Override this if your schema uses Relay global IDs, raw database IDs, or another encoding:
163
+
164
+ ```tsx
165
+ const fate = createFateClient({
166
+ decodeNodeId: (type, id) => {
167
+ const [nodeType, nodeId] = atob(String(id)).split(':');
168
+ if (nodeType !== type) {
169
+ throw new Error(`Expected a ${type} node id.`);
170
+ }
171
+ return nodeId;
172
+ },
173
+ encodeNodeId: (type, id) => btoa(`${type}:${id}`),
174
+ url: '/graphql',
175
+ });
176
+ ```
177
+
178
+ If your GraphQL API already accepts and returns the same IDs you use in the app, return `id` from both functions.
179
+
180
+ ## Requests and Arguments
181
+
182
+ Client code keeps using `useRequest` with the same shape as the other transports:
183
+
184
+ ```tsx
185
+ const { posts, viewer } = useRequest({
186
+ posts: {
187
+ args: { first: 10 },
188
+ list: PostView,
189
+ },
190
+ viewer: { view: UserView },
191
+ });
192
+ ```
193
+
194
+ Root arguments are sent to the root GraphQL field. Nested relation arguments are scoped by relation name:
195
+
196
+ ```tsx
197
+ const { posts } = useRequest({
198
+ posts: {
199
+ args: {
200
+ comments: { first: 3 },
201
+ first: 10,
202
+ },
203
+ list: PostWithCommentsView,
204
+ },
205
+ });
206
+ ```
207
+
208
+ This produces a root `posts(first: 10)` field and a nested `comments(first: 3)` field in the generated GraphQL selection.
209
+
210
+ ## Mutations
211
+
212
+ Map fate mutation names to GraphQL mutation fields with `graphqlMutation`:
213
+
214
+ ```tsx
215
+ export const fateGraphQL = {
216
+ mutations: {
217
+ 'post.like': graphqlMutation<Post, { id: string }, Post>('Post', {
218
+ field: 'postLike',
219
+ }),
220
+ },
221
+ roots: {
222
+ posts: { field: 'posts' },
223
+ },
224
+ } as const;
225
+ ```
226
+
227
+ By default, the input is sent as an `input` argument:
228
+
229
+ ```graphql
230
+ mutation {
231
+ postLike(input: { id: "12" }) {
232
+ id
233
+ likes
234
+ }
235
+ }
236
+ ```
237
+
238
+ Use `inputArg` when your schema uses a different argument name, or `inputArg: false` when the input object should be spread into field arguments:
239
+
240
+ ```tsx
241
+ export const fateGraphQL = {
242
+ mutations: {
243
+ 'post.like': graphqlMutation<Post, { id: string }, Post>('Post', {
244
+ field: 'likePost',
245
+ inputArg: 'payload',
246
+ }),
247
+ 'user.follow': graphqlMutation<User, { id: string }, User>('User', {
248
+ field: 'followUser',
249
+ inputArg: false,
250
+ }),
251
+ },
252
+ } as const;
253
+ ```
254
+
255
+ Actions use the same `mutation(...)` and `useActionState` APIs described in the [Actions Guide](actions.md).
256
+
257
+ ## Live Views
258
+
259
+ GraphQL live views use [GraphQL SSE](https://github.com/enisdenjo/graphql-sse). Install `graphql-sse` in the client package and leave `live` enabled, or pass `live: false` when your schema does not support subscriptions.
260
+
261
+ ```tsx
262
+ const fate = createFateClient({
263
+ live: {
264
+ url: 'https://api.example.com/graphql/stream',
265
+ },
266
+ url: 'https://api.example.com/graphql',
267
+ });
268
+ ```
269
+
270
+ The default subscription fields are `fateLiveNode` for `useLiveView` and `fateLiveConnection` for `useLiveListView`. Rename them with `entityField` and `connectionField`:
271
+
272
+ ```tsx
273
+ const fate = createFateClient({
274
+ live: {
275
+ connectionField: 'liveConnection',
276
+ entityField: 'liveNode',
277
+ url: '/graphql/stream',
278
+ },
279
+ url: '/graphql',
280
+ });
281
+ ```
282
+
283
+ The live node subscription returns `{ data, delete, id, select }`. The live connection subscription returns events such as `appendNode`, `prependNode`, `deleteEdge`, and `invalidate`. These payloads match fate's live transport events, so the cache update behavior is the same as the native transport.
284
+
285
+ If you do not need live views, disable them explicitly:
286
+
287
+ ```tsx
288
+ const fate = createFateClient({
289
+ live: false,
290
+ url: '/graphql',
291
+ });
292
+ ```
293
+
294
+ ## Existing Servers
295
+
296
+ The GraphQL transport is intentionally a mapping layer. It does not require `createFateServer`, the Prisma adapter, or the Drizzle adapter. Your GraphQL server remains responsible for authorization, validation, resolver behavior, cursor pagination, and mutation side effects.
297
+
298
+ Use data views to expose only the fields the client should be able to select, keep GraphQL schema authorization in your server, and treat `src/fate/graphql.ts` as the contract between your GraphQL API and fate's React client.
@@ -128,3 +128,55 @@ try {
128
128
  ```
129
129
 
130
130
  Garbage collection waits for active optimistic updates to settle before sweeping records. This keeps temporary optimistic records and their list positions stable while mutations are still pending.
131
+
132
+ ## SSR and Hydration
133
+
134
+ Create a request-scoped fate client on the server, preload the route data, and dehydrate its normalized cache:
135
+
136
+ ```tsx
137
+ const fate = createFateClient();
138
+ await fate.request({ post: { id: '12', view: PostView } });
139
+
140
+ return {
141
+ fate: fate.dehydrate(),
142
+ };
143
+ ```
144
+
145
+ Transport the returned value through your framework's loader serialization, React Server Component props, or a safely escaped JSON bootstrap script. The snapshot contains plain serializable values, so serializers such as Seroval can carry it without fate-specific integration. Treat the snapshot as opaque: hydrate it through fate rather than reading or editing its internal data.
146
+
147
+ On the browser, hydrate the new client before rendering components that call `useRequest`:
148
+
149
+ ```tsx
150
+ const fate = createFateClient();
151
+ fate.hydrate(loaderData.fate);
152
+
153
+ hydrateRoot(
154
+ document,
155
+ <FateClient client={fate}>
156
+ <App />
157
+ </FateClient>,
158
+ );
159
+ ```
160
+
161
+ Hydrated `cache-first` requests resolve from the normalized cache without refetching. Hydration restores records, selected-field coverage, root queries, and list pagination state. It intentionally does not restore active requests, subscriptions, retainers, timers, or optimistic mutation state.
162
+
163
+ Snapshots carry a hydration scope and are rejected by clients with a different scope. Generated clients set a stable scope automatically. When constructing a client directly, pass `hydrationScope` and rotate it when deploying an incompatible cache schema or when separating cache namespaces:
164
+
165
+ ```tsx
166
+ const fate = createClient({
167
+ hydrationScope: 'storefront-v2',
168
+ // ...
169
+ });
170
+ ```
171
+
172
+ Use `hydrationLimits` when an application needs stricter bootstrap payload limits. fate applies conservative defaults for total encoded values, collection sizes, and string lengths.
173
+
174
+ By default, hydration preserves values already present in the browser cache while adding missing server data. Pass `{ merge: 'replace' }` only when the snapshot should authoritatively reset the durable cache:
175
+
176
+ ```tsx
177
+ fate.hydrate(loaderData.fate, { merge: 'replace' });
178
+ ```
179
+
180
+ `preserve-existing` recursively combines plain scalar objects while keeping browser values on conflicts. Arrays, dates, entity references, and list windows are atomic: an existing browser value wins as a whole. Replaying a snapshot is safe and does not notify subscribers when durable cache state is unchanged.
181
+
182
+ Do not reuse request-scoped snapshots across users. Dehydrate after awaited route preloading: snapshots are point-in-time values and do not stream cache patches for data that resolves later. Hydration and dehydration reject clients with in-flight requests, so hydrate the initial snapshot before rendering.
@@ -1,9 +1,10 @@
1
1
  # Server Integration
2
2
 
3
- Until now, we have focused on the client-side API of fate. You'll need a backend that follows fate's data protocol so the Vite plugin can wire the typed fate APIs into your app. _fate_ currently ships two server paths:
3
+ Until now, we have focused on the client-side API of fate. You'll need a backend that can be wired into fate's typed request model so the Vite plugin can connect the typed fate APIs to your app. _fate_ currently ships three integration paths:
4
4
 
5
5
  - The native fate protocol, which is transport-agnostic and can be hosted by any Fetch-compatible server.
6
6
  - The tRPC adapter, which keeps compatibility with existing tRPC backends.
7
+ - The [GraphQL transport](graphql-integration.md), which maps fate views and roots to an existing GraphQL schema.
7
8
 
8
9
  _fate_ currently provides database adapters for Prisma and Drizzle, but the framework itself is not coupled to a particular ORM. The adapters plug into the same source execution runtime and can be exposed through the native protocol or through tRPC.
9
10