Skip to content

Commit 6ee69a1

Browse files
author
Andrei Fidelman
committed
Review fixes (path apollo and invariant result.complete)
1 parent ef2edbf commit 6ee69a1

File tree

4 files changed

+191
-47
lines changed

4 files changed

+191
-47
lines changed

packages/apollo-react-relay-duct-tape/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"duct-tape-compiler": "yarn duct-tape-compiler:compiled-hooks && yarn duct-tape-compiler:noop-hooks"
2121
},
2222
"devDependencies": {
23-
"@apollo/client": "3.8.1",
23+
"@apollo/client": ">= ^3.3.0 < 3.7.0",
2424
"@graphitation/apollo-mock-client": "^0.11.10",
2525
"@graphitation/graphql-js-operation-payload-generator": "^0.12.10",
2626
"@graphitation/graphql-js-tag": "^0.9.4",
@@ -34,6 +34,7 @@
3434
"ts-expect": "^1.3.0"
3535
},
3636
"peerDependencies": {
37+
"@apollo/client": ">= ^3.3.0 < 3.7.0",
3738
"graphql": "^15.0.0",
3839
"react": "^18.2.0"
3940
},

packages/apollo-react-relay-duct-tape/src/storeObservation/compiledHooks/useCompiledFragment.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ export function useCompiledFragment(
3232
const from = fragmentReference.id;
3333
invariant(
3434
typeof from === "string",
35-
"useFragment(): Expected the fragment reference to have a string id. " +
36-
"Did you forget to invoke the compiler?",
35+
"useFragment(): Expected the fragment reference to have a string id.",
3736
);
3837
invariant(
3938
metadata,
@@ -58,8 +57,13 @@ export function useCompiledFragment(
5857
};
5958

6059
const result = useFragment(doc);
61-
const data = result.complete ? result.data : {};
62-
return data as object;
60+
61+
invariant(
62+
result.complete,
63+
"useFragment(): Missing data expected to be seeded by the execution query document. Please check your type policies and possibleTypes configuration. If only subset of properties is missing you might need to configure merge functions for non-normalized types.",
64+
);
65+
66+
return result.data as object;
6367
}
6468

6569
type DefaultValue =

patches/@apollo+client+3.6.10.patch

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,70 @@ index afbbcd0..d93d0af 100644
1919
fieldName: fieldName,
2020
storeFieldName: storeFieldName,
2121
variables: variables,
22+
diff --git a/node_modules/@apollo/client/cache/core/types/Cache.d.ts b/node_modules/@apollo/client/cache/core/types/Cache.d.ts
23+
index 13fdd1f..6ceac48 100644
24+
--- a/node_modules/@apollo/client/cache/core/types/Cache.d.ts
25+
+++ b/node_modules/@apollo/client/cache/core/types/Cache.d.ts
26+
@@ -14,9 +14,9 @@ export declare namespace Cache {
27+
dataId?: string;
28+
result: TResult;
29+
}
30+
- interface DiffOptions<TData = any, TVariables = any> extends ReadOptions<TVariables, TData> {
31+
+ interface DiffOptions<TData = any, TVariables = any> extends Omit<ReadOptions<TVariables, TData>, "rootId"> {
32+
}
33+
- interface WatchOptions<TData = any, TVariables = any> extends ReadOptions<TVariables, TData> {
34+
+ interface WatchOptions<TData = any, TVariables = any> extends DiffOptions<TData, TVariables> {
35+
watcher?: object;
36+
immediate?: boolean;
37+
callback: WatchCallback<TData>;
38+
diff --git a/node_modules/@apollo/client/cache/core/types/common.js b/node_modules/@apollo/client/cache/core/types/common.js
39+
index bf82b96..266f9fe 100644
40+
--- a/node_modules/@apollo/client/cache/core/types/common.js
41+
+++ b/node_modules/@apollo/client/cache/core/types/common.js
42+
@@ -7,6 +7,15 @@ var MissingFieldError = (function (_super) {
43+
_this.path = path;
44+
_this.query = query;
45+
_this.variables = variables;
46+
+ if (Array.isArray(_this.path)) {
47+
+ _this.missing = _this.message;
48+
+ for (var i = _this.path.length - 1; i >= 0; --i) {
49+
+ _this.missing = (_a = {}, _a[_this.path[i]] = _this.missing, _a);
50+
+ }
51+
+ }
52+
+ else {
53+
+ _this.missing = _this.path;
54+
+ }
55+
_this.__proto__ = MissingFieldError.prototype;
56+
return _this;
57+
}
58+
diff --git a/node_modules/@apollo/client/cache/index.d.ts b/node_modules/@apollo/client/cache/index.d.ts
59+
index 2f13047..4d9e466 100644
60+
--- a/node_modules/@apollo/client/cache/index.d.ts
61+
+++ b/node_modules/@apollo/client/cache/index.d.ts
62+
@@ -2,7 +2,7 @@ import '../utilities/globals';
63+
export { Transaction, ApolloCache } from './core/cache';
64+
export { Cache } from './core/types/Cache';
65+
export { DataProxy } from './core/types/DataProxy';
66+
-export { Modifier, Modifiers, MissingFieldError, ReadFieldOptions } from './core/types/common';
67+
+export { MissingTree, Modifier, Modifiers, MissingFieldError, ReadFieldOptions } from './core/types/common';
68+
export { Reference, isReference, makeReference, } from '../utilities';
69+
export { EntityStore } from './inmemory/entityStore';
70+
export { fieldNameFromStoreName, defaultDataIdFromObject, } from './inmemory/helpers';
71+
diff --git a/node_modules/@apollo/client/cache/inmemory/inMemoryCache.js b/node_modules/@apollo/client/cache/inmemory/inMemoryCache.js
72+
index 18fb41a..d8360e0 100644
73+
--- a/node_modules/@apollo/client/cache/inmemory/inMemoryCache.js
74+
+++ b/node_modules/@apollo/client/cache/inmemory/inMemoryCache.js
75+
@@ -60,8 +60,8 @@ var InMemoryCache = (function (_super) {
76+
makeCacheKey: function (c) {
77+
var store = c.optimistic ? _this.optimisticData : _this.data;
78+
if (supportsResultCaching(store)) {
79+
- var optimistic = c.optimistic, rootId = c.rootId, variables = c.variables;
80+
- return store.makeCacheKey(c.query, c.callback, canonicalStringify({ optimistic: optimistic, rootId: rootId, variables: variables }));
81+
+ var optimistic = c.optimistic, id = c.id, variables = c.variables;
82+
+ return store.makeCacheKey(c.query, c.callback, canonicalStringify({ optimistic: optimistic, id: id, variables: variables }));
83+
}
84+
}
85+
});
2286
diff --git a/node_modules/@apollo/client/cache/inmemory/policies.d.ts b/node_modules/@apollo/client/cache/inmemory/policies.d.ts
2387
index 6a93238..fdef0a4 100644
2488
--- a/node_modules/@apollo/client/cache/inmemory/policies.d.ts
@@ -58,3 +122,118 @@ index a2c1b93..d28dddf 100644
58122
fieldName: fieldName,
59123
storeFieldName: storeFieldName,
60124
variables: variables,
125+
diff --git a/node_modules/@apollo/client/react/hooks/index.d.ts b/node_modules/@apollo/client/react/hooks/index.d.ts
126+
index a590efb..458f53f 100644
127+
--- a/node_modules/@apollo/client/react/hooks/index.d.ts
128+
+++ b/node_modules/@apollo/client/react/hooks/index.d.ts
129+
@@ -5,4 +5,5 @@ export * from './useMutation';
130+
export { useQuery } from './useQuery';
131+
export * from './useSubscription';
132+
export * from './useReactiveVar';
133+
+export * from "./useFragment.js";
134+
//# sourceMappingURL=index.d.ts.map
135+
\ No newline at end of file
136+
diff --git a/node_modules/@apollo/client/react/hooks/index.js b/node_modules/@apollo/client/react/hooks/index.js
137+
index 5a93908..074732c 100644
138+
--- a/node_modules/@apollo/client/react/hooks/index.js
139+
+++ b/node_modules/@apollo/client/react/hooks/index.js
140+
@@ -5,4 +5,5 @@ export * from "./useMutation.js";
141+
export { useQuery } from "./useQuery.js";
142+
export * from "./useSubscription.js";
143+
export * from "./useReactiveVar.js";
144+
+export * from "./useFragment.js";
145+
//# sourceMappingURL=index.js.map
146+
\ No newline at end of file
147+
diff --git a/node_modules/@apollo/client/react/hooks/useFragment.d.ts b/node_modules/@apollo/client/react/hooks/useFragment.d.ts
148+
new file mode 100644
149+
index 0000000..0e91e5e
150+
--- /dev/null
151+
+++ b/node_modules/@apollo/client/react/hooks/useFragment.d.ts
152+
@@ -0,0 +1,19 @@
153+
+import type { DeepPartial } from "../../utilities/index.js";
154+
+import type { Cache, Reference, StoreObject, MissingTree } from "../../cache/index.js";
155+
+import type { OperationVariables } from "../../core/index.js";
156+
+import type { NoInfer } from "../types/types.js";
157+
+export interface UseFragmentOptions<TData, TVars> extends Omit<Cache.DiffOptions<NoInfer<TData>, NoInfer<TVars>>, "id" | "query" | "optimistic" | "previousResult" | "returnPartialData">, Omit<Cache.ReadFragmentOptions<TData, TVars>, "id" | "variables" | "returnPartialData"> {
158+
+ from: StoreObject | Reference | string;
159+
+ optimistic?: boolean;
160+
+}
161+
+export type UseFragmentResult<TData> = {
162+
+ data: TData;
163+
+ complete: true;
164+
+ missing?: never;
165+
+} | {
166+
+ data: DeepPartial<TData>;
167+
+ complete: false;
168+
+ missing?: MissingTree;
169+
+};
170+
+export declare function useFragment<TData = any, TVars = OperationVariables>(options: UseFragmentOptions<TData, TVars>): UseFragmentResult<TData>;
171+
+//# sourceMappingURL=useFragment.d.ts.map
172+
\ No newline at end of file
173+
diff --git a/node_modules/@apollo/client/react/hooks/useFragment.d.ts.map b/node_modules/@apollo/client/react/hooks/useFragment.d.ts.map
174+
new file mode 100644
175+
index 0000000..09c87a4
176+
--- /dev/null
177+
+++ b/node_modules/@apollo/client/react/hooks/useFragment.d.ts.map
178+
@@ -0,0 +1 @@
179+
+{"version":3,"file":"useFragment.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useFragment.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAE5D,OAAO,KAAK,EACV,KAAK,EACL,SAAS,EACT,WAAW,EACX,WAAW,EACZ,MAAM,sBAAsB,CAAC;AAI9B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,WAAW,kBAAkB,CAAC,KAAK,EAAE,KAAK,CAC9C,SAAQ,IAAI,CACR,KAAK,CAAC,WAAW,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,EACjD,IAAI,GAAG,OAAO,GAAG,YAAY,GAAG,gBAAgB,GAAG,mBAAmB,CACvE,EACD,IAAI,CACF,KAAK,CAAC,mBAAmB,CAAC,KAAK,EAAE,KAAK,CAAC,EACvC,IAAI,GAAG,WAAW,GAAG,mBAAmB,CACzC;IACH,IAAI,EAAE,WAAW,GAAG,SAAS,GAAG,MAAM,CAAC;IAEvC,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,MAAM,iBAAiB,CAAC,KAAK,IAC/B;IACE,IAAI,EAAE,KAAK,CAAC;IACZ,QAAQ,EAAE,IAAI,CAAC;IACf,OAAO,CAAC,EAAE,KAAK,CAAC;CACjB,GACD;IACE,IAAI,EAAE,WAAW,CAAC,KAAK,CAAC,CAAC;IACzB,QAAQ,EAAE,KAAK,CAAC;IAChB,OAAO,CAAC,EAAE,WAAW,CAAC;CACvB,CAAC;AAEN,wBAAgB,WAAW,CAAC,KAAK,GAAG,GAAG,EAAE,KAAK,GAAG,kBAAkB,EACjE,OAAO,EAAE,kBAAkB,CAAC,KAAK,EAAE,KAAK,CAAC,GACxC,iBAAiB,CAAC,KAAK,CAAC,CA8C1B"}
180+
\ No newline at end of file
181+
diff --git a/node_modules/@apollo/client/react/hooks/useFragment.js b/node_modules/@apollo/client/react/hooks/useFragment.js
182+
new file mode 100644
183+
index 0000000..0f480b5
184+
--- /dev/null
185+
+++ b/node_modules/@apollo/client/react/hooks/useFragment.js
186+
@@ -0,0 +1,44 @@
187+
+import { __assign, __rest } from "tslib";
188+
+import * as React from "react";
189+
+import { equal } from "@wry/equality";
190+
+import { mergeDeepArray } from "../../utilities/index.js";
191+
+import { useApolloClient } from "./useApolloClient.js";
192+
+import { useSyncExternalStore } from "./useSyncExternalStore.js";
193+
+export function useFragment(options) {
194+
+ var cache = useApolloClient().cache;
195+
+ var fragment = options.fragment, fragmentName = options.fragmentName, from = options.from, _a = options.optimistic, optimistic = _a === void 0 ? true : _a, rest = __rest(options, ["fragment", "fragmentName", "from", "optimistic"]);
196+
+ var diffOptions = __assign(__assign({}, rest), { returnPartialData: true, id: typeof from === "string" ? from : cache.identify(from), query: cache["getFragmentDoc"](fragment, fragmentName), optimistic: optimistic });
197+
+ var resultRef = React.useRef();
198+
+ var latestDiff = cache.diff(diffOptions);
199+
+ var getSnapshot = function () {
200+
+ var latestDiffToResult = diffToResult(latestDiff);
201+
+ return resultRef.current &&
202+
+ equal(resultRef.current.data, latestDiffToResult.data)
203+
+ ? resultRef.current
204+
+ : (resultRef.current = latestDiffToResult);
205+
+ };
206+
+ return useSyncExternalStore(function (forceUpdate) {
207+
+ var lastTimeout = 0;
208+
+ var unsubcribe = cache.watch(__assign(__assign({}, diffOptions), { immediate: true, callback: function (diff) {
209+
+ if (!equal(diff, latestDiff)) {
210+
+ resultRef.current = diffToResult((latestDiff = diff));
211+
+ lastTimeout = setTimeout(forceUpdate);
212+
+ }
213+
+ } }));
214+
+ return function () {
215+
+ unsubcribe();
216+
+ clearTimeout(lastTimeout);
217+
+ };
218+
+ }, getSnapshot, getSnapshot);
219+
+}
220+
+function diffToResult(diff) {
221+
+ var result = {
222+
+ data: diff.result,
223+
+ complete: !!diff.complete,
224+
+ };
225+
+ if (diff.missing) {
226+
+ result.missing = mergeDeepArray(diff.missing.map(function (error) { return error.missing; }));
227+
+ }
228+
+ return result;
229+
+}
230+
+//# sourceMappingURL=useFragment.js.map
231+
\ No newline at end of file
232+
diff --git a/node_modules/@apollo/client/react/hooks/useFragment.js.map b/node_modules/@apollo/client/react/hooks/useFragment.js.map
233+
new file mode 100644
234+
index 0000000..abd2a70
235+
--- /dev/null
236+
+++ b/node_modules/@apollo/client/react/hooks/useFragment.js.map
237+
@@ -0,0 +1 @@
238+
+{"version":3,"file":"useFragment.js","sourceRoot":"","sources":["../../../src/react/hooks/useFragment.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAGtC,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAQ1D,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AA8BjE,MAAM,UAAU,WAAW,CACzB,OAAyC;IAEjC,IAAA,KAAK,GAAK,eAAe,EAAE,MAAtB,CAAuB;IAE5B,IAAA,QAAQ,GAAqD,OAAO,SAA5D,EAAE,YAAY,GAAuC,OAAO,aAA9C,EAAE,IAAI,GA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9C;YACH,CAAC,IACD,CAAC;QACH,OAAO;YACL,UAAU,EAAE,CAAC;YACb,YAAY,CAAC,WAAW,CAAC,CAAC;QAC5B,CAAC,CAAC;IACJ,CAAC,EACD,WAAW,EACX,WAAW,CACZ,CAAC;AACJ,CAAC;AAED,SAAS,YAAY,CACnB,IAA6B;IAE7B,IAAM,MAAM,GAAG;QACb,IAAI,EAAE,IAAI,CAAC,MAAO;QAClB,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ;KACE,CAAC;IAE9B,IAAI,IAAI,CAAC,OAAO,EAAE;QAChB,MAAM,CAAC,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAC,KAAK,IAAK,OAAA,KAAK,CAAC,OAAO,EAAb,CAAa,CAAC,CAAC,CAAC;KAC7E;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import * as React from \"react\";\nimport { equal } from \"@wry/equality\";\n\nimport type { DeepPartial } from \"../../utilities/index.js\";\nimport { mergeDeepArray } from \"../../utilities/index.js\";\nimport type {\n Cache,\n Reference,\n StoreObject,\n MissingTree,\n} from \"../../cache/index.js\";\n\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport { useSyncExternalStore } from \"./useSyncExternalStore.js\";\nimport type { OperationVariables } from \"../../core/index.js\";\nimport type { NoInfer } from \"../types/types.js\";\n\nexport interface UseFragmentOptions<TData, TVars>\n extends Omit<\n Cache.DiffOptions<NoInfer<TData>, NoInfer<TVars>>,\n \"id\" | \"query\" | \"optimistic\" | \"previousResult\" | \"returnPartialData\"\n >,\n Omit<\n Cache.ReadFragmentOptions<TData, TVars>,\n \"id\" | \"variables\" | \"returnPartialData\"\n > {\n from: StoreObject | Reference | string;\n // Override this field to make it optional (default: true).\n optimistic?: boolean;\n}\n\nexport type UseFragmentResult<TData> =\n | {\n data: TData;\n complete: true;\n missing?: never;\n }\n | {\n data: DeepPartial<TData>;\n complete: false;\n missing?: MissingTree;\n };\n\nexport function useFragment<TData = any, TVars = OperationVariables>(\n options: UseFragmentOptions<TData, TVars>\n): UseFragmentResult<TData> {\n const { cache } = useApolloClient();\n\n const { fragment, fragmentName, from, optimistic = true, ...rest } = options;\n\n const diffOptions: Cache.DiffOptions<TData, TVars> = {\n ...rest,\n returnPartialData: true,\n id: typeof from === \"string\" ? from : cache.identify(from),\n query: cache[\"getFragmentDoc\"](fragment, fragmentName),\n optimistic,\n };\n\n const resultRef = React.useRef<UseFragmentResult<TData>>();\n let latestDiff = cache.diff<TData>(diffOptions);\n\n // Used for both getSnapshot and getServerSnapshot\n const getSnapshot = () => {\n const latestDiffToResult = diffToResult(latestDiff);\n return resultRef.current &&\n equal(resultRef.current.data, latestDiffToResult.data)\n ? resultRef.current\n : (resultRef.current = latestDiffToResult);\n };\n\n return useSyncExternalStore(\n (forceUpdate) => {\n let lastTimeout = 0;\n const unsubcribe = cache.watch({\n ...diffOptions,\n immediate: true,\n callback(diff) {\n if (!equal(diff, latestDiff)) {\n resultRef.current = diffToResult((latestDiff = diff));\n lastTimeout = setTimeout(forceUpdate) as any;\n }\n },\n });\n return () => {\n unsubcribe();\n clearTimeout(lastTimeout);\n };\n },\n getSnapshot,\n getSnapshot\n );\n}\n\nfunction diffToResult<TData>(\n diff: Cache.DiffResult<TData>\n): UseFragmentResult<TData> {\n const result = {\n data: diff.result!,\n complete: !!diff.complete,\n } as UseFragmentResult<TData>;\n\n if (diff.missing) {\n result.missing = mergeDeepArray(diff.missing.map((error) => error.missing));\n }\n\n return result;\n}\n"]}
239+
\ No newline at end of file

0 commit comments

Comments
 (0)