houdini 1.1.6 → 1.2.0-next.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 (85) hide show
  1. package/README.md +1 -1
  2. package/build/cmd-cjs/index.js +30836 -30260
  3. package/build/cmd-esm/index.js +30836 -30260
  4. package/build/codegen/generators/artifacts/selection.d.ts +2 -1
  5. package/build/codegen/generators/typescript/inlineType.d.ts +5 -2
  6. package/build/codegen/generators/typescript/loadingState.d.ts +8 -0
  7. package/build/codegen-cjs/index.js +30791 -30254
  8. package/build/codegen-esm/index.js +30791 -30254
  9. package/build/lib/code.d.ts +11 -0
  10. package/build/lib/config.d.ts +5 -0
  11. package/build/lib/graphql.d.ts +5 -1
  12. package/build/lib/index.d.ts +1 -0
  13. package/build/lib/parse.d.ts +0 -7
  14. package/build/lib/types.d.ts +4 -1
  15. package/build/lib-cjs/index.js +29983 -29749
  16. package/build/lib-esm/index.js +29980 -29749
  17. package/build/runtime/cache/cache.d.ts +5 -6
  18. package/build/runtime/client/documentStore.d.ts +3 -0
  19. package/build/runtime/client/index.d.ts +7 -6
  20. package/build/runtime/client/plugins/cache.d.ts +1 -1
  21. package/build/runtime/client/plugins/fetch.d.ts +1 -0
  22. package/build/runtime/client/plugins/subscription.d.ts +1 -0
  23. package/build/runtime/client/plugins/throwOnError.d.ts +2 -1
  24. package/build/runtime/lib/pageInfo.d.ts +7 -0
  25. package/build/runtime/lib/pagination.d.ts +27 -0
  26. package/build/runtime/lib/selection.d.ts +1 -1
  27. package/build/runtime/lib/types.d.ts +75 -3
  28. package/build/runtime-cjs/cache/cache.d.ts +5 -6
  29. package/build/runtime-cjs/cache/cache.js +78 -36
  30. package/build/runtime-cjs/cache/subscription.js +5 -5
  31. package/build/runtime-cjs/client/documentStore.d.ts +3 -0
  32. package/build/runtime-cjs/client/documentStore.js +20 -7
  33. package/build/runtime-cjs/client/index.d.ts +7 -6
  34. package/build/runtime-cjs/client/index.js +12 -4
  35. package/build/runtime-cjs/client/plugins/cache.d.ts +1 -1
  36. package/build/runtime-cjs/client/plugins/cache.js +12 -2
  37. package/build/runtime-cjs/client/plugins/fetch.d.ts +1 -0
  38. package/build/runtime-cjs/client/plugins/fetch.js +3 -2
  39. package/build/runtime-cjs/client/plugins/fragment.js +8 -1
  40. package/build/runtime-cjs/client/plugins/query.js +2 -1
  41. package/build/runtime-cjs/client/plugins/subscription.d.ts +1 -0
  42. package/build/runtime-cjs/client/plugins/subscription.js +1 -0
  43. package/build/runtime-cjs/client/plugins/throwOnError.d.ts +2 -1
  44. package/build/runtime-cjs/lib/config.js +2 -1
  45. package/build/runtime-cjs/lib/pageInfo.d.ts +7 -0
  46. package/build/runtime-cjs/lib/pageInfo.js +79 -0
  47. package/build/runtime-cjs/lib/pagination.d.ts +27 -0
  48. package/build/runtime-cjs/lib/pagination.js +219 -0
  49. package/build/runtime-cjs/lib/scalars.js +1 -1
  50. package/build/runtime-cjs/lib/selection.d.ts +1 -1
  51. package/build/runtime-cjs/lib/selection.js +28 -1
  52. package/build/runtime-cjs/lib/types.d.ts +75 -3
  53. package/build/runtime-cjs/lib/types.js +3 -0
  54. package/build/runtime-esm/cache/cache.d.ts +5 -6
  55. package/build/runtime-esm/cache/cache.js +79 -37
  56. package/build/runtime-esm/cache/subscription.js +5 -5
  57. package/build/runtime-esm/client/documentStore.d.ts +3 -0
  58. package/build/runtime-esm/client/documentStore.js +20 -7
  59. package/build/runtime-esm/client/index.d.ts +7 -6
  60. package/build/runtime-esm/client/index.js +15 -7
  61. package/build/runtime-esm/client/plugins/cache.d.ts +1 -1
  62. package/build/runtime-esm/client/plugins/cache.js +12 -2
  63. package/build/runtime-esm/client/plugins/fetch.d.ts +1 -0
  64. package/build/runtime-esm/client/plugins/fetch.js +3 -2
  65. package/build/runtime-esm/client/plugins/fragment.js +8 -1
  66. package/build/runtime-esm/client/plugins/query.js +2 -1
  67. package/build/runtime-esm/client/plugins/subscription.d.ts +1 -0
  68. package/build/runtime-esm/client/plugins/subscription.js +1 -0
  69. package/build/runtime-esm/client/plugins/throwOnError.d.ts +2 -1
  70. package/build/runtime-esm/lib/config.js +2 -1
  71. package/build/runtime-esm/lib/pageInfo.d.ts +7 -0
  72. package/build/runtime-esm/lib/pageInfo.js +52 -0
  73. package/build/runtime-esm/lib/pagination.d.ts +27 -0
  74. package/build/runtime-esm/lib/pagination.js +194 -0
  75. package/build/runtime-esm/lib/scalars.js +1 -1
  76. package/build/runtime-esm/lib/selection.d.ts +1 -1
  77. package/build/runtime-esm/lib/selection.js +28 -1
  78. package/build/runtime-esm/lib/types.d.ts +75 -3
  79. package/build/runtime-esm/lib/types.js +2 -0
  80. package/build/test/index.d.ts +15 -0
  81. package/build/test-cjs/index.js +30708 -30140
  82. package/build/test-esm/index.js +30708 -30140
  83. package/build/vite-cjs/index.js +30864 -30290
  84. package/build/vite-esm/index.js +30864 -30290
  85. package/package.json +5 -1
@@ -38,6 +38,7 @@ class DocumentStore extends import_store.Writable {
38
38
  #plugins;
39
39
  #lastVariables;
40
40
  #lastContext = null;
41
+ pendingPromise = null;
41
42
  constructor({
42
43
  artifact,
43
44
  plugins,
@@ -70,7 +71,15 @@ class DocumentStore extends import_store.Writable {
70
71
  this.#plugins = pipeline ?? [
71
72
  (0, import_plugins.cachePolicy)({
72
73
  enabled: cache,
73
- setFetching: (fetching2) => this.update((state) => ({ ...state, fetching: fetching2 }))
74
+ setFetching: (fetching2, data) => {
75
+ this.update((state) => {
76
+ const newState = { ...state, fetching: fetching2 };
77
+ if (fetching2 && data) {
78
+ newState.data = data;
79
+ }
80
+ return newState;
81
+ });
82
+ }
74
83
  })(),
75
84
  ...plugins ?? []
76
85
  ];
@@ -110,7 +119,7 @@ class DocumentStore extends import_store.Writable {
110
119
  const draft = context.draft();
111
120
  draft.variables = variables ?? null;
112
121
  context = context.apply(draft, false);
113
- return await new Promise((resolve, reject) => {
122
+ const promise = new Promise((resolve, reject) => {
114
123
  const state = {
115
124
  setup,
116
125
  currentStep: 0,
@@ -119,12 +128,17 @@ class DocumentStore extends import_store.Writable {
119
128
  promise: {
120
129
  resolved: false,
121
130
  resolve,
122
- reject
131
+ reject,
132
+ then: (...args) => promise.then(...args)
123
133
  },
124
134
  context
125
135
  };
136
+ if (this.pendingPromise === null) {
137
+ this.pendingPromise = state.promise;
138
+ }
126
139
  this.#step("forward", state);
127
140
  });
141
+ return await promise;
128
142
  }
129
143
  async cleanup() {
130
144
  for (const plugin of this.#plugins) {
@@ -243,16 +257,15 @@ class DocumentStore extends import_store.Writable {
243
257
  value
244
258
  );
245
259
  }
260
+ if (!ctx.silenceEcho || value.data !== this.state.data) {
261
+ this.set(value);
262
+ }
246
263
  if (!ctx.promise.resolved) {
247
264
  ctx.promise.resolve(value);
248
265
  ctx.promise.resolved = true;
249
266
  }
250
267
  this.#lastContext = ctx.context.draft();
251
268
  this.#lastVariables = this.#lastContext.stuff.inputs.marshaled;
252
- if (ctx.silenceEcho && value.data === this.state.data) {
253
- return;
254
- }
255
- this.set(value);
256
269
  }
257
270
  }
258
271
  class ClientPluginContextWrapper {
@@ -1,11 +1,11 @@
1
1
  /// <reference path="../../../../../houdini.d.ts" />
2
2
  import type { DocumentArtifact, GraphQLObject, NestedList } from '../lib/types';
3
- import type { ClientPlugin, ClientHooks } from './documentStore';
3
+ import type { ClientHooks, ClientPlugin } from './documentStore';
4
4
  import { DocumentStore } from './documentStore';
5
- import { type FetchParamFn, type ThrowOnErrorParams } from './plugins';
6
- export { DocumentStore, type ClientPlugin } from './documentStore';
5
+ import type { FetchParamFn, ThrowOnErrorOperations, ThrowOnErrorParams } from './plugins';
6
+ export { DocumentStore, type ClientPlugin, type SendParams } from './documentStore';
7
7
  export { fetch, mutation, query, subscription } from './plugins';
8
- type ConstructorArgs = {
8
+ export type HoudiniClientConstructorArgs = {
9
9
  url: string;
10
10
  fetchParams?: FetchParamFn;
11
11
  plugins?: NestedList<ClientPlugin>;
@@ -19,9 +19,10 @@ export type ObserveParams<_Data extends GraphQLObject, _Artifact extends Documen
19
19
  fetching?: boolean;
20
20
  };
21
21
  export declare class HoudiniClient {
22
- #private;
23
22
  url: string;
24
- constructor({ url, fetchParams, plugins, pipeline, throwOnError }: ConstructorArgs);
23
+ readonly plugins: ClientPlugin[];
24
+ readonly throwOnError_operations: ThrowOnErrorOperations[];
25
+ constructor({ url, fetchParams, plugins, pipeline, throwOnError, }: HoudiniClientConstructorArgs);
25
26
  observe<_Data extends GraphQLObject, _Input extends Record<string, any>>({ artifact, cache, initialValue, fetching, }: ObserveParams<_Data>): DocumentStore<_Data, _Input>;
26
27
  }
27
28
  export declare function createPluginHooks(plugins: ClientPlugin[]): ClientHooks[];
@@ -41,14 +41,22 @@ var import_documentStore2 = require("./documentStore");
41
41
  var import_plugins2 = require("./plugins");
42
42
  class HoudiniClient {
43
43
  url;
44
- #plugins;
45
- constructor({ url, fetchParams, plugins, pipeline, throwOnError }) {
44
+ plugins;
45
+ throwOnError_operations;
46
+ constructor({
47
+ url,
48
+ fetchParams,
49
+ plugins,
50
+ pipeline,
51
+ throwOnError
52
+ }) {
46
53
  if (plugins && pipeline) {
47
54
  throw new Error(
48
55
  "A client cannot be given a pipeline and a list of plugins at the same time."
49
56
  );
50
57
  }
51
- this.#plugins = (0, import_flatten.flatten)(
58
+ this.throwOnError_operations = throwOnError?.operations ?? [];
59
+ this.plugins = (0, import_flatten.flatten)(
52
60
  [].concat(
53
61
  throwOnError ? [(0, import_plugins.throwOnError)(throwOnError)] : [],
54
62
  (0, import_plugins.fetchParams)(fetchParams),
@@ -74,7 +82,7 @@ class HoudiniClient {
74
82
  return new import_documentStore.DocumentStore({
75
83
  client: this,
76
84
  artifact,
77
- plugins: createPluginHooks(this.#plugins),
85
+ plugins: createPluginHooks(this.plugins),
78
86
  cache,
79
87
  initialValue,
80
88
  fetching
@@ -3,7 +3,7 @@ import { Cache } from '../../cache/cache';
3
3
  import type { ClientPlugin } from '../documentStore';
4
4
  export declare const cachePolicy: ({ enabled, setFetching, cache: localCache, serverSideFallback, }: {
5
5
  enabled: boolean;
6
- setFetching: (val: boolean) => void;
6
+ setFetching: (val: boolean, data?: any) => void;
7
7
  cache?: Cache | undefined;
8
8
  serverSideFallback?: boolean | undefined;
9
9
  }) => ClientPlugin;
@@ -41,7 +41,7 @@ const cachePolicy = ({
41
41
  network(ctx, { initialValue, next, resolve, marshalVariables }) {
42
42
  const { policy, artifact } = ctx;
43
43
  let useCache = false;
44
- if (enabled && artifact.kind === import_types.ArtifactKind.Query && !ctx.cacheParams?.disableRead) {
44
+ if (enabled && (artifact.kind === import_types.ArtifactKind.Query || artifact.kind === import_types.ArtifactKind.Fragment) && !ctx.cacheParams?.disableRead) {
45
45
  if (policy !== import_types.CachePolicy.NetworkOnly) {
46
46
  const value = localCache.read({
47
47
  selection: artifact.selection,
@@ -82,7 +82,17 @@ const cachePolicy = ({
82
82
  localCache._internal_unstable.collectGarbage();
83
83
  }, 0);
84
84
  }
85
- setFetching(!useCache);
85
+ if (!ctx.stuff?.silenceLoading) {
86
+ let fetchingState = null;
87
+ if (!useCache && "enableLoadingState" in artifact && artifact.enableLoadingState) {
88
+ fetchingState = localCache.read({
89
+ selection: artifact.selection,
90
+ variables: marshalVariables(ctx),
91
+ loading: true
92
+ }).data;
93
+ }
94
+ setFetching(!useCache, fetchingState);
95
+ }
86
96
  return next(ctx);
87
97
  },
88
98
  afterNetwork(ctx, { resolve, value, marshalVariables }) {
@@ -21,6 +21,7 @@ export type FetchContext = {
21
21
  export type RequestHandlerArgs = FetchContext & FetchParams;
22
22
  export type RequestHandler<_Data = any> = (args: RequestHandlerArgs) => Promise<RequestPayload<_Data>>;
23
23
  export type FetchParams = {
24
+ name: string;
24
25
  text: string;
25
26
  hash: string;
26
27
  variables: {
@@ -33,6 +33,7 @@ const fetch = (target) => {
33
33
  }
34
34
  const fetch2 = ctx.fetch ?? globalThis.fetch;
35
35
  const fetchParams = {
36
+ name: ctx.artifact.name,
36
37
  text: ctx.text,
37
38
  hash: ctx.hash,
38
39
  variables: marshalVariables(ctx)
@@ -73,10 +74,10 @@ const defaultFetch = (url, params) => {
73
74
  "Could not find configured client url. Please specify one in your HoudiniClient constructor."
74
75
  );
75
76
  }
76
- return async ({ fetch: fetch2, text, variables }) => {
77
+ return async ({ fetch: fetch2, name, text, variables }) => {
77
78
  const result = await fetch2(url, {
78
79
  method: "POST",
79
- body: JSON.stringify({ query: text, variables }),
80
+ body: JSON.stringify({ operationName: name, query: text, variables }),
80
81
  ...params,
81
82
  headers: {
82
83
  Accept: "application/graphql+json, application/json",
@@ -28,16 +28,22 @@ __export(fragment_exports, {
28
28
  });
29
29
  module.exports = __toCommonJS(fragment_exports);
30
30
  var import_cache = __toESM(require("../../cache"), 1);
31
+ var import_deepEquals = require("../../lib/deepEquals");
31
32
  var import_types = require("../../lib/types");
32
33
  var import_utils = require("../utils");
33
34
  const fragment = (0, import_utils.documentPlugin)(import_types.ArtifactKind.Fragment, function() {
34
35
  let subscriptionSpec = null;
36
+ let lastReference = null;
35
37
  return {
36
38
  start(ctx, { next, resolve, variablesChanged, marshalVariables }) {
37
39
  if (!ctx.stuff.parentID) {
38
40
  return next(ctx);
39
41
  }
40
- if (variablesChanged(ctx) && !ctx.cacheParams?.disableSubscriptions) {
42
+ const currentReference = {
43
+ parent: ctx.stuff.parentID,
44
+ variables: marshalVariables(ctx)
45
+ };
46
+ if (!ctx.cacheParams?.disableSubscriptions && (!(0, import_deepEquals.deepEquals)(lastReference, currentReference) || variablesChanged(ctx))) {
41
47
  if (subscriptionSpec) {
42
48
  import_cache.default.unsubscribe(subscriptionSpec, subscriptionSpec.variables?.() || {});
43
49
  }
@@ -60,6 +66,7 @@ const fragment = (0, import_utils.documentPlugin)(import_types.ArtifactKind.Frag
60
66
  }
61
67
  };
62
68
  import_cache.default.subscribe(subscriptionSpec, variables);
69
+ lastReference = currentReference;
63
70
  }
64
71
  next(ctx);
65
72
  },
@@ -47,10 +47,11 @@ const query = (0, import_utils.documentPlugin)(import_types.ArtifactKind.Query,
47
47
  import_cache.default.unsubscribe(subscriptionSpec, subscriptionSpec.variables?.() || {});
48
48
  }
49
49
  lastVariables = { ...marshalVariables(ctx) };
50
+ const variables = lastVariables;
50
51
  subscriptionSpec = {
51
52
  rootType: ctx.artifact.rootType,
52
53
  selection: ctx.artifact.selection,
53
- variables: () => lastVariables,
54
+ variables: () => variables,
54
55
  set: (newValue) => {
55
56
  resolve(ctx, {
56
57
  data: newValue,
@@ -3,6 +3,7 @@ export declare function subscription(factory: SubscriptionHandler): import("../d
3
3
  export type SubscriptionHandler = (ctx: ClientPluginContext) => SubscriptionClient;
4
4
  export type SubscriptionClient = {
5
5
  subscribe: (payload: {
6
+ operationName: string;
6
7
  query: string;
7
8
  variables?: {};
8
9
  }, handlers: {
@@ -55,6 +55,7 @@ function subscription(factory) {
55
55
  clearSubscription?.();
56
56
  clearSubscription = client.subscribe(
57
57
  {
58
+ operationName: ctx.artifact.name,
58
59
  query: ctx.artifact.raw,
59
60
  variables: marshalVariables(ctx)
60
61
  },
@@ -1,7 +1,8 @@
1
1
  import type { QueryResult } from '../../lib';
2
2
  import type { ClientPlugin, ClientPluginContext } from '../documentStore';
3
+ export type ThrowOnErrorOperations = 'all' | 'query' | 'mutation' | 'subscription';
3
4
  export type ThrowOnErrorParams = {
4
- operations: ('all' | 'query' | 'mutation' | 'subscription')[];
5
+ operations: ThrowOnErrorOperations[];
5
6
  error?: (errors: NonNullable<QueryResult<any, any>['errors']>, ctx: ClientPluginContext) => unknown;
6
7
  };
7
8
  export declare const throwOnError: ({ operations, error }: ThrowOnErrorParams) => ClientPlugin;
@@ -58,7 +58,8 @@ function defaultConfigValues(file) {
58
58
  };
59
59
  }
60
60
  function keyFieldsForType(configFile, type) {
61
- return configFile.types?.[type]?.keys || configFile.defaultKeys;
61
+ const withDefault = defaultConfigValues(configFile);
62
+ return withDefault.types?.[type]?.keys || withDefault.defaultKeys;
62
63
  }
63
64
  function computeID(configFile, type, data) {
64
65
  const fields = keyFieldsForType(configFile, type);
@@ -0,0 +1,7 @@
1
+ import type { GraphQLObject, PageInfo } from './types';
2
+ export declare function nullPageInfo(): PageInfo;
3
+ export declare function missingPageSizeError(fnName: string): {
4
+ message: string;
5
+ };
6
+ export declare function extractPageInfo(data: any, path: string[]): PageInfo;
7
+ export declare function countPage<_Data extends GraphQLObject>(source: string[], value: _Data | null): number;
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var pageInfo_exports = {};
20
+ __export(pageInfo_exports, {
21
+ countPage: () => countPage,
22
+ extractPageInfo: () => extractPageInfo,
23
+ missingPageSizeError: () => missingPageSizeError,
24
+ nullPageInfo: () => nullPageInfo
25
+ });
26
+ module.exports = __toCommonJS(pageInfo_exports);
27
+ var import_constants = require("./constants");
28
+ function nullPageInfo() {
29
+ return { startCursor: null, endCursor: null, hasNextPage: false, hasPreviousPage: false };
30
+ }
31
+ function missingPageSizeError(fnName) {
32
+ return {
33
+ message: `${fnName} is missing the required page arguments. For more information, please visit this link: ${import_constants.siteURL}/guides/pagination`
34
+ };
35
+ }
36
+ function extractPageInfo(data, path) {
37
+ if (!data) {
38
+ return {
39
+ startCursor: null,
40
+ endCursor: null,
41
+ hasNextPage: false,
42
+ hasPreviousPage: false
43
+ };
44
+ }
45
+ let localPath = [...path];
46
+ let current = data;
47
+ while (localPath.length > 0) {
48
+ if (!current) {
49
+ break;
50
+ }
51
+ current = current[localPath.shift()];
52
+ }
53
+ return current?.pageInfo ?? nullPageInfo();
54
+ }
55
+ function countPage(source, value) {
56
+ let data = value;
57
+ if (value === null || data === null || data === void 0) {
58
+ return 0;
59
+ }
60
+ for (const field of source) {
61
+ const obj = data[field];
62
+ if (obj && !Array.isArray(obj)) {
63
+ data = obj;
64
+ } else if (!data) {
65
+ throw new Error("Could not count page size");
66
+ }
67
+ if (Array.isArray(obj)) {
68
+ return obj.length;
69
+ }
70
+ }
71
+ return 0;
72
+ }
73
+ // Annotate the CommonJS export names for ESM import in node:
74
+ 0 && (module.exports = {
75
+ countPage,
76
+ extractPageInfo,
77
+ missingPageSizeError,
78
+ nullPageInfo
79
+ });
@@ -0,0 +1,27 @@
1
+ import type { SendParams } from '../client/documentStore';
2
+ import type { CursorHandlers, FetchFn, GraphQLObject, QueryArtifact, QueryResult, FetchParams } from './types';
3
+ export declare function cursorHandlers<_Data extends GraphQLObject, _Input extends Record<string, any>>({ artifact, fetchUpdate: parentFetchUpdate, fetch: parentFetch, getState, getVariables, getSession, }: {
4
+ artifact: QueryArtifact;
5
+ getState: () => _Data | null;
6
+ getVariables: () => _Input;
7
+ getSession: () => Promise<App.Session>;
8
+ fetch: FetchFn<_Data, _Input>;
9
+ fetchUpdate: (arg: SendParams, updates: string[]) => ReturnType<FetchFn<_Data, _Input>>;
10
+ }): CursorHandlers<_Data, _Input>;
11
+ export declare function offsetHandlers<_Data extends GraphQLObject, _Input extends {}>({ artifact, storeName, getState, getVariables, fetch: parentFetch, fetchUpdate: parentFetchUpdate, getSession, }: {
12
+ artifact: QueryArtifact;
13
+ fetch: FetchFn<_Data, _Input>;
14
+ fetchUpdate: (arg: SendParams) => ReturnType<FetchFn<_Data, _Input>>;
15
+ storeName: string;
16
+ getState: () => _Data | null;
17
+ getVariables: () => _Input;
18
+ getSession: () => Promise<App.Session>;
19
+ }): {
20
+ loadNextPage: ({ limit, offset, fetch, metadata, }?: {
21
+ limit?: number | undefined;
22
+ offset?: number | undefined;
23
+ fetch?: typeof fetch | undefined;
24
+ metadata?: {} | undefined;
25
+ }) => Promise<void>;
26
+ fetch(params?: FetchParams<_Input>): Promise<QueryResult<_Data, _Input>>;
27
+ };
@@ -0,0 +1,219 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var pagination_exports = {};
20
+ __export(pagination_exports, {
21
+ cursorHandlers: () => cursorHandlers,
22
+ offsetHandlers: () => offsetHandlers
23
+ });
24
+ module.exports = __toCommonJS(pagination_exports);
25
+ var import_config = require("./config");
26
+ var import_deepEquals = require("./deepEquals");
27
+ var import_pageInfo = require("./pageInfo");
28
+ var import_types = require("./types");
29
+ function cursorHandlers({
30
+ artifact,
31
+ fetchUpdate: parentFetchUpdate,
32
+ fetch: parentFetch,
33
+ getState,
34
+ getVariables,
35
+ getSession
36
+ }) {
37
+ const loadPage = async ({
38
+ pageSizeVar,
39
+ input,
40
+ functionName,
41
+ metadata = {},
42
+ fetch,
43
+ where
44
+ }) => {
45
+ const config = (0, import_config.getCurrentConfig)();
46
+ const loadVariables = {
47
+ ...getVariables(),
48
+ ...input
49
+ };
50
+ if (!loadVariables[pageSizeVar] && !artifact.refetch.pageSize) {
51
+ throw (0, import_pageInfo.missingPageSizeError)(functionName);
52
+ }
53
+ let isSinglePage = artifact.refetch?.mode === "SinglePage";
54
+ await (isSinglePage ? parentFetch : parentFetchUpdate)(
55
+ {
56
+ variables: loadVariables,
57
+ fetch,
58
+ metadata,
59
+ policy: isSinglePage ? artifact.policy : import_types.CachePolicy.NetworkOnly,
60
+ session: await getSession()
61
+ },
62
+ isSinglePage ? [] : [where === "start" ? "prepend" : "append"]
63
+ );
64
+ };
65
+ const getPageInfo = () => {
66
+ return (0, import_pageInfo.extractPageInfo)(getState(), artifact.refetch?.path ?? []);
67
+ };
68
+ return {
69
+ loadNextPage: async ({
70
+ first,
71
+ after,
72
+ fetch,
73
+ metadata
74
+ } = {}) => {
75
+ const currentPageInfo = getPageInfo();
76
+ if (!currentPageInfo.hasNextPage) {
77
+ return;
78
+ }
79
+ const input = {
80
+ first: first ?? artifact.refetch.pageSize,
81
+ after: after ?? currentPageInfo.endCursor,
82
+ before: null,
83
+ last: null
84
+ };
85
+ return await loadPage({
86
+ pageSizeVar: "first",
87
+ functionName: "loadNextPage",
88
+ input,
89
+ fetch,
90
+ metadata,
91
+ where: "end"
92
+ });
93
+ },
94
+ loadPreviousPage: async ({
95
+ last,
96
+ before,
97
+ fetch,
98
+ metadata
99
+ } = {}) => {
100
+ const currentPageInfo = getPageInfo();
101
+ if (!currentPageInfo.hasPreviousPage) {
102
+ return;
103
+ }
104
+ const input = {
105
+ before: before ?? currentPageInfo.startCursor,
106
+ last: last ?? artifact.refetch.pageSize,
107
+ first: null,
108
+ after: null
109
+ };
110
+ return await loadPage({
111
+ pageSizeVar: "last",
112
+ functionName: "loadPreviousPage",
113
+ input,
114
+ fetch,
115
+ metadata,
116
+ where: "start"
117
+ });
118
+ },
119
+ async fetch(args) {
120
+ const { variables } = args ?? {};
121
+ if (variables && !(0, import_deepEquals.deepEquals)(getVariables(), variables)) {
122
+ return await parentFetch(args);
123
+ }
124
+ try {
125
+ var currentPageInfo = (0, import_pageInfo.extractPageInfo)(getState(), artifact.refetch.path);
126
+ } catch {
127
+ return await parentFetch(args);
128
+ }
129
+ const queryVariables = {};
130
+ const count = (0, import_pageInfo.countPage)(artifact.refetch.path.concat("edges"), getState()) || artifact.refetch.pageSize;
131
+ if (count && count > artifact.refetch.pageSize) {
132
+ if (currentPageInfo.hasPreviousPage && currentPageInfo.hasNextPage && !(variables?.["first"] && variables?.["after"] || variables?.["last"] && variables?.["before"])) {
133
+ console.warn(`\u26A0\uFE0F Encountered a fetch() in the middle of the connection.
134
+ Make sure to pass a cursor value by hand that includes the current set (ie the entry before startCursor)
135
+ `);
136
+ }
137
+ if (!currentPageInfo.hasPreviousPage) {
138
+ queryVariables["first"] = count;
139
+ queryVariables["after"] = null;
140
+ queryVariables["last"] = null;
141
+ queryVariables["before"] = null;
142
+ } else if (!currentPageInfo.hasNextPage) {
143
+ queryVariables["last"] = count;
144
+ queryVariables["first"] = null;
145
+ queryVariables["after"] = null;
146
+ queryVariables["before"] = null;
147
+ }
148
+ }
149
+ Object.assign(queryVariables, variables ?? {});
150
+ const result = await parentFetch({
151
+ ...args,
152
+ variables: queryVariables
153
+ });
154
+ return result;
155
+ }
156
+ };
157
+ }
158
+ function offsetHandlers({
159
+ artifact,
160
+ storeName,
161
+ getState,
162
+ getVariables,
163
+ fetch: parentFetch,
164
+ fetchUpdate: parentFetchUpdate,
165
+ getSession
166
+ }) {
167
+ let getOffset = () => artifact.refetch?.start || (0, import_pageInfo.countPage)(artifact.refetch.path, getState()) || artifact.refetch.pageSize;
168
+ let currentOffset = getOffset() ?? 0;
169
+ return {
170
+ loadNextPage: async ({
171
+ limit,
172
+ offset,
173
+ fetch,
174
+ metadata
175
+ } = {}) => {
176
+ const queryVariables = {
177
+ ...getVariables(),
178
+ offset: offset ?? getOffset()
179
+ };
180
+ if (limit || limit === 0) {
181
+ queryVariables.limit = limit;
182
+ }
183
+ if (!queryVariables.limit && !artifact.refetch.pageSize) {
184
+ throw (0, import_pageInfo.missingPageSizeError)("loadNextPage");
185
+ }
186
+ let isSinglePage = artifact.refetch?.mode === "SinglePage";
187
+ const targetFetch = isSinglePage ? parentFetch : parentFetchUpdate;
188
+ await targetFetch({
189
+ variables: queryVariables,
190
+ fetch,
191
+ metadata,
192
+ policy: isSinglePage ? artifact.policy : import_types.CachePolicy.NetworkOnly,
193
+ session: await getSession()
194
+ });
195
+ const pageSize = queryVariables.limit || artifact.refetch.pageSize;
196
+ currentOffset = offset + pageSize;
197
+ },
198
+ async fetch(params = {}) {
199
+ const { variables } = params;
200
+ if (variables && !(0, import_deepEquals.deepEquals)(getVariables(), variables)) {
201
+ return parentFetch.call(this, params);
202
+ }
203
+ const count = currentOffset || getOffset();
204
+ const queryVariables = {};
205
+ if (!artifact.refetch.pageSize || count > artifact.refetch.pageSize) {
206
+ queryVariables.limit = count;
207
+ }
208
+ return await parentFetch.call(this, {
209
+ ...params,
210
+ variables: queryVariables
211
+ });
212
+ }
213
+ };
214
+ }
215
+ // Annotate the CommonJS export names for ESM import in node:
216
+ 0 && (module.exports = {
217
+ cursorHandlers,
218
+ offsetHandlers
219
+ });
@@ -37,7 +37,7 @@ async function marshalSelection({
37
37
  if (Array.isArray(data)) {
38
38
  return await Promise.all(data.map((val) => marshalSelection({ selection, data: val })));
39
39
  }
40
- const targetSelection = (0, import_selection.getFieldsForType)(selection, data["__typename"]);
40
+ const targetSelection = (0, import_selection.getFieldsForType)(selection, data["__typename"], false);
41
41
  return Object.fromEntries(
42
42
  await Promise.all(
43
43
  Object.entries(data).map(async ([fieldName, value]) => {
@@ -1,2 +1,2 @@
1
1
  import type { SubscriptionSelection } from './types';
2
- export declare function getFieldsForType(selection: SubscriptionSelection, __typename: string | undefined | null): Required<SubscriptionSelection>['fields'];
2
+ export declare function getFieldsForType(selection: SubscriptionSelection, __typename: string | undefined | null, loading: boolean): Required<SubscriptionSelection>['fields'];