@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,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/errors.ts", "../src/client-transport.ts", "../src/query.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import {\n\tisRetryableStatus,\n\tRivaAbortError,\n\tRivaHttpError,\n\tRivaNetworkError,\n\tRivaRequestError,\n} from '@softistx/riva';\n\n// The graphql-client errors are read by name, not by `instanceof`, so a second\n// copy of the package (or none imported here) still maps, and this entry\n// never imports the client at runtime.\ntype Failure = Error & {\n\terrors?: unknown;\n\tcode?: string;\n\tstatus?: number;\n\treason?: string;\n};\n\nconst isNamed = (e: unknown, name: string): e is Failure =>\n\te instanceof Error && e.name === name;\n\n/**\n * Maps what `@softistx/graphql-client` throws to riva's errors, so the\n * core's retry policy and `instanceof` checks work:\n *\n * - the caller's abort: `RivaAbortError`;\n * - `ApiError` (the response carried `errors`): `RivaRequestError` with the\n * API's `extensions.code` untouched, its `status`, retryable only for 5xx,\n * 408 and 429; the `ApiError` is its `cause`;\n * - `ApiStatusError`: `RivaHttpError`;\n * - `ApiUnavailableError`: `RivaNetworkError` (unreachable, timeout) or a\n * non-retryable `RivaRequestError` (`RIVA_INVALID_RESPONSE`).\n *\n * Anything else is returned as it is.\n */\nexport function toRivaError(error: unknown, signal?: AbortSignal): unknown {\n\tif (error instanceof RivaAbortError) return error;\n\tif (\n\t\tsignal?.aborted ||\n\t\t(error instanceof Error && error.name === 'AbortError')\n\t)\n\t\treturn new RivaAbortError();\n\tif (isNamed(error, 'ApiError') && Array.isArray(error.errors)) {\n\t\tconst status = error.status ?? 400;\n\t\treturn new RivaRequestError(error.code ?? 'GRAPHQL_ERROR', error.message, {\n\t\t\tstatus,\n\t\t\tretryable: isRetryableStatus(status),\n\t\t\tcause: error,\n\t\t});\n\t}\n\tif (isNamed(error, 'ApiStatusError') && typeof error.status === 'number')\n\t\treturn new RivaHttpError(error.status, error.message, { cause: error });\n\tif (isNamed(error, 'ApiUnavailableError')) {\n\t\tif (error.reason === 'invalid-response')\n\t\t\treturn new RivaRequestError('RIVA_INVALID_RESPONSE', error.message, {\n\t\t\t\tretryable: false,\n\t\t\t\tcause: error,\n\t\t\t});\n\t\treturn new RivaNetworkError(error.message, { cause: error });\n\t}\n\treturn error;\n}\n",
|
|
6
|
+
"import type { GraphQLClient } from '@softistx/graphql-client';\nimport { toRivaError } from './errors';\nimport type { GraphQLTransport } from './transport';\n\n/**\n * The transport over an `@softistx/graphql-client` client: a query goes\n * through `client.query`, a mutation through `client.mutate`, with the\n * definition's signal. The client's own `persisted` option decides whether the\n * persisted id or the text is sent. Its errors are mapped by `toRivaError`.\n */\nexport function graphqlClientTransport(\n\tclient: GraphQLClient,\n): GraphQLTransport {\n\treturn {\n\t\tasync execute(operation, variables, { signal }) {\n\t\t\tconst call = [operation.document, variables, { signal }] as never as [\n\t\t\t\tnever,\n\t\t\t\tnever,\n\t\t\t\t{ signal: AbortSignal },\n\t\t\t];\n\t\t\ttry {\n\t\t\t\treturn (await (operation.kind === 'mutation'\n\t\t\t\t\t? client.mutate(...call)\n\t\t\t\t\t: client.query(...call))) as never;\n\t\t\t} catch (error) {\n\t\t\t\tthrow toRivaError(error, signal);\n\t\t\t}\n\t\t},\n\t};\n}\n",
|
|
7
|
+
"import type { TypedDocumentNode } from '@graphql-typed-document-node/core';\nimport {\n\ttype CachePolicy,\n\tmutation as coreMutation,\n\tquery as coreQuery,\n\ttype Mutation,\n\ttype MutationOptions,\n\ttype Query,\n\ttype QueryOptions,\n} from '@softistx/riva';\nimport type { DocumentNode } from 'graphql';\nimport { type Operation, operationOf } from './operation';\nimport { fetchOf } from './run';\nimport type { VarsOf } from './types';\n\n/** What a cache binds to: the operation a definition was made from. */\nexport type BoundOperation = Pick<Operation, 'kind' | 'name' | 'id'> & {\n\treadonly document: DocumentNode;\n};\n\n/**\n * A cache that needs the document to read and write its data, such as\n * `normalized()` from `@softistx/riva-graphql/cache`: the definition binds it\n * to its operation.\n */\nexport interface OperationCache {\n\tforOperation(operation: BoundOperation): CachePolicy;\n}\n\n/** The policy of a `cache` option: a core policy as is, an operation cache bound to `op`. */\nconst policyOf = (\n\tcache: CachePolicy | OperationCache | undefined,\n\top: BoundOperation,\n): { cache?: CachePolicy } => {\n\tconst policy =\n\t\tcache && 'forOperation' in cache ? cache.forOperation(op) : cache;\n\treturn policy ? { cache: policy } : {};\n};\n\n/** The core's query options, minus what the document gives. */\nexport type GraphQLQueryOptions = Omit<\n\tQueryOptions<unknown, unknown>,\n\t'id' | 'fetch' | 'derive' | 'cache'\n> & {\n\t/** Replaces the id derived from the document (to define one document twice with different options). */\n\tid?: string;\n\t/** How the data is stored: a core policy, or `normalized()` from `@softistx/riva-graphql/cache`. */\n\tcache?: CachePolicy | OperationCache;\n};\n\nexport type GraphQLMutationOptions<Vars, Data> = Omit<\n\tMutationOptions<Vars, Data>,\n\t'id' | 'run' | 'cache'\n> & {\n\t/** Replaces the id derived from the document. */\n\tid?: string;\n\t/** Where the result and the optimistic data go: a core policy, or `normalized()`. */\n\tcache?: CachePolicy | OperationCache;\n};\n\n/**\n * A query definition from a typed document: variables and data are the\n * document's, never annotated. Its id is the document's persisted id, else\n * its operation name. Throws a `TypeError` for a document that is not exactly\n * one named query.\n */\nexport function query<Data, Vars>(\n\tdocument: TypedDocumentNode<Data, Vars>,\n\topts: GraphQLQueryOptions = {},\n): Query<VarsOf<Vars>, Data> {\n\tconst op = operationOf(document, 'query');\n\tconst { id, cache, ...rest } = opts;\n\treturn coreQuery<VarsOf<Vars>, Data>({\n\t\t...rest,\n\t\t...policyOf(cache, op),\n\t\tid: id ?? op.id,\n\t\tfetch: fetchOf(op) as QueryOptions<VarsOf<Vars>, Data>['fetch'],\n\t});\n}\n\n/** A mutation definition from a typed document; the id is derived as for `query`. */\nexport function mutation<Data, Vars>(\n\tdocument: TypedDocumentNode<Data, Vars>,\n\topts: GraphQLMutationOptions<VarsOf<Vars>, Data> = {},\n): Mutation<VarsOf<Vars>, Data> {\n\tconst op = operationOf(document, 'mutation');\n\tconst { id, cache, ...rest } = opts;\n\treturn coreMutation<VarsOf<Vars>, Data>({\n\t\t...rest,\n\t\t...policyOf(cache, op),\n\t\tid: id ?? op.id,\n\t\trun: fetchOf(op) as MutationOptions<VarsOf<Vars>, Data>['run'],\n\t});\n}\n"
|
|
8
|
+
],
|
|
9
|
+
"mappings": ";;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBA,IAAM,UAAU,CAAC,GAAY,SAC5B,aAAa,SAAS,EAAE,SAAS;AAgB3B,SAAS,WAAW,CAAC,OAAgB,QAA+B;AAAA,EAC1E,IAAI,iBAAiB;AAAA,IAAgB,OAAO;AAAA,EAC5C,IACC,QAAQ,WACP,iBAAiB,SAAS,MAAM,SAAS;AAAA,IAE1C,OAAO,IAAI;AAAA,EACZ,IAAI,QAAQ,OAAO,UAAU,KAAK,MAAM,QAAQ,MAAM,MAAM,GAAG;AAAA,IAC9D,MAAM,SAAS,MAAM,UAAU;AAAA,IAC/B,OAAO,IAAI,iBAAiB,MAAM,QAAQ,iBAAiB,MAAM,SAAS;AAAA,MACzE;AAAA,MACA,WAAW,kBAAkB,MAAM;AAAA,MACnC,OAAO;AAAA,IACR,CAAC;AAAA,EACF;AAAA,EACA,IAAI,QAAQ,OAAO,gBAAgB,KAAK,OAAO,MAAM,WAAW;AAAA,IAC/D,OAAO,IAAI,cAAc,MAAM,QAAQ,MAAM,SAAS,EAAE,OAAO,MAAM,CAAC;AAAA,EACvE,IAAI,QAAQ,OAAO,qBAAqB,GAAG;AAAA,IAC1C,IAAI,MAAM,WAAW;AAAA,MACpB,OAAO,IAAI,iBAAiB,yBAAyB,MAAM,SAAS;AAAA,QACnE,WAAW;AAAA,QACX,OAAO;AAAA,MACR,CAAC;AAAA,IACF,OAAO,IAAI,iBAAiB,MAAM,SAAS,EAAE,OAAO,MAAM,CAAC;AAAA,EAC5D;AAAA,EACA,OAAO;AAAA;;;AClDD,SAAS,sBAAsB,CACrC,QACmB;AAAA,EACnB,OAAO;AAAA,UACA,OAAO,CAAC,WAAW,aAAa,UAAU;AAAA,MAC/C,MAAM,OAAO,CAAC,UAAU,UAAU,WAAW,EAAE,OAAO,CAAC;AAAA,MAKvD,IAAI;AAAA,QACH,OAAQ,OAAO,UAAU,SAAS,aAC/B,OAAO,OAAO,GAAG,IAAI,IACrB,OAAO,MAAM,GAAG,IAAI;AAAA,QACtB,OAAO,OAAO;AAAA,QACf,MAAM,YAAY,OAAO,MAAM;AAAA;AAAA;AAAA,EAGlC;AAAA;;AC3BD;AAAA,cAEC;AAAA,WACA;AAAA;AA0BD,IAAM,WAAW,CAChB,OACA,OAC6B;AAAA,EAC7B,MAAM,SACL,SAAS,kBAAkB,QAAQ,MAAM,aAAa,EAAE,IAAI;AAAA,EAC7D,OAAO,SAAS,EAAE,OAAO,OAAO,IAAI,CAAC;AAAA;AA8B/B,SAAS,KAAiB,CAChC,UACA,OAA4B,CAAC,GACD;AAAA,EAC5B,MAAM,KAAK,aAAY,UAAU,OAAO;AAAA,EACxC,QAAQ,IAAI,UAAU,SAAS;AAAA,EAC/B,OAAO,UAA8B;AAAA,OACjC;AAAA,OACA,SAAS,OAAO,EAAE;AAAA,IACrB,IAAI,MAAM,GAAG;AAAA,IACb,OAAO,QAAQ,EAAE;AAAA,EAClB,CAAC;AAAA;AAIK,SAAS,QAAoB,CACnC,UACA,OAAmD,CAAC,GACrB;AAAA,EAC/B,MAAM,KAAK,aAAY,UAAU,UAAU;AAAA,EAC3C,QAAQ,IAAI,UAAU,SAAS;AAAA,EAC/B,OAAO,aAAiC;AAAA,OACpC;AAAA,OACA,SAAS,OAAO,EAAE;AAAA,IACrB,IAAI,MAAM,GAAG;AAAA,IACb,KAAK,QAAQ,EAAE;AAAA,EAChB,CAAC;AAAA;",
|
|
10
|
+
"debugId": "62F134F7C948C7F564756E2164756E21",
|
|
11
|
+
"names": []
|
|
12
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
|
|
2
|
+
import { type FeedVars, type Infinite, type InfiniteOptions } from '@softistx/riva/infinite';
|
|
3
|
+
export type GraphQLInfiniteOptions<Vars, Data, Node, A extends string = 'after', B extends string = 'before'> = Omit<InfiniteOptions<Vars, Data, Node, A, B>, 'id' | 'fetch'> & {
|
|
4
|
+
/** Replaces the id derived from the document. */
|
|
5
|
+
id?: string;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* A cursor-paginated list from a typed query document: the document's
|
|
9
|
+
* variables carry the cursors (`after`, `before`, or `cursor` names), a page
|
|
10
|
+
* is the document's data and `connection` reads the connection out of it.
|
|
11
|
+
* Its id is the query's id plus `#infinite`, so one document may back both
|
|
12
|
+
* `query()` and `infinite()` without sharing a cache entry.
|
|
13
|
+
*/
|
|
14
|
+
export declare function infinite<Data, Vars, Node, A extends string = 'after', B extends string = 'before'>(document: TypedDocumentNode<Data, Vars>, opts: GraphQLInfiniteOptions<Vars, Data, Node, A, B>): Infinite<FeedVars<Vars, A | B>, Data, Node>;
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/infinite/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAC3E,OAAO,EAEN,KAAK,QAAQ,EACb,KAAK,QAAQ,EACb,KAAK,eAAe,EACpB,MAAM,yBAAyB,CAAC;AAIjC,MAAM,MAAM,sBAAsB,CACjC,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,CAAC,SAAS,MAAM,GAAG,OAAO,EAC1B,CAAC,SAAS,MAAM,GAAG,QAAQ,IACxB,IAAI,CAAC,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,IAAI,GAAG,OAAO,CAAC,GAAG;IACnE,iDAAiD;IACjD,EAAE,CAAC,EAAE,MAAM,CAAC;CACZ,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,QAAQ,CACvB,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,CAAC,SAAS,MAAM,GAAG,OAAO,EAC1B,CAAC,SAAS,MAAM,GAAG,QAAQ,EAE3B,QAAQ,EAAE,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,EACvC,IAAI,EAAE,sBAAsB,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,GAClD,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,CAQ7C"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {
|
|
2
|
+
operationOf2
|
|
3
|
+
} from "../chunks/index-zpgy1hwx.js";
|
|
4
|
+
import {
|
|
5
|
+
fetchOf
|
|
6
|
+
} from "../chunks/index-etvmjkh7.js";
|
|
7
|
+
|
|
8
|
+
// src/infinite/index.ts
|
|
9
|
+
import {
|
|
10
|
+
infinite as coreInfinite
|
|
11
|
+
} from "@softistx/riva/infinite";
|
|
12
|
+
function infinite(document, opts) {
|
|
13
|
+
const op = operationOf2(document, "query");
|
|
14
|
+
const { id, ...rest } = opts;
|
|
15
|
+
return coreInfinite({
|
|
16
|
+
...rest,
|
|
17
|
+
id: id ?? `${op.id}#infinite`,
|
|
18
|
+
fetch: fetchOf(op)
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
infinite
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
//# debugId=F48D0DB27BEA23A764756E2164756E21
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/infinite/index.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type { TypedDocumentNode } from '@graphql-typed-document-node/core';\nimport {\n\tinfinite as coreInfinite,\n\ttype FeedVars,\n\ttype Infinite,\n\ttype InfiniteOptions,\n} from '@softistx/riva/infinite';\nimport { operationOf } from '../operation';\nimport { fetchOf } from '../run';\n\nexport type GraphQLInfiniteOptions<\n\tVars,\n\tData,\n\tNode,\n\tA extends string = 'after',\n\tB extends string = 'before',\n> = Omit<InfiniteOptions<Vars, Data, Node, A, B>, 'id' | 'fetch'> & {\n\t/** Replaces the id derived from the document. */\n\tid?: string;\n};\n\n/**\n * A cursor-paginated list from a typed query document: the document's\n * variables carry the cursors (`after`, `before`, or `cursor` names), a page\n * is the document's data and `connection` reads the connection out of it.\n * Its id is the query's id plus `#infinite`, so one document may back both\n * `query()` and `infinite()` without sharing a cache entry.\n */\nexport function infinite<\n\tData,\n\tVars,\n\tNode,\n\tA extends string = 'after',\n\tB extends string = 'before',\n>(\n\tdocument: TypedDocumentNode<Data, Vars>,\n\topts: GraphQLInfiniteOptions<Vars, Data, Node, A, B>,\n): Infinite<FeedVars<Vars, A | B>, Data, Node> {\n\tconst op = operationOf(document, 'query');\n\tconst { id, ...rest } = opts;\n\treturn coreInfinite<Vars, Data, Node, A, B>({\n\t\t...rest,\n\t\tid: id ?? `${op.id}#infinite`,\n\t\tfetch: fetchOf(op) as InfiniteOptions<Vars, Data, Node, A, B>['fetch'],\n\t});\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;;;;AACA;AAAA,cACC;AAAA;AA0BM,SAAS,QAMf,CACA,UACA,MAC8C;AAAA,EAC9C,MAAM,KAAK,aAAY,UAAU,OAAO;AAAA,EACxC,QAAQ,OAAO,SAAS;AAAA,EACxB,OAAO,aAAqC;AAAA,OACxC;AAAA,IACH,IAAI,MAAM,GAAG,GAAG;AAAA,IAChB,OAAO,QAAQ,EAAE;AAAA,EAClB,CAAC;AAAA;",
|
|
8
|
+
"debugId": "F48D0DB27BEA23A764756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
|
|
2
|
+
export type OperationKind = 'query' | 'mutation';
|
|
3
|
+
/**
|
|
4
|
+
* What a transport executes: the typed document and what was read from it.
|
|
5
|
+
* `id` names the operation to a server that holds an allowlist: its persisted
|
|
6
|
+
* id when the document carries one, else its name.
|
|
7
|
+
*/
|
|
8
|
+
export interface Operation<Data = unknown, Vars = unknown> {
|
|
9
|
+
readonly document: TypedDocumentNode<Data, Vars>;
|
|
10
|
+
/** The operation's name, unique in the application. */
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly kind: OperationKind;
|
|
13
|
+
/** The client preset's `__meta__.hash`, when the document carries one. */
|
|
14
|
+
readonly persistedId: string | undefined;
|
|
15
|
+
/** `persistedId`, else `name`. */
|
|
16
|
+
readonly id: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Reads a document once. Throws a `RivaError` (`RIVA_DEFINITION`) when it is not a document node,
|
|
20
|
+
* holds no operation, several, an unnamed one or a subscription, or when it
|
|
21
|
+
* is not of the `expected` kind.
|
|
22
|
+
*/
|
|
23
|
+
export declare function operationOf<Data, Vars>(document: TypedDocumentNode<Data, Vars>, expected?: OperationKind): Operation<Data, Vars>;
|
|
24
|
+
//# sourceMappingURL=operation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operation.d.ts","sourceRoot":"","sources":["../src/operation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAI3E,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,UAAU,CAAC;AAEjD;;;;GAIG;AACH,MAAM,WAAW,SAAS,CAAC,IAAI,GAAG,OAAO,EAAE,IAAI,GAAG,OAAO;IACxD,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACjD,uDAAuD;IACvD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAC7B,0EAA0E;IAC1E,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;IACzC,kCAAkC;IAClC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;CACpB;AAuDD;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,EACrC,QAAQ,EAAE,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,EACvC,QAAQ,CAAC,EAAE,aAAa,GACtB,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,CAYvB"}
|
package/dist/query.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
|
|
2
|
+
import { type CachePolicy, type Mutation, type MutationOptions, type Query, type QueryOptions } from '@softistx/riva';
|
|
3
|
+
import type { DocumentNode } from 'graphql';
|
|
4
|
+
import { type Operation } from './operation';
|
|
5
|
+
import type { VarsOf } from './types';
|
|
6
|
+
/** What a cache binds to: the operation a definition was made from. */
|
|
7
|
+
export type BoundOperation = Pick<Operation, 'kind' | 'name' | 'id'> & {
|
|
8
|
+
readonly document: DocumentNode;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* A cache that needs the document to read and write its data, such as
|
|
12
|
+
* `normalized()` from `@softistx/riva-graphql/cache`: the definition binds it
|
|
13
|
+
* to its operation.
|
|
14
|
+
*/
|
|
15
|
+
export interface OperationCache {
|
|
16
|
+
forOperation(operation: BoundOperation): CachePolicy;
|
|
17
|
+
}
|
|
18
|
+
/** The core's query options, minus what the document gives. */
|
|
19
|
+
export type GraphQLQueryOptions = Omit<QueryOptions<unknown, unknown>, 'id' | 'fetch' | 'derive' | 'cache'> & {
|
|
20
|
+
/** Replaces the id derived from the document (to define one document twice with different options). */
|
|
21
|
+
id?: string;
|
|
22
|
+
/** How the data is stored: a core policy, or `normalized()` from `@softistx/riva-graphql/cache`. */
|
|
23
|
+
cache?: CachePolicy | OperationCache;
|
|
24
|
+
};
|
|
25
|
+
export type GraphQLMutationOptions<Vars, Data> = Omit<MutationOptions<Vars, Data>, 'id' | 'run' | 'cache'> & {
|
|
26
|
+
/** Replaces the id derived from the document. */
|
|
27
|
+
id?: string;
|
|
28
|
+
/** Where the result and the optimistic data go: a core policy, or `normalized()`. */
|
|
29
|
+
cache?: CachePolicy | OperationCache;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* A query definition from a typed document: variables and data are the
|
|
33
|
+
* document's, never annotated. Its id is the document's persisted id, else
|
|
34
|
+
* its operation name. Throws a `TypeError` for a document that is not exactly
|
|
35
|
+
* one named query.
|
|
36
|
+
*/
|
|
37
|
+
export declare function query<Data, Vars>(document: TypedDocumentNode<Data, Vars>, opts?: GraphQLQueryOptions): Query<VarsOf<Vars>, Data>;
|
|
38
|
+
/** A mutation definition from a typed document; the id is derived as for `query`. */
|
|
39
|
+
export declare function mutation<Data, Vars>(document: TypedDocumentNode<Data, Vars>, opts?: GraphQLMutationOptions<VarsOf<Vars>, Data>): Mutation<VarsOf<Vars>, Data>;
|
|
40
|
+
//# sourceMappingURL=query.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAC3E,OAAO,EACN,KAAK,WAAW,EAGhB,KAAK,QAAQ,EACb,KAAK,eAAe,EACpB,KAAK,KAAK,EACV,KAAK,YAAY,EACjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,KAAK,SAAS,EAAe,MAAM,aAAa,CAAC;AAE1D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEtC,uEAAuE;AACvE,MAAM,MAAM,cAAc,GAAG,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC,GAAG;IACtE,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;CAChC,CAAC;AAEF;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC9B,YAAY,CAAC,SAAS,EAAE,cAAc,GAAG,WAAW,CAAC;CACrD;AAYD,+DAA+D;AAC/D,MAAM,MAAM,mBAAmB,GAAG,IAAI,CACrC,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC,EAC9B,IAAI,GAAG,OAAO,GAAG,QAAQ,GAAG,OAAO,CACnC,GAAG;IACH,uGAAuG;IACvG,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,oGAAoG;IACpG,KAAK,CAAC,EAAE,WAAW,GAAG,cAAc,CAAC;CACrC,CAAC;AAEF,MAAM,MAAM,sBAAsB,CAAC,IAAI,EAAE,IAAI,IAAI,IAAI,CACpD,eAAe,CAAC,IAAI,EAAE,IAAI,CAAC,EAC3B,IAAI,GAAG,KAAK,GAAG,OAAO,CACtB,GAAG;IACH,iDAAiD;IACjD,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,qFAAqF;IACrF,KAAK,CAAC,EAAE,WAAW,GAAG,cAAc,CAAC;CACrC,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAC/B,QAAQ,EAAE,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,EACvC,IAAI,GAAE,mBAAwB,GAC5B,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAS3B;AAED,qFAAqF;AACrF,wBAAgB,QAAQ,CAAC,IAAI,EAAE,IAAI,EAClC,QAAQ,EAAE,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,EACvC,IAAI,GAAE,sBAAsB,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,CAAM,GACnD,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAS9B"}
|
package/dist/run.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { FetchContext } from '@softistx/riva';
|
|
2
|
+
import type { Operation } from './operation';
|
|
3
|
+
/** The core's `fetch` / `run` of an operation: through the transport of the client in scope. */
|
|
4
|
+
export declare const fetchOf: <Data, Vars>(operation: Operation<Data, Vars>) => (vars: Vars, ctx: FetchContext) => Promise<Data>;
|
|
5
|
+
//# sourceMappingURL=run.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAG7C,gGAAgG;AAChG,eAAO,MAAM,OAAO,GAClB,IAAI,EAAE,IAAI,aAAa,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,YAChC,IAAI,OAAO,YAAY,KAAG,OAAO,CAAC,IAAI,CACsB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type DataOperation, type DataSource } from '@softistx/riva';
|
|
2
|
+
/**
|
|
3
|
+
* What `allowlist` reads: the manifest as a JSON import types it (`kind` is a
|
|
4
|
+
* `string`), so no assertion is needed. `AllowlistManifest` fits it.
|
|
5
|
+
*/
|
|
6
|
+
export type AllowlistInput = Readonly<Record<string, {
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly kind: string;
|
|
10
|
+
readonly document: string;
|
|
11
|
+
}>>;
|
|
12
|
+
/**
|
|
13
|
+
* The server side of `bffTransport`: runs the operations of `buildAllowlist`'s
|
|
14
|
+
* manifest through the request client's transport (`provideGraphQL`). Only the
|
|
15
|
+
* manifest's ids are known, and the text executed is the manifest's, never the
|
|
16
|
+
* browser's. Each document is parsed once, on first use.
|
|
17
|
+
*/
|
|
18
|
+
export declare function allowlist(manifest: AllowlistInput): DataSource;
|
|
19
|
+
export type { DataOperation, DataSource };
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,UAAU,EAAa,MAAM,gBAAgB,CAAC;AAKhF;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,QAAQ,CACpC,MAAM,CACL,MAAM,EACN;IACC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC1B,CACD,CACD,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,QAAQ,EAAE,cAAc,GAAG,UAAU,CAmC9D;AAED,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {
|
|
2
|
+
operationOf2,
|
|
3
|
+
transportOf
|
|
4
|
+
} from "../chunks/index-zpgy1hwx.js";
|
|
5
|
+
|
|
6
|
+
// src/server/index.ts
|
|
7
|
+
import { RivaError } from "@softistx/riva";
|
|
8
|
+
import { parse } from "graphql";
|
|
9
|
+
function allowlist(manifest) {
|
|
10
|
+
for (const id of Object.keys(manifest)) {
|
|
11
|
+
const kind = manifest[id]?.kind;
|
|
12
|
+
if (kind !== "query" && kind !== "mutation")
|
|
13
|
+
throw new RivaError("RIVA_DEFINITION", `riva-graphql: the allowlist entry "${id}" has the kind "${String(kind)}", not "query" or "mutation": rerun the codegen to rebuild the manifest.`);
|
|
14
|
+
}
|
|
15
|
+
const made = new Map;
|
|
16
|
+
return {
|
|
17
|
+
find(id) {
|
|
18
|
+
const entry = Object.hasOwn(manifest, id) ? manifest[id] : undefined;
|
|
19
|
+
if (!entry)
|
|
20
|
+
return;
|
|
21
|
+
let op = made.get(id);
|
|
22
|
+
if (!op) {
|
|
23
|
+
let operation;
|
|
24
|
+
op = {
|
|
25
|
+
kind: entry.kind,
|
|
26
|
+
run(client, variables, signal) {
|
|
27
|
+
operation ??= {
|
|
28
|
+
...operationOf2(parse(entry.document)),
|
|
29
|
+
id: entry.id
|
|
30
|
+
};
|
|
31
|
+
return transportOf(client).execute(operation, variables, {
|
|
32
|
+
signal
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
made.set(id, op);
|
|
37
|
+
}
|
|
38
|
+
return op;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export {
|
|
43
|
+
allowlist
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
//# debugId=0A52EB9D21B64B9964756E2164756E21
|
|
47
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/server/index.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import { type DataOperation, type DataSource, RivaError } from '@softistx/riva';\nimport { type DocumentNode, parse } from 'graphql';\nimport { type Operation, operationOf } from '../operation';\nimport { transportOf } from '../transport';\n\n/**\n * What `allowlist` reads: the manifest as a JSON import types it (`kind` is a\n * `string`), so no assertion is needed. `AllowlistManifest` fits it.\n */\nexport type AllowlistInput = Readonly<\n\tRecord<\n\t\tstring,\n\t\t{\n\t\t\treadonly id: string;\n\t\t\treadonly name: string;\n\t\t\treadonly kind: string;\n\t\t\treadonly document: string;\n\t\t}\n\t>\n>;\n\n/**\n * The server side of `bffTransport`: runs the operations of `buildAllowlist`'s\n * manifest through the request client's transport (`provideGraphQL`). Only the\n * manifest's ids are known, and the text executed is the manifest's, never the\n * browser's. Each document is parsed once, on first use.\n */\nexport function allowlist(manifest: AllowlistInput): DataSource {\n\tfor (const id of Object.keys(manifest)) {\n\t\tconst kind = manifest[id]?.kind;\n\t\tif (kind !== 'query' && kind !== 'mutation')\n\t\t\tthrow new RivaError(\n\t\t\t\t'RIVA_DEFINITION',\n\t\t\t\t`riva-graphql: the allowlist entry \"${id}\" has the kind \"${String(kind)}\", not \"query\" or \"mutation\": rerun the codegen to rebuild the manifest.`,\n\t\t\t);\n\t}\n\tconst made = new Map<string, DataOperation>();\n\treturn {\n\t\tfind(id) {\n\t\t\t// An own key only: `__proto__` or `toString` are not ids.\n\t\t\tconst entry = Object.hasOwn(manifest, id) ? manifest[id] : undefined;\n\t\t\tif (!entry) return undefined;\n\t\t\tlet op = made.get(id);\n\t\t\tif (!op) {\n\t\t\t\tlet operation: Operation | undefined;\n\t\t\t\top = {\n\t\t\t\t\tkind: entry.kind as DataOperation['kind'],\n\t\t\t\t\trun(client, variables, signal) {\n\t\t\t\t\t\toperation ??= {\n\t\t\t\t\t\t\t...operationOf(parse(entry.document) as DocumentNode),\n\t\t\t\t\t\t\tid: entry.id,\n\t\t\t\t\t\t};\n\t\t\t\t\t\treturn transportOf(client).execute(operation, variables, {\n\t\t\t\t\t\t\tsignal,\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t\tmade.set(id, op);\n\t\t\t}\n\t\t\treturn op;\n\t\t},\n\t};\n}\n\nexport type { DataOperation, DataSource };\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;;AAAA;AACA;AA0BO,SAAS,SAAS,CAAC,UAAsC;AAAA,EAC/D,WAAW,MAAM,OAAO,KAAK,QAAQ,GAAG;AAAA,IACvC,MAAM,OAAO,SAAS,KAAK;AAAA,IAC3B,IAAI,SAAS,WAAW,SAAS;AAAA,MAChC,MAAM,IAAI,UACT,mBACA,sCAAsC,qBAAqB,OAAO,IAAI,2EACvE;AAAA,EACF;AAAA,EACA,MAAM,OAAO,IAAI;AAAA,EACjB,OAAO;AAAA,IACN,IAAI,CAAC,IAAI;AAAA,MAER,MAAM,QAAQ,OAAO,OAAO,UAAU,EAAE,IAAI,SAAS,MAAM;AAAA,MAC3D,IAAI,CAAC;AAAA,QAAO;AAAA,MACZ,IAAI,KAAK,KAAK,IAAI,EAAE;AAAA,MACpB,IAAI,CAAC,IAAI;AAAA,QACR,IAAI;AAAA,QACJ,KAAK;AAAA,UACJ,MAAM,MAAM;AAAA,UACZ,GAAG,CAAC,QAAQ,WAAW,QAAQ;AAAA,YAC9B,cAAc;AAAA,iBACV,aAAY,MAAM,MAAM,QAAQ,CAAiB;AAAA,cACpD,IAAI,MAAM;AAAA,YACX;AAAA,YACA,OAAO,YAAY,MAAM,EAAE,QAAQ,WAAW,WAAW;AAAA,cACxD;AAAA,YACD,CAAC;AAAA;AAAA,QAEH;AAAA,QACA,KAAK,IAAI,IAAI,EAAE;AAAA,MAChB;AAAA,MACA,OAAO;AAAA;AAAA,EAET;AAAA;",
|
|
8
|
+
"debugId": "0A52EB9D21B64B9964756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type RivaClient } from '@softistx/riva';
|
|
2
|
+
import type { Operation } from './operation';
|
|
3
|
+
/**
|
|
4
|
+
* Runs one operation. `@softistx/riva-graphql` ships `graphqlClientTransport`;
|
|
5
|
+
* any object of this shape works (a transport that posts the id and the
|
|
6
|
+
* variables to a BFF route, a test double). It throws `RivaError`s.
|
|
7
|
+
*/
|
|
8
|
+
export interface GraphQLTransport {
|
|
9
|
+
execute<Data, Vars>(operation: Operation<Data, Vars>, variables: Vars, options: {
|
|
10
|
+
signal: AbortSignal;
|
|
11
|
+
}): Promise<Data>;
|
|
12
|
+
}
|
|
13
|
+
/** Gives `client` the transport its GraphQL definitions fetch through. */
|
|
14
|
+
export declare function provideGraphQL(client: RivaClient, transport: GraphQLTransport): void;
|
|
15
|
+
/** The transport `provideGraphQL` gave `client`; throws `RivaScopeError` when none. */
|
|
16
|
+
export declare function transportOf(client: RivaClient): GraphQLTransport;
|
|
17
|
+
//# sourceMappingURL=transport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAkB,MAAM,gBAAgB,CAAC;AACjE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C;;;;GAIG;AACH,MAAM,WAAW,gBAAgB;IAChC,OAAO,CAAC,IAAI,EAAE,IAAI,EACjB,SAAS,EAAE,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,EAChC,SAAS,EAAE,IAAI,EACf,OAAO,EAAE;QAAE,MAAM,EAAE,WAAW,CAAA;KAAE,GAC9B,OAAO,CAAC,IAAI,CAAC,CAAC;CACjB;AAKD,0EAA0E;AAC1E,wBAAgB,cAAc,CAC7B,MAAM,EAAE,UAAU,EAClB,SAAS,EAAE,gBAAgB,GACzB,IAAI,CAEN;AAED,uFAAuF;AACvF,wBAAgB,WAAW,CAAC,MAAM,EAAE,UAAU,GAAG,gBAAgB,CAOhE"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The variables a definition takes, from a document's variables type: `void`
|
|
3
|
+
* (so the argument is optional) when the operation has none.
|
|
4
|
+
*/
|
|
5
|
+
export type VarsOf<V> = [keyof V] extends [never] ? void : string extends keyof V ? [V[string]] extends [never] ? void : V : V;
|
|
6
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GAE/C,IAAI,GACH,MAAM,SAAS,MAAM,CAAC,GACrB,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GAE3B,IAAI,GACH,CAAC,GACF,CAAC,CAAC"}
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# riva-graphql docs
|
|
2
|
+
|
|
3
|
+
The package's [README](../README.md) is the short path. These pages go deeper.
|
|
4
|
+
|
|
5
|
+
## Guides
|
|
6
|
+
|
|
7
|
+
| Page | Read it when |
|
|
8
|
+
|---|---|
|
|
9
|
+
| [Documents](./guide/documents.md) | You write the `graphql()` documents a definition is built from, or a document is refused. |
|
|
10
|
+
| [Transport](./guide/transport.md) | You wire a client to the API, or write a transport of your own. |
|
|
11
|
+
| [Queries](./guide/queries.md) | You define a query and need its options. |
|
|
12
|
+
| [Mutations](./guide/mutations.md) | You define a write, with optimistic updates or invalidation. |
|
|
13
|
+
| [Infinite lists](./guide/infinite.md) | You paginate a Relay connection. |
|
|
14
|
+
| [Normalized cache](./guide/cache.md) | Several queries show the same entity and must stay in step. |
|
|
15
|
+
| [Errors](./guide/errors.md) | You branch on a failure, or map errors in a transport. |
|
|
16
|
+
| [Allowlist codegen](./guide/codegen.md) | A BFF must execute only known documents. |
|
|
17
|
+
| [BFF mode](./guide/bff.md) | The browser must never send a document. |
|
|
18
|
+
|
|
19
|
+
## Also
|
|
20
|
+
|
|
21
|
+
- [Troubleshooting](./troubleshooting.md): every error message and `RivaError` code this package throws, with the fix.
|
|
22
|
+
- [The core guides](../../riva/docs/README.md).
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# BFF mode
|
|
2
|
+
|
|
3
|
+
Entry points: `@softistx/riva-graphql/bff` (browser) and `@softistx/riva-graphql/server` (server).
|
|
4
|
+
|
|
5
|
+
## What it is for
|
|
6
|
+
|
|
7
|
+
In the browser, `bffTransport` posts `{ id, variables }` to your `dataRoute` (from `@softistx/riva-react-router/server`) and reads `{ data }` back, Dates included. The browser never sends a document: the route runs the manifest's text through the request client, with the session your `setup` gave it.
|
|
8
|
+
|
|
9
|
+
## Browser
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// app/riva.client.ts
|
|
13
|
+
import { bffTransport } from '@softistx/riva-graphql/bff';
|
|
14
|
+
import { provideGraphQL } from '@softistx/riva-graphql';
|
|
15
|
+
|
|
16
|
+
provideGraphQL(client, bffTransport({ url: '/_data' }));
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Server
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
// app/routes/_data.ts (a route with an action and a loader)
|
|
23
|
+
import { dataRoute } from '@softistx/riva-react-router/server';
|
|
24
|
+
import { allowlist } from '@softistx/riva-graphql/server';
|
|
25
|
+
import manifest from '../generated/allowlist.json';
|
|
26
|
+
|
|
27
|
+
const route = dataRoute({
|
|
28
|
+
sources: [allowlist(manifest)],
|
|
29
|
+
});
|
|
30
|
+
export const action = route.action;
|
|
31
|
+
export const loader = route.loader;
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Options
|
|
35
|
+
|
|
36
|
+
`bffTransport(options?)`:
|
|
37
|
+
|
|
38
|
+
| Option | Default | Meaning |
|
|
39
|
+
|---|---|---|
|
|
40
|
+
| `url` | `/_data` | The data route. |
|
|
41
|
+
| `fetch` | global `fetch` | A fetch of your own. |
|
|
42
|
+
| `headers` | none | A function returning `HeadersInit`, called at each request: `headers: () => ({ 'x-csrf': token() })`. |
|
|
43
|
+
|
|
44
|
+
`allowlist(manifest)` is a `DataSource`: it knows only the manifest's ids, parses each document once, and runs it through the transport `provideGraphQL` set on the request client.
|
|
45
|
+
|
|
46
|
+
## Errors
|
|
47
|
+
|
|
48
|
+
The route's refusals and the API's errors come back as `RivaRequestError` with the route's `code` and `status` (retryable for 5xx, 408 and 429). A failed fetch is a `RivaNetworkError`, an abort a `RivaAbortError`.
|
|
49
|
+
|
|
50
|
+
## Traps
|
|
51
|
+
|
|
52
|
+
- Keep `/bff` and `/server` out of each other's bundle: `/bff` is for the browser, `/server` imports `graphql`.
|
|
53
|
+
- An id that is not in the manifest is refused by the route: regenerate the allowlist after changing a document ([codegen](./codegen.md)).
|
|
54
|
+
- The request client needs `provideGraphQL` too, in the `rivaMiddleware` `setup`, or the allowlist has no transport to run through.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# Normalized cache
|
|
2
|
+
|
|
3
|
+
## What it is for
|
|
4
|
+
|
|
5
|
+
`normalized()` stores each object that has a `__typename` and an `id` once, as an entity, so two queries that show the same patient update each other, and a mutation's result updates every query showing it. It is built on the core entity store (`@softistx/riva/entities`).
|
|
6
|
+
|
|
7
|
+
## Use it
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { mutation, query } from '@softistx/riva-graphql';
|
|
11
|
+
import { normalized } from '@softistx/riva-graphql/cache';
|
|
12
|
+
import { PATIENT_QUERY, PATIENTS_QUERY, RENAME_PATIENT_MUTATION } from './graphql';
|
|
13
|
+
|
|
14
|
+
const cache = normalized({ possibleTypes: { Person: ['Patient', 'Doctor'] } });
|
|
15
|
+
|
|
16
|
+
export const patient = query(PATIENT_QUERY, { cache });
|
|
17
|
+
export const patients = query(PATIENTS_QUERY, { cache });
|
|
18
|
+
export const rename = mutation(RENAME_PATIENT_MUTATION, {
|
|
19
|
+
cache, // the mutation's result updates every entity it returns
|
|
20
|
+
optimistic: (v) => ({ renamePatient: { __typename: 'Patient', id: v.id, name: v.name } }),
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`query()` and `mutation()` take `cache?: CachePolicy | OperationCache`: a core policy as is, or an object with `forOperation(op)` returning the policy for the operation (`op.document`, `name`, `kind`, `id`). `normalized()` is one; a cache of your own implements `forOperation` the same way. `infinite()` and the core `query()` take core policies only.
|
|
25
|
+
|
|
26
|
+
## Options
|
|
27
|
+
|
|
28
|
+
| Option | Default | Meaning |
|
|
29
|
+
|---|---|---|
|
|
30
|
+
| `possibleTypes` | none | Maps each interface or union to its member type names (`{ Person: ['Patient', 'Doctor'] }`, as codegen's `fragment-matcher` writes it); may nest abstract types. |
|
|
31
|
+
| `keys` | `id`, then `_id` | Core identity option: `{ Type: (obj) => string }`, the identity of a type. |
|
|
32
|
+
| `identify` | the rule above | Core identity option: `(obj) => 'Type:id' \| undefined`, replaces the identity rule altogether. |
|
|
33
|
+
|
|
34
|
+
## How it reads
|
|
35
|
+
|
|
36
|
+
- **Objects without an id** are stored inside their parent, merged with what another query stored for the same field. When a shared scalar disagrees (a reordered list) the stored object is replaced, so another query selecting more fields of it refetches once.
|
|
37
|
+
- **Aliases** are stored under the field's name and read back under the response key.
|
|
38
|
+
- **Field arguments** get one stored field per argument set, variables resolved. `readEntity` and a plain `entities()` read skip those keys; read them through the query.
|
|
39
|
+
- **Fragments** (inline and named) and type conditions work. Without `possibleTypes`, a fragment on an abstract type is applied only to objects whose `__typename` equals its condition.
|
|
40
|
+
- **A miss is a miss**: if a selected field or entity is missing from the store, the query's data is `undefined`, never half-served. An observed query then refetches.
|
|
41
|
+
|
|
42
|
+
## Traps
|
|
43
|
+
|
|
44
|
+
- **Selections must include `__typename`** below the operation's root, selected directly (or in an inline fragment with no type condition). Otherwise `query()` or `mutation()` throws a `RivaError` (`RIVA_DEFINITION`); the document is never rewritten, since a persisted id is the hash of its text. The client preset's `addTypenameSelectionDocumentTransform` adds it.
|
|
45
|
+
- A fetch result that is itself missing a selected field ends the query in the error status with code `RIVA_CACHE` (`ensure` rejects with it) instead of looping.
|
|
46
|
+
- The cache holds JSON: a `Date`, a function or a `NaN` in a result throws a `RivaError` with code `RIVA_CACHE`.
|
|
47
|
+
- Share one `normalized()` between a client's definitions: a second one with other identity rules is refused by the core ([troubleshooting](../../../riva/docs/troubleshooting.md#this-client-already-uses-other-entity-identity-rules)).
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Allowlist codegen
|
|
2
|
+
|
|
3
|
+
Entry point: `@softistx/riva-graphql/codegen` (Node and codegen only; needs `graphql`).
|
|
4
|
+
|
|
5
|
+
## What it is for
|
|
6
|
+
|
|
7
|
+
A BFF's `/_data` route executes only documents it knows by id. The client preset (`@graphql-codegen/client-preset`) with `persistedDocuments: true` writes `persisted-documents.json`, `{ [hash]: text }`, and puts the same hash on each document as `__meta__.hash`, the id a definition sends. `codegen` turns that file into the manifest: one entry per query and mutation, `{ id, name, kind, document }`, keyed by the preset's hash **as it is** (never recomputed, so an id matches by construction) and sorted, so the same documents give the same bytes.
|
|
8
|
+
|
|
9
|
+
## Wire it
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// codegen.ts (needs @graphql-codegen/cli and @graphql-codegen/client-preset)
|
|
13
|
+
import type { CodegenConfig } from '@graphql-codegen/cli';
|
|
14
|
+
import { addTypenameSelectionDocumentTransform } from '@graphql-codegen/client-preset';
|
|
15
|
+
|
|
16
|
+
export default {
|
|
17
|
+
schema: 'schema.graphql',
|
|
18
|
+
documents: ['app/**/*.{ts,tsx}'],
|
|
19
|
+
generates: {
|
|
20
|
+
'app/generated/': {
|
|
21
|
+
preset: 'client',
|
|
22
|
+
presetConfig: { persistedDocuments: true },
|
|
23
|
+
// Needed by `normalized()`: it adds __typename to every selection set.
|
|
24
|
+
documentTransforms: [addTypenameSelectionDocumentTransform],
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
// Runs once every file is written, whatever the order of the outputs.
|
|
28
|
+
hooks: { afterAllFileWrite: ['bun scripts/allowlist.ts'] },
|
|
29
|
+
} satisfies CodegenConfig;
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
// scripts/allowlist.ts
|
|
34
|
+
import { writeAllowlist } from '@softistx/riva-graphql/codegen';
|
|
35
|
+
|
|
36
|
+
await writeAllowlist(
|
|
37
|
+
'app/generated/persisted-documents.json',
|
|
38
|
+
'app/generated/allowlist.json',
|
|
39
|
+
);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## `buildAllowlist(persisted, config?)`
|
|
43
|
+
|
|
44
|
+
The same step without the files: it takes the parsed `persisted-documents.json` and returns the manifest.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { buildAllowlist } from '@softistx/riva-graphql/codegen';
|
|
48
|
+
|
|
49
|
+
const manifest = buildAllowlist({
|
|
50
|
+
'sha256:ab12…': 'fragment F on Patient { id } query Patients { patients { ...F } }',
|
|
51
|
+
});
|
|
52
|
+
// { 'sha256:ab12…': { id: 'sha256:ab12…', name: 'Patients', kind: 'query', document: '…' } }
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Options (`AllowlistConfig`)
|
|
56
|
+
|
|
57
|
+
| Option | Default | Meaning |
|
|
58
|
+
|---|---|---|
|
|
59
|
+
| `ids` | `'persisted'` | `'persisted'` keys by the preset's hash. `'name'` keys by operation name, for documents with no persisted id (the runtime then uses the name as the id). |
|
|
60
|
+
|
|
61
|
+
`document` is the preset's text for the operation: the operation and the fragments it uses.
|
|
62
|
+
|
|
63
|
+
## Traps
|
|
64
|
+
|
|
65
|
+
- It fails on a text with no operation or several, an anonymous one, a subscription, and two operations of one name.
|
|
66
|
+
- Do not rely on the order of outputs inside one codegen run: build the manifest from the `afterAllFileWrite` hook.
|
|
67
|
+
- Import the manifest JSON as it is: `allowlist` takes its JSON shape (`kind` as a `string`) and refuses an unknown kind at startup.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Documents
|
|
2
|
+
|
|
3
|
+
Read this first: every definition is built from a typed document, so the document rules decide what the rest infers.
|
|
4
|
+
|
|
5
|
+
## What it is for
|
|
6
|
+
|
|
7
|
+
`query()`, `mutation()` and `infinite()` take a `TypedDocumentNode` written with the client preset's `graphql()` function. Variables and data are inferred from it; you never annotate them.
|
|
8
|
+
|
|
9
|
+
## Write the documents
|
|
10
|
+
|
|
11
|
+
Hold each document in a `SCREAMING_SNAKE_CASE` constant (`PATIENTS_QUERY`). Codegen derives the PascalCase types and, with `persistedDocuments`, a hash per operation.
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
// app/patients/graphql.ts
|
|
15
|
+
import { graphql } from '../generated/gql';
|
|
16
|
+
|
|
17
|
+
export const PATIENTS_QUERY = graphql(`
|
|
18
|
+
query Patients($school: ID!, $first: Int, $after: String) {
|
|
19
|
+
patients(school: $school, first: $first, after: $after) {
|
|
20
|
+
__typename
|
|
21
|
+
edges { __typename node { __typename id name } cursor }
|
|
22
|
+
pageInfo { __typename hasNextPage endCursor }
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
`);
|
|
26
|
+
|
|
27
|
+
export const RENAME_PATIENT_MUTATION = graphql(`
|
|
28
|
+
mutation RenamePatient($id: ID!, $name: String!) {
|
|
29
|
+
renamePatient(id: $id, name: $name) { __typename id name }
|
|
30
|
+
}
|
|
31
|
+
`);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## The rules
|
|
35
|
+
|
|
36
|
+
| Rule | Why |
|
|
37
|
+
|---|---|
|
|
38
|
+
| One named operation per document (`query` or `mutation`) | The definition reads the operation's name and kind. |
|
|
39
|
+
| `documentMode: 'documentNode'` (the preset's default) | A `TypedDocumentString` is refused: a definition reads the document's operation. |
|
|
40
|
+
| `__typename` selected below the root | Required by `normalized()`; the preset's `addTypenameSelectionDocumentTransform` adds it. |
|
|
41
|
+
| Unique operation names | The default definition `id` is the persisted id, else the name. |
|
|
42
|
+
|
|
43
|
+
## Traps
|
|
44
|
+
|
|
45
|
+
- A document that is not exactly one named query or mutation is refused when the definition is created, with a `RivaError` (`RIVA_DEFINITION`) that says why: no operation, several operations, no name, a subscription, or the wrong helper (`query()` on a mutation).
|
|
46
|
+
- A `TypedDocumentString` (`documentMode: 'string'`) is refused for the same reason.
|
|
47
|
+
- Never rewrite a document at runtime: a persisted id is the hash of its text.
|
|
48
|
+
|
|
49
|
+
See [queries](./queries.md), [mutations](./mutations.md) and [codegen](./codegen.md).
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Errors
|
|
2
|
+
|
|
3
|
+
## What it is for
|
|
4
|
+
|
|
5
|
+
The transport maps `@softistx/graphql-client`'s errors to riva's, so `instanceof` holds and the core's retry policy applies. Never retried: a refusal, 4xx. Retried: 5xx, 408, 429, the network. An `ApiError` that carries no status is read as 400, so it is not retried.
|
|
6
|
+
|
|
7
|
+
| graphql-client | riva | `code` | retried |
|
|
8
|
+
|---|---|---|---|
|
|
9
|
+
| `ApiError` (the response had `errors`) | `RivaRequestError` | the API's `extensions.code`, untouched (`GRAPHQL_ERROR` when it sent none) | 5xx, 408, 429 |
|
|
10
|
+
| `ApiStatusError` | `RivaHttpError` | `HTTP_<status>` | 5xx, 408, 429 |
|
|
11
|
+
| `ApiUnavailableError` (unreachable, timeout) | `RivaNetworkError` | `RIVA_NETWORK` | yes |
|
|
12
|
+
| `ApiUnavailableError` (no data, no errors) | `RivaRequestError` | `RIVA_INVALID_RESPONSE` | no |
|
|
13
|
+
| the call's abort | `RivaAbortError` | `RIVA_ABORT` | never |
|
|
14
|
+
|
|
15
|
+
## Read the cause
|
|
16
|
+
|
|
17
|
+
The original error is the `cause`, with its `fields`, `params` and `errors`.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { RivaRequestError } from '@softistx/riva';
|
|
21
|
+
import { isApiError } from '@softistx/graphql-client';
|
|
22
|
+
|
|
23
|
+
const state = client.get(patients, { school: 'north' });
|
|
24
|
+
if (state.error instanceof RivaRequestError) {
|
|
25
|
+
state.error.code; // 'patient.errors.not-found', as the API sent it
|
|
26
|
+
if (isApiError(state.error.cause)) state.error.cause.fields; // the refused fields
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Codes this package throws
|
|
31
|
+
|
|
32
|
+
| Code | When |
|
|
33
|
+
|---|---|
|
|
34
|
+
| `RIVA_CACHE` | `normalized()` could not store or read a result (a non-JSON value, a selected field missing from a fetch). |
|
|
35
|
+
| `RIVA_INVALID_RESPONSE` | The transport got neither data nor errors. |
|
|
36
|
+
|
|
37
|
+
Fixes: [troubleshooting](../troubleshooting.md).
|
|
38
|
+
|
|
39
|
+
## Traps
|
|
40
|
+
|
|
41
|
+
- Match on `code`, not on the message: the API's code is passed untouched.
|
|
42
|
+
- `toRivaError(error, signal?)` is exported for a transport of your own; pass the signal so an abort becomes a `RivaAbortError`.
|