@eleven-am/golem-react 0.1.1 → 0.2.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.
package/README.md CHANGED
@@ -16,7 +16,7 @@ Peer dependencies: `@eleven-am/golem-client` ^0.1.0, `@tanstack/react-query` ^5,
16
16
 
17
17
  The client lives on your application's `QueryClient`. It does not make a second one.
18
18
 
19
- The manifest is `zz_golem_client.gen.ts`, written by `golem generate` beside your GraphQL schema on the server (go/v0.8.2 or later). Copy or import it into your front end, and regenerate it whenever the schema changes. Never edit it.
19
+ The manifest is `zz_golem_client.gen.ts`, written by `golem generate` beside your GraphQL schema on the server (go/v0.8.3 or later). Copy or import it into your front end, and regenerate it whenever the schema changes. Never edit it.
20
20
 
21
21
  ```tsx
22
22
  import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
@@ -73,7 +73,7 @@ function Posts() {
73
73
  }
74
74
  ```
75
75
 
76
- It throws `GolemNotLiveError` (naming the reason and the root) for a document that cannot be live. Use `useGolemStaticQuery` for those.
76
+ Aggregates, group-by, relation group-by and custom roots that return model rows are live: any event on a model they come from refetches the document through the coalescer, and they are never patched. Put counts in their own query. It throws `GolemNotLiveError` (naming the reason and the root) for a document that reaches a model without events or a custom root whose result holds no model rows. Use `useGolemStaticQuery` for those. Relation group-by needs a go/v0.8.3 manifest to refetch on events of the models it groups by.
77
77
 
78
78
  ### useGolemStatus
79
79
 
@@ -117,7 +117,7 @@ const pages = feed.data?.pages ?? []
117
117
 
118
118
  ### useGolemStaticQuery
119
119
 
120
- `useQuery` for a document that cannot be live: aggregates, group-by, counts and update-many roots. Fetched once, never patched.
120
+ `useQuery` for a document you want fetched once and never refreshed, or one that cannot be live (a model without events, a custom root with no model rows). Counts and aggregates are live through `useGolemQuery`.
121
121
 
122
122
  ```tsx
123
123
  const COUNT = `
@@ -127,6 +127,7 @@ const COUNT = `
127
127
  `
128
128
 
129
129
  const total = useGolemStaticQuery(COUNT)
130
+ const liveTotal = useGolemQuery(COUNT)
130
131
  ```
131
132
 
132
133
  ## Live status
@@ -221,6 +222,18 @@ GraphQL does not say which subscription root carries a model's events, what its
221
222
 
222
223
  For a walkthrough from a model to a live component, see [Using golem from React](https://github.com/eleven-am/golem/blob/main/docs/golem-go/REACT.md).
223
224
 
225
+ ## Mutations on page unload
226
+
227
+ Browsers cancel ordinary fetches while a page unloads. Pass `keepalive: true` to `useGolemMutation` and that mutation's requests are sent with `fetch(..., { keepalive: true })`.
228
+
229
+ ```tsx
230
+ const endSession = useGolemMutation(END_SESSION, { keepalive: true })
231
+
232
+ addEventListener('pagehide', () => endSession.mutate({ id: sessionId }))
233
+ ```
234
+
235
+ The response may never be processed, so treat the mutation as possibly committed; the next page load refetches the authoritative state. Browsers cap keepalive request bodies at 64 KiB in total, so a larger body throws `GolemKeepaliveTooLargeError` before anything is sent.
236
+
224
237
  ## Errors
225
238
 
226
239
  All server errors are `GolemError` (`code`, `path`, `extensions`, `retryable`) or a subclass:
@@ -234,7 +247,7 @@ Client-side failures: `GolemConfigurationError` (bad setup), `GolemDocumentError
234
247
 
235
248
  ## Requirements
236
249
 
237
- - golem **go/v0.8.2 or later** on the server, with `golem.Subscriptions` on every model a live query reaches.
250
+ - golem **go/v0.8.3 or later** on the server, with `golem.Subscriptions` on every model a live query reaches.
238
251
  - `react` >=18, `@tanstack/react-query` 5.
239
252
  - A socket deadline for every principal, and a manifest regenerated after every schema change. See [The socket deadline](https://github.com/eleven-am/golem/blob/main/docs/golem-go/GUIDE.md#the-socket-deadline) and [The client manifest](https://github.com/eleven-am/golem/blob/main/docs/golem-go/GUIDE.md#the-client-manifest).
240
253
 
package/dist/hooks.d.ts CHANGED
@@ -3,7 +3,9 @@ import type { Cursor } from '@eleven-am/golem-client';
3
3
  import type { GolemDocument, ResultFor, VariablesFor } from './types.js';
4
4
  export type GolemQueryOptions<TData> = Omit<UseQueryOptions<TData, Error, TData, readonly unknown[]>, 'queryKey' | 'queryFn' | 'meta'>;
5
5
  export type GolemInfiniteQueryOptions<TPage> = Omit<UseInfiniteQueryOptions<TPage, Error, InfiniteData<TPage, Cursor | null>, readonly unknown[], Cursor | null>, 'queryKey' | 'queryFn' | 'meta' | 'initialPageParam' | 'getNextPageParam'>;
6
- export type GolemMutationOptions<TData, TVariables> = Omit<UseMutationOptions<TData, Error, TVariables>, 'mutationFn'>;
6
+ export type GolemMutationOptions<TData, TVariables> = Omit<UseMutationOptions<TData, Error, TVariables>, 'mutationFn'> & {
7
+ readonly keepalive?: boolean;
8
+ };
7
9
  export declare function useGolemQuery<TDocument extends GolemDocument>(document: TDocument, variables?: VariablesFor<TDocument>, options?: GolemQueryOptions<ResultFor<TDocument>>): UseQueryResult<ResultFor<TDocument>, Error>;
8
10
  export declare function useGolemStaticQuery<TDocument extends GolemDocument>(document: TDocument, variables?: VariablesFor<TDocument>, options?: GolemQueryOptions<ResultFor<TDocument>>): UseQueryResult<ResultFor<TDocument>, Error>;
9
11
  export declare function useGolemInfiniteQuery<TDocument extends GolemDocument>(document: TDocument, variables?: VariablesFor<TDocument>, options?: GolemInfiniteQueryOptions<ResultFor<TDocument>>): UseInfiniteQueryResult<InfiniteData<ResultFor<TDocument>, Cursor | null>, Error>;
package/dist/hooks.js CHANGED
@@ -21,8 +21,9 @@ export function useGolemInfiniteQuery(document, variables, options) {
21
21
  }
22
22
  export function useGolemMutation(document, options) {
23
23
  const client = useGolemClient();
24
+ const { keepalive, ...mutation } = options ?? {};
24
25
  return useMutation({
25
- ...options,
26
- mutationFn: (variables) => client.mutate(document, variables),
26
+ ...mutation,
27
+ mutationFn: (variables) => client.mutate(document, variables, keepalive === undefined ? undefined : { keepalive }),
27
28
  });
28
29
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/golem-react",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "React bindings for the golem live GraphQL client",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -20,7 +20,7 @@
20
20
  "test": "tsc --noEmit -p tsconfig.spec.json && vitest run"
21
21
  },
22
22
  "peerDependencies": {
23
- "@eleven-am/golem-client": "^0.1.1",
23
+ "@eleven-am/golem-client": "^0.2.0",
24
24
  "@tanstack/react-query": "^5.0.0",
25
25
  "gql.tada": "^1.8.0",
26
26
  "react": ">=18"