@softistx/riva-graphql 0.0.0-stage → 0.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.
- package/LICENSE +21 -0
- package/README.md +182 -2
- package/dist/bff/index.d.ts +14 -0
- package/dist/bff/index.d.ts.map +1 -0
- package/dist/bff/index.js +13 -0
- package/dist/bff/index.js.map +10 -0
- package/dist/cache/field-key.d.ts +21 -0
- package/dist/cache/field-key.d.ts.map +1 -0
- package/dist/cache/index.d.ts +3 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/cache/index.js +380 -0
- package/dist/cache/index.js.map +17 -0
- package/dist/cache/normalized.d.ts +14 -0
- package/dist/cache/normalized.d.ts.map +1 -0
- package/dist/cache/read.d.ts +11 -0
- package/dist/cache/read.d.ts.map +1 -0
- package/dist/cache/selection.d.ts +33 -0
- package/dist/cache/selection.d.ts.map +1 -0
- package/dist/cache/type-match.d.ts +16 -0
- package/dist/cache/type-match.d.ts.map +1 -0
- package/dist/cache/typename.d.ts +10 -0
- package/dist/cache/typename.d.ts.map +1 -0
- package/dist/cache/values.d.ts +15 -0
- package/dist/cache/values.d.ts.map +1 -0
- package/dist/cache/write.d.ts +18 -0
- package/dist/cache/write.d.ts.map +1 -0
- package/dist/chunks/index-etvmjkh7.js +11 -0
- package/dist/chunks/index-etvmjkh7.js.map +10 -0
- package/dist/chunks/index-zpgy1hwx.js +59 -0
- package/dist/chunks/index-zpgy1hwx.js.map +11 -0
- package/dist/client-transport.d.ts +10 -0
- package/dist/client-transport.d.ts.map +1 -0
- package/dist/codegen/index.d.ts +34 -0
- package/dist/codegen/index.d.ts.map +1 -0
- package/dist/codegen/index.js +44 -0
- package/dist/codegen/index.js.map +10 -0
- package/dist/errors.d.ts +16 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +96 -0
- package/dist/index.js.map +12 -0
- package/dist/infinite/index.d.ts +15 -0
- package/dist/infinite/index.d.ts.map +1 -0
- package/dist/infinite/index.js +26 -0
- package/dist/infinite/index.js.map +10 -0
- package/dist/operation.d.ts +24 -0
- package/dist/operation.d.ts.map +1 -0
- package/dist/query.d.ts +40 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/run.d.ts +5 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/server/index.d.ts +20 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +47 -0
- package/dist/server/index.js.map +10 -0
- package/dist/transport.d.ts +17 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -0
- package/docs/README.md +22 -0
- package/docs/guide/bff.md +54 -0
- package/docs/guide/cache.md +47 -0
- package/docs/guide/codegen.md +67 -0
- package/docs/guide/documents.md +49 -0
- package/docs/guide/errors.md +42 -0
- package/docs/guide/infinite.md +48 -0
- package/docs/guide/mutations.md +38 -0
- package/docs/guide/queries.md +42 -0
- package/docs/guide/transport.md +71 -0
- package/docs/troubleshooting.md +210 -0
- package/package.json +104 -5
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Infinite lists
|
|
2
|
+
|
|
3
|
+
## What it is for
|
|
4
|
+
|
|
5
|
+
`infinite(document, { connection })` builds a paginated list from a Relay-shaped query: pages are fetched on demand and flattened.
|
|
6
|
+
|
|
7
|
+
## Define and run
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { infinite } from '@softistx/riva-graphql/infinite';
|
|
11
|
+
import { PATIENTS_QUERY } from './graphql';
|
|
12
|
+
|
|
13
|
+
export const patientsFeed = infinite(PATIENTS_QUERY, {
|
|
14
|
+
connection: (data) => data.patients, // { edges, pageInfo }
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
await patientsFeed.ensure(client, { school: 'north' });
|
|
18
|
+
await patientsFeed.fetchNext(client, { school: 'north' });
|
|
19
|
+
const { data, hasNext } = client.get(patientsFeed, { school: 'north' });
|
|
20
|
+
data?.nodes; // the flattened nodes, typed from the document
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Options
|
|
24
|
+
|
|
25
|
+
| Option | Default | Meaning |
|
|
26
|
+
|---|---|---|
|
|
27
|
+
| `connection` | required | Picks `{ edges: [{ node, cursor }], pageInfo }` out of a page's data. |
|
|
28
|
+
| `cursor` | `{ after: 'after', before: 'before' }` | Names of the document's cursor variables. |
|
|
29
|
+
| `id` | the query's id plus `#infinite` | Override the list id. |
|
|
30
|
+
| `staleTime`, `gcTime`, `retry`, `retryDelay`, `refetchOnFocus`, `refetchOnReconnect`, `refetchEvery` | as `query()` | The core's options. |
|
|
31
|
+
|
|
32
|
+
See the [core guide](../../../riva/docs/guide/infinite.md) for the state (`hasNext`, `fetchPrevious`…).
|
|
33
|
+
|
|
34
|
+
## Custom cursor variable names
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
// a Patients document declaring `$afterCursor`
|
|
38
|
+
export const feed = infinite(PATIENTS_BY_CURSOR_QUERY, {
|
|
39
|
+
connection: (data) => data.patients,
|
|
40
|
+
cursor: { after: 'afterCursor' }, // query Patients($afterCursor: String) { … }
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Traps
|
|
45
|
+
|
|
46
|
+
- The caller's variables leave the cursors out: never pass `after` yourself.
|
|
47
|
+
- `query(PATIENTS_QUERY)` and `infinite(PATIENTS_QUERY, …)` never share a cache entry (the `#infinite` suffix).
|
|
48
|
+
- `infinite()` takes core cache policies only, not `normalized()`.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Mutations
|
|
2
|
+
|
|
3
|
+
## What it is for
|
|
4
|
+
|
|
5
|
+
`mutation(document, options?)` runs a write typed from its document, with optimistic updates, cache updates and invalidation.
|
|
6
|
+
|
|
7
|
+
## Define and run
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { mutation } from '@softistx/riva-graphql';
|
|
11
|
+
import { patients } from './queries';
|
|
12
|
+
import { RENAME_PATIENT_MUTATION } from './graphql';
|
|
13
|
+
|
|
14
|
+
export const renamePatient = mutation(RENAME_PATIENT_MUTATION, {
|
|
15
|
+
invalidates: [patients], // observed lists refetch once it succeeded
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
await renamePatient.run(client, { id: '1', name: 'Ana' }); // rejects after the rollback
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Options
|
|
22
|
+
|
|
23
|
+
`GraphQLMutationOptions` is the core's mutation options, typed from the document, plus `cache`.
|
|
24
|
+
|
|
25
|
+
| Option | Default | Meaning |
|
|
26
|
+
|---|---|---|
|
|
27
|
+
| `id` | the persisted id, else the operation name | Override the derived id. |
|
|
28
|
+
| `optimistic(vars, tx)` | none | Shows the expected result at once; returns the document's data (for the `cache` layer) or writes with `tx.set`. Rolled back on failure. |
|
|
29
|
+
| `updates(tx, data, vars)` | none | On success, writes the committed data. |
|
|
30
|
+
| `invalidates` | none | `[query]`, `[at(query, vars)]` or `(data, vars) => [...]`: queries marked stale on success. |
|
|
31
|
+
| `cache` | none | A core policy or `normalized()`: the result updates every entity it returns. |
|
|
32
|
+
| `retry` | `0` | A count, or `(failures, error) => boolean`. |
|
|
33
|
+
| `retryDelay` | `min(1000 * 2 ** failures, 30000)` | `(failures) => ms`. |
|
|
34
|
+
|
|
35
|
+
## Traps
|
|
36
|
+
|
|
37
|
+
- `run` rejects after the rollback: catch it, or read the error on the React hook.
|
|
38
|
+
- With `normalized()`, the optimistic data must include `__typename` and `id`.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Queries
|
|
2
|
+
|
|
3
|
+
## What it is for
|
|
4
|
+
|
|
5
|
+
`query(document, options?)` turns a typed document into a riva query definition: the variables and the data are inferred.
|
|
6
|
+
|
|
7
|
+
## Define and run
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { query } from '@softistx/riva-graphql';
|
|
11
|
+
import { PATIENTS_QUERY } from './graphql';
|
|
12
|
+
|
|
13
|
+
export const patients = query(PATIENTS_QUERY, { staleTime: '30s' });
|
|
14
|
+
|
|
15
|
+
await patients.ensure(client, { school: 'north' }); // variables typed from the document
|
|
16
|
+
const state = client.get(patients, { school: 'north' }); // state.data is the document's data
|
|
17
|
+
patients.invalidate(client, { school: 'north' });
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
A document with no variables takes none: `viewer.ensure(client)`.
|
|
21
|
+
|
|
22
|
+
## Options
|
|
23
|
+
|
|
24
|
+
`GraphQLQueryOptions` is the core's query options minus `fetch`, plus `id` and `cache`.
|
|
25
|
+
|
|
26
|
+
| Option | Default | Meaning |
|
|
27
|
+
|---|---|---|
|
|
28
|
+
| `id` | the persisted id, else the operation name | Override to define one document twice with different options. |
|
|
29
|
+
| `staleTime` | core default | How long data is fresh. |
|
|
30
|
+
| `gcTime` | core default | How long unobserved data stays. |
|
|
31
|
+
| `retry` | 3 in a browser, 0 on the server | A count, or `(failures, error) => boolean`. |
|
|
32
|
+
| `retryDelay` | `min(1000 * 2 ** failures, 30000)` | `(failures) => ms`. |
|
|
33
|
+
| `refetchOnFocus`, `refetchOnReconnect` | core default | Refetch when the window refocuses or the network returns. |
|
|
34
|
+
| `refetchEvery` | none | Polling interval. |
|
|
35
|
+
| `cache` | none | A core policy or an `OperationCache` such as `normalized()` ([cache](./cache.md)). |
|
|
36
|
+
|
|
37
|
+
Each one is documented in the [core guide](../../../riva/docs/guide/queries.md).
|
|
38
|
+
|
|
39
|
+
## Traps
|
|
40
|
+
|
|
41
|
+
- **Ids must be unique.** Two definitions with the same `id` (an override reused by mistake, or two documents sharing an operation name) are refused on the server with a `RivaError` (`RIVA_KEY`): define each query once and import it, or pass a distinct `id`. In a browser a hot reload takes over.
|
|
42
|
+
- A document that is not one named query is refused with a `RivaError` (`RIVA_DEFINITION`) when the definition is created.
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Transport
|
|
2
|
+
|
|
3
|
+
## What it is for
|
|
4
|
+
|
|
5
|
+
Definitions are stateless; the transport lives on the `RivaClient` that runs them. Give each client one when you create it.
|
|
6
|
+
|
|
7
|
+
## Provide it
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { createGraphQLClient } from '@softistx/graphql-client';
|
|
11
|
+
import { createRivaClient } from '@softistx/riva';
|
|
12
|
+
import { graphqlClientTransport, provideGraphQL } from '@softistx/riva-graphql';
|
|
13
|
+
|
|
14
|
+
const gql = createGraphQLClient({ url: 'https://api.example.com/graphql' });
|
|
15
|
+
|
|
16
|
+
export const client = createRivaClient();
|
|
17
|
+
provideGraphQL(client, graphqlClientTransport(gql));
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
On the server, build both inside the request handler so the caller's credentials stay with the request.
|
|
21
|
+
|
|
22
|
+
## `graphqlClientTransport(gql)`
|
|
23
|
+
|
|
24
|
+
Sends a query through `gql.query` and a mutation through `gql.mutate`, with the definition's `AbortSignal`. The client's own `persisted` option decides whether the text or the persisted id is sent:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
const gql = createGraphQLClient({
|
|
28
|
+
url: 'https://api.example.com/graphql',
|
|
29
|
+
persisted: { mode: 'documentId' }, // the preset's persistedDocuments hash
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## A transport of your own
|
|
34
|
+
|
|
35
|
+
Any object with `execute(operation, variables, { signal })` is a transport. The `operation` carries:
|
|
36
|
+
|
|
37
|
+
| Field | Meaning |
|
|
38
|
+
|---|---|
|
|
39
|
+
| `document` | The typed document. |
|
|
40
|
+
| `name` | The operation's name. |
|
|
41
|
+
| `kind` | `'query'` or `'mutation'`. |
|
|
42
|
+
| `persistedId` | The preset's hash, when there is one. |
|
|
43
|
+
| `id` | The persisted id, else the name. |
|
|
44
|
+
|
|
45
|
+
A transport that only posts an id and variables to a BFF route fits. Map its failures with `toRivaError(error, signal?)`.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { provideGraphQL, toRivaError, type GraphQLTransport } from '@softistx/riva-graphql';
|
|
49
|
+
|
|
50
|
+
const transport: GraphQLTransport = {
|
|
51
|
+
async execute(operation, variables, { signal }) {
|
|
52
|
+
try {
|
|
53
|
+
const res = await fetch('/_data', {
|
|
54
|
+
method: 'POST',
|
|
55
|
+
signal,
|
|
56
|
+
headers: { 'content-type': 'application/json' },
|
|
57
|
+
body: JSON.stringify({ id: operation.id, variables }),
|
|
58
|
+
});
|
|
59
|
+
return (await res.json()).data;
|
|
60
|
+
} catch (error) {
|
|
61
|
+
throw toRivaError(error, signal);
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
provideGraphQL(client, transport);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Traps
|
|
69
|
+
|
|
70
|
+
- A definition that runs on a client with no transport throws a `RivaScopeError` naming `provideGraphQL`.
|
|
71
|
+
- Providing a transport on one client never affects another: it is per client.
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# Troubleshooting
|
|
2
|
+
|
|
3
|
+
Each entry is headed by the message you will see.
|
|
4
|
+
|
|
5
|
+
## `No GraphQL transport on this RivaClient: call provideGraphQL(client, graphqlClientTransport(gqlClient)) when you create it.`
|
|
6
|
+
|
|
7
|
+
Code: `RIVA_SCOPE` (`RivaScopeError`).
|
|
8
|
+
|
|
9
|
+
**When:** a query, mutation or infinite list ran on a `RivaClient` that was never given a transport.
|
|
10
|
+
|
|
11
|
+
**Why:** definitions are stateless and the transport lives on the client, so two server requests never share one.
|
|
12
|
+
|
|
13
|
+
**Fix:** provide it where the client is created, per request on the server.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { createGraphQLClient } from '@softistx/graphql-client';
|
|
17
|
+
import { createRivaClient } from '@softistx/riva';
|
|
18
|
+
import { graphqlClientTransport, provideGraphQL } from '@softistx/riva-graphql';
|
|
19
|
+
|
|
20
|
+
const client = createRivaClient();
|
|
21
|
+
provideGraphQL(client, graphqlClientTransport(createGraphQLClient({ url: 'https://api.example.com/graphql' })));
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## `riva-graphql: the operation has no name; name it (query Patients { ... }) so it has an id.`
|
|
25
|
+
|
|
26
|
+
Code: `RIVA_DEFINITION` (`RivaError`), thrown when the definition is created.
|
|
27
|
+
|
|
28
|
+
**Why:** the definition's id, its cache keys and the allowlist's ids come from the operation name (or its persisted id), so an anonymous operation has none.
|
|
29
|
+
|
|
30
|
+
**Fix:** name the operation: `query Patients { ... }`, not `{ ... }`.
|
|
31
|
+
|
|
32
|
+
## `riva-graphql: the document holds N operations; define one operation per document.`
|
|
33
|
+
|
|
34
|
+
**Why:** one definition is one operation. Fragments may share the document; a second operation may not.
|
|
35
|
+
|
|
36
|
+
**Fix:** split the document, one constant per operation (`PATIENTS_QUERY`, `PATIENT_QUERY`).
|
|
37
|
+
|
|
38
|
+
## `riva-graphql: the document holds no operation (a fragment alone cannot be queried).`
|
|
39
|
+
|
|
40
|
+
**Fix:** pass the query or mutation document, not the fragment's.
|
|
41
|
+
|
|
42
|
+
## `riva-graphql: X is a mutation; use mutation() to define it, not query().`
|
|
43
|
+
|
|
44
|
+
**Fix:** use the helper that matches the document: `query()` and `infinite()` for queries, `mutation()` for mutations. Subscriptions are not supported yet (`riva-graphql: X is a subscription; …`).
|
|
45
|
+
|
|
46
|
+
## `riva-graphql: expected a TypedDocumentNode (set documentMode: "documentNode" in the client preset), got something else.`
|
|
47
|
+
|
|
48
|
+
**When:** the document is a string or a `TypedDocumentString`.
|
|
49
|
+
|
|
50
|
+
**Why:** a definition reads the operation's name and kind from the parsed document, without importing `graphql` at runtime.
|
|
51
|
+
|
|
52
|
+
**Fix:** generate documents as `TypedDocumentNode` (the client preset's default `documentMode`).
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
// codegen.ts
|
|
56
|
+
generates: { 'app/generated/': { preset: 'client', presetConfig: { persistedDocuments: true } } }
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## `@softistx/riva-graphql/codegen: the operation "X" is defined twice (sha256:a… and sha256:b…); operation names must be unique.`
|
|
60
|
+
|
|
61
|
+
**Why:** operation names are the ids of `ids: 'name'` (and the runtime's, without persisted ids), so they are unique.
|
|
62
|
+
|
|
63
|
+
**Fix:** rename one of them.
|
|
64
|
+
|
|
65
|
+
Related: `the persisted document <hash> must hold exactly one operation.` and `… must be a named query or mutation.` come from a text with no operation, several, an anonymous one or a subscription. Fix the document that produced it.
|
|
66
|
+
|
|
67
|
+
## A query is refused by the BFF with an unknown id
|
|
68
|
+
|
|
69
|
+
**When:** the `/_data` route does not know the id a definition sends.
|
|
70
|
+
|
|
71
|
+
**Why:** the runtime id is the document's `__meta__.hash` (the client preset's `persistedDocuments`), else its operation name. The manifest keeps the keys of the preset's `persisted-documents.json` as they are, so they match, unless the manifest is stale (codegen ran without the `afterAllFileWrite` step), or documents carry no hash while the manifest is keyed by hash.
|
|
72
|
+
|
|
73
|
+
**Fix:** run `writeAllowlist` from `hooks.afterAllFileWrite` (see "Allowlist codegen" in the [README](../README.md#allowlist-codegen)) so it follows each codegen run. With no `persistedDocuments`, use `ids: 'name'`.
|
|
74
|
+
|
|
75
|
+
## `RIVA_DATA_*` refusals from the `/_data` route
|
|
76
|
+
|
|
77
|
+
Raised by `dataRoute` (`@softistx/riva-react-router/server`) and rethrown by `bffTransport` as a `RivaRequestError` with that `code` and `status`. The checks run in this order:
|
|
78
|
+
|
|
79
|
+
| Code | Status | When | Fix |
|
|
80
|
+
|---|---|---|---|
|
|
81
|
+
| `RIVA_DATA_METHOD` | 405 | A GET, or any method but POST | Call it through `bffTransport`; never link to it. |
|
|
82
|
+
| `RIVA_DATA_CROSS_SITE` | 403 | `Origin` is not the route's origin, or `Sec-Fetch-Site` is cross-site | Call from the same origin; behind a proxy set `origin` on `dataRoute`. |
|
|
83
|
+
| `RIVA_DATA_MEDIA_TYPE` | 415 | `content-type` is not `application/json` | Do not override the header in `bffTransport`'s `headers`. |
|
|
84
|
+
| `RIVA_DATA_TOO_LARGE` | 413 | The body is over `maxBytes` (262144) | Send less, or raise `maxBytes`. |
|
|
85
|
+
| `RIVA_DATA_BAD_REQUEST` | 400 | The body is not `{ "id": string, "variables"?: object }` | Use `bffTransport`. |
|
|
86
|
+
| `RIVA_DATA_UNKNOWN` | 404 | No source knows the id | See below. |
|
|
87
|
+
| `RIVA_DATA_INTERNAL` | 500 | An operation threw something that is not a `RivaRequestError` | The browser sees only this generic answer; read the cause through `dataRoute`'s `onError`, which also receives the API's network and HTTP failures (sent to the browser with their own code and the message "The API is unavailable."). |
|
|
88
|
+
|
|
89
|
+
## `The data route answered <status> with a body that is not JSON.`
|
|
90
|
+
|
|
91
|
+
Code: `RIVA_INVALID_RESPONSE` (`RivaRequestError`), or `RivaHttpError` (`HTTP_<status>`) when the body is JSON without the route's `{ error }` shape.
|
|
92
|
+
|
|
93
|
+
**When:** `bffTransport` got a reply `dataRoute` did not write: a proxy's 502 or 504 HTML page, a login redirect, a CDN error, or a `/_data` URL that no route serves.
|
|
94
|
+
|
|
95
|
+
**Why:** the transport reads `{ data }` or `{ error: { code, message, status } }`; anything else is not the route's answer, so it keeps the HTTP status and nothing of the body.
|
|
96
|
+
|
|
97
|
+
**Fix:** check that `url` is the route's path and that the proxy forwards POSTs to it untouched. 5xx, 408 and 429 are marked retryable, so the query retries on its own.
|
|
98
|
+
|
|
99
|
+
## 404 on an operation that exists
|
|
100
|
+
|
|
101
|
+
**When:** `RIVA_DATA_UNKNOWN` for a query or mutation the app defines.
|
|
102
|
+
|
|
103
|
+
**Why:** the id the browser sends (the document's `__meta__.hash`, else its operation name) is not a key of the manifest. Either the manifest is stale, or it was built with the other `ids` mode (`'persisted'` keys by hash, `'name'` by operation name).
|
|
104
|
+
|
|
105
|
+
**Fix:** rebuild the manifest from codegen's `afterAllFileWrite` hook with the `ids` mode that matches the documents (see "Allowlist codegen" in the [README](../README.md#allowlist-codegen)).
|
|
106
|
+
|
|
107
|
+
## `ENOENT` or a `SyntaxError` from `writeAllowlist` reading `persisted-documents.json`
|
|
108
|
+
|
|
109
|
+
**When:** `writeAllowlist` (or the `afterAllFileWrite` hook running it) fails with a missing file, or invalid JSON.
|
|
110
|
+
|
|
111
|
+
**Why:** the client preset writes `persisted-documents.json` only with `persistedDocuments: true`, and `writeAllowlist` reads it. The hook runs after all files are written, so a failed or partial codegen run, a wrong path or a preset without the option leaves nothing valid to read.
|
|
112
|
+
|
|
113
|
+
**Fix:** set `presetConfig: { persistedDocuments: true }` on the preset, check that the path passed to `writeAllowlist` is the preset's output folder plus `persisted-documents.json`, and fix the codegen error that came first.
|
|
114
|
+
|
|
115
|
+
## Two definitions share the id "<id>": define each query once (a module-level const) and import it, or pass a distinct id.
|
|
116
|
+
|
|
117
|
+
Code: `RIVA_KEY` (`RivaError`).
|
|
118
|
+
|
|
119
|
+
**Why:** a definition's `id` is the document's persisted id, else its operation name. Two definitions with the same `id` read on one client on the server throw `RIVA_KEY`; in a browser a hot reload takes over instead.
|
|
120
|
+
|
|
121
|
+
**Fix:** define each query once, as a module-level const, and import it. Pass a distinct `id` when you define one document twice with different options. See the [core entry](../../riva/docs/troubleshooting.md).
|
|
122
|
+
|
|
123
|
+
## An error has the API's code but `error.fields` is undefined
|
|
124
|
+
|
|
125
|
+
**Why:** `RivaRequestError` carries `code`, `status` and `retryable`; the refused fields, params and every GraphQL error stay on its `cause`, the `ApiError` of `@softistx/graphql-client`.
|
|
126
|
+
|
|
127
|
+
**Fix:** read `error.cause` (see "Errors" in the [README](../README.md#errors)).
|
|
128
|
+
|
|
129
|
+
## A query is retried although the API refused it
|
|
130
|
+
|
|
131
|
+
**Why:** a refusal carries `extensions.http.status` below 500, or none (read as 400): it is never retried. A 5xx, 408 or 429 is, like any other transport failure.
|
|
132
|
+
|
|
133
|
+
**Fix:** have the API send a 4xx `extensions.http.status` for a refusal, or decide yourself: `retry: (failures, error) => …`.
|
|
134
|
+
|
|
135
|
+
## `The GraphQL cache holds JSON: field X holds Y.`
|
|
136
|
+
|
|
137
|
+
Code: `RIVA_CACHE` (`RivaError`).
|
|
138
|
+
|
|
139
|
+
**Why:** `normalized()` stores plain JSON; a `Date`, a class instance, a function or a non-finite number in a result (a custom scalar parsed by the client) cannot be stored.
|
|
140
|
+
|
|
141
|
+
**Fix:** keep the scalar serialized in the result, or parse it at the point of use.
|
|
142
|
+
|
|
143
|
+
## `The GraphQL cache stores an operation's data: an object.`
|
|
144
|
+
|
|
145
|
+
Code: `RIVA_CACHE` (`RivaError`).
|
|
146
|
+
|
|
147
|
+
**Why:** the transport returned something other than an object for the operation's data.
|
|
148
|
+
|
|
149
|
+
**Fix:** check the transport; a GraphQL result's `data` is an object.
|
|
150
|
+
|
|
151
|
+
## `riva-graphql: the document uses the fragment X but does not define it` and `riva-graphql: the document holds no operation`
|
|
152
|
+
|
|
153
|
+
Code: `RIVA_DEFINITION` (`RivaError`), thrown when `query()` or `mutation()` is called.
|
|
154
|
+
|
|
155
|
+
**Why:** the document given to `query()` or `mutation()` with a `normalized()` cache spreads a fragment it does not define, or defines no operation.
|
|
156
|
+
|
|
157
|
+
**Fix:** pass the preset's document, which carries the fragments it uses.
|
|
158
|
+
|
|
159
|
+
## `riva-graphql: the field "X" selects no __typename, which the cache needs to identify objects: …`
|
|
160
|
+
|
|
161
|
+
Code: `RIVA_DEFINITION` (`RivaError`), thrown when `query()` or `mutation()` is called with a `normalized()` cache.
|
|
162
|
+
|
|
163
|
+
**Why:** an object with no `__typename` has no identity, so it could not be stored as an entity. The cache refuses the document rather than rewrite it: a persisted id is the hash of the document's text.
|
|
164
|
+
|
|
165
|
+
**Fix:** add the client preset's `addTypenameSelectionDocumentTransform` to its `documentTransforms` (or select `__typename` yourself in every selection set below the root).
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
// codegen.ts
|
|
169
|
+
import { addTypenameSelectionDocumentTransform } from '@graphql-codegen/client-preset';
|
|
170
|
+
|
|
171
|
+
generates: {
|
|
172
|
+
'app/generated/': {
|
|
173
|
+
preset: 'client',
|
|
174
|
+
presetConfig: { persistedDocuments: true },
|
|
175
|
+
documentTransforms: [addTypenameSelectionDocumentTransform], // beside `preset`, not inside presetConfig
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## `The cache cannot read the query's data after a fetch.`
|
|
181
|
+
|
|
182
|
+
Code: `RIVA_CACHE` (`RivaError`), shown as the query's error status; `ensure` rejects with it.
|
|
183
|
+
|
|
184
|
+
**When:** a query with a `normalized()` cache fetched, and the result still reads as a miss.
|
|
185
|
+
|
|
186
|
+
**Why:** the server's answer lacks a selected field (or an object the selection needs), so the whole result reads `undefined`: the cache never serves half a result. A second fetch would give the same answer, so it stops instead of looping. A query already showing data whose entity is later evicted is refetched on its own.
|
|
187
|
+
|
|
188
|
+
**Fix:** have the API return every selected field (`null` for an empty one), or remove it from the selection. The generic explanation is on the [core page](../../riva/docs/troubleshooting.md).
|
|
189
|
+
|
|
190
|
+
## `This client already uses other entity identity rules…`
|
|
191
|
+
|
|
192
|
+
Code: `RIVA_CACHE` (`RivaError`).
|
|
193
|
+
|
|
194
|
+
**Why:** two `normalized()` (or `entities()`) policies with different `keys` or `identify` were used on one client.
|
|
195
|
+
|
|
196
|
+
**Fix:** create one `normalized(options)` and pass it to every definition. The core explains it in [its troubleshooting page](../../riva/docs/troubleshooting.md#this-client-already-uses-other-entity-identity-rules).
|
|
197
|
+
|
|
198
|
+
## A query fetches although another query showed the same entity
|
|
199
|
+
|
|
200
|
+
**Why:** each query entry has its own root: which fields it selected and with which arguments. A query's first fetch is unconditional, and it reads from the store afterwards. Later, a field or entity its selection needs that no write has stored (an argument set not yet fetched: `friends({"first":2})` is not `friends({"first":1})`) makes its data `undefined`, and an observed query refetches once.
|
|
201
|
+
|
|
202
|
+
**Fix:** expected: the query fetches, then reads from the store.
|
|
203
|
+
|
|
204
|
+
## `riva-graphql: X is a subscription; subscriptions are not supported yet.`
|
|
205
|
+
|
|
206
|
+
Code: `RIVA_DEFINITION` (`RivaError`), thrown when the definition is created (`X` is the operation's name).
|
|
207
|
+
|
|
208
|
+
**Why:** a definition runs one request and one reply; a subscription is a stream.
|
|
209
|
+
|
|
210
|
+
**Fix:** use a query or a mutation. Poll with `refetchEvery` where a stream would have refreshed the data.
|
package/package.json
CHANGED
|
@@ -1,6 +1,105 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@softistx/riva-graphql",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "GraphQL transport for riva over @softistx/graphql-client, with persisted ids and the allowlist codegen plugin.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"docs",
|
|
13
|
+
"README.md",
|
|
14
|
+
"package.json",
|
|
15
|
+
"LICENSE"
|
|
16
|
+
],
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./infinite": {
|
|
24
|
+
"types": "./dist/infinite/index.d.ts",
|
|
25
|
+
"import": "./dist/infinite/index.js",
|
|
26
|
+
"default": "./dist/infinite/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./cache": {
|
|
29
|
+
"types": "./dist/cache/index.d.ts",
|
|
30
|
+
"import": "./dist/cache/index.js",
|
|
31
|
+
"default": "./dist/cache/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./codegen": {
|
|
34
|
+
"types": "./dist/codegen/index.d.ts",
|
|
35
|
+
"import": "./dist/codegen/index.js",
|
|
36
|
+
"default": "./dist/codegen/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./server": {
|
|
39
|
+
"types": "./dist/server/index.d.ts",
|
|
40
|
+
"import": "./dist/server/index.js",
|
|
41
|
+
"default": "./dist/server/index.js"
|
|
42
|
+
},
|
|
43
|
+
"./bff": {
|
|
44
|
+
"types": "./dist/bff/index.d.ts",
|
|
45
|
+
"import": "./dist/bff/index.js",
|
|
46
|
+
"default": "./dist/bff/index.js"
|
|
47
|
+
},
|
|
48
|
+
"./package.json": "./package.json"
|
|
49
|
+
},
|
|
50
|
+
"keywords": [
|
|
51
|
+
"state",
|
|
52
|
+
"signals",
|
|
53
|
+
"cache",
|
|
54
|
+
"graphql",
|
|
55
|
+
"rest",
|
|
56
|
+
"ssr"
|
|
57
|
+
],
|
|
58
|
+
"repository": {
|
|
59
|
+
"type": "git",
|
|
60
|
+
"url": "git+https://github.com/softistx/riva.git",
|
|
61
|
+
"directory": "packages/riva-graphql"
|
|
62
|
+
},
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"registry": "https://registry.npmjs.org/",
|
|
65
|
+
"access": "public"
|
|
66
|
+
},
|
|
67
|
+
"scripts": {
|
|
68
|
+
"build": "bun run ../../build.ts",
|
|
69
|
+
"test": "bun test src",
|
|
70
|
+
"typecheck": "bun check"
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"@graphql-codegen/cli": "^8.0.0",
|
|
74
|
+
"@graphql-codegen/client-preset": "^7.0.0",
|
|
75
|
+
"@graphql-typed-document-node/core": "^3.2.0",
|
|
76
|
+
"@softistx/graphql-client": "^0.10.0",
|
|
77
|
+
"@softistx/riva": "0.1.0",
|
|
78
|
+
"@types/bun": "^1.4.5",
|
|
79
|
+
"graphql": "^17.0.0"
|
|
80
|
+
},
|
|
81
|
+
"peerDependencies": {
|
|
82
|
+
"@softistx/riva": "^0.1.0",
|
|
83
|
+
"@softistx/graphql-client": "^0.9.0 || ^0.10.0",
|
|
84
|
+
"@graphql-typed-document-node/core": "^3.2.0",
|
|
85
|
+
"graphql": "^16.11.0 || ^17.0.0"
|
|
86
|
+
},
|
|
87
|
+
"peerDependenciesMeta": {
|
|
88
|
+
"@graphql-typed-document-node/core": {
|
|
89
|
+
"optional": true
|
|
90
|
+
},
|
|
91
|
+
"graphql": {
|
|
92
|
+
"optional": true
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
"nxgt": {
|
|
96
|
+
"entrypoints": [
|
|
97
|
+
"src/index.ts",
|
|
98
|
+
"src/infinite/index.ts",
|
|
99
|
+
"src/cache/index.ts",
|
|
100
|
+
"src/codegen/index.ts",
|
|
101
|
+
"src/server/index.ts",
|
|
102
|
+
"src/bff/index.ts"
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
}
|