Skip to content

Commit

Permalink
Adds @defer support (#10018)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben Newman <ben@apollographql.com>
  • Loading branch information
alessbell and benjamn committed Sep 8, 2022
1 parent d2c858a commit f2bb099
Show file tree
Hide file tree
Showing 27 changed files with 2,557 additions and 134 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Expand Up @@ -14,6 +14,9 @@ pids
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Ignore Wallaby.js configuration file
wallaby.js

# Coverage directory used by tools like istanbul
coverage

Expand Down Expand Up @@ -61,4 +64,7 @@ junit.xml
.rpt2_cache

# Local Netlify folder
.netlify
.netlify

# Ignore generated test report output
reports
2 changes: 1 addition & 1 deletion .vscode/settings.json
Expand Up @@ -4,7 +4,7 @@
"editor.rulers": [80],
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"typescript.tsdk": "../node_modules/typescript/lib",
"typescript.tsdk": "node_modules/typescript/lib",
"cSpell.enableFiletypes": [
"mdx"
]
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,11 @@
- Delay calling `onCompleted` and `onError` callbacks passed to `useQuery` using `Promise.resolve().then(() => ...)` to fix issue [#9794](https://github.com/apollographql/apollo-client/pull/9794). <br/>
[@dylanwulf](https://github.com/dylanwulf) in [#9823](https://github.com/apollographql/apollo-client/pull/9823)

### Potentially disruptive

- The optional `subscribeAndCount` testing utility exported from `@apollo/client/testing/core` now takes a single generic `TResult` type parameter, instead of `TData`. This type will typically be inferred from the `observable` argument type, but if you have any explicit calls to `subscribeAndCount<TData>(...)` in your own codebase, you may need to adjust those calls accordingly. <br/>
[@benjamn](https://github.com/benjamn) in [#9718](https://github.com/apollographql/apollo-client/pull/9718)

## Apollo Client 3.6.9 (2022-06-21)

### Bug Fixes
Expand Down
66 changes: 65 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Expand Up @@ -56,7 +56,7 @@
{
"name": "apollo-client",
"path": "./dist/apollo-client.min.cjs",
"maxSize": "29.95kB"
"maxSize": "31.4kB"
}
],
"engines": {
Expand Down Expand Up @@ -88,6 +88,7 @@
"hoist-non-react-statics": "^3.3.2",
"optimism": "^0.16.1",
"prop-types": "^15.7.2",
"response-iterator": "^0.2.6",
"symbol-observable": "^4.0.0",
"ts-invariant": "^0.10.3",
"tslib": "^2.3.0",
Expand All @@ -107,10 +108,12 @@
"@types/jest": "27.5.2",
"@types/lodash": "4.14.182",
"@types/node": "16.11.41",
"@types/node-fetch": "2.6.2",
"@types/react": "17.0.47",
"@types/react-dom": "17.0.17",
"@types/use-sync-external-store": "^0.0.3",
"@types/use-sync-external-store": "0.0.3",
"acorn": "8.7.1",
"blob-polyfill": "7.0.20220408",
"bundlesize": "0.18.1",
"cross-fetch": "3.1.5",
"crypto-hash": "1.3.0",
Expand All @@ -136,6 +139,7 @@
"ts-node": "10.8.1",
"typescript": "4.6.4",
"wait-for-observables": "1.0.3",
"web-streams-polyfill": "3.2.1",
"whatwg-fetch": "3.6.2"
},
"publishConfig": {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/exports.ts.snap
Expand Up @@ -350,6 +350,7 @@ Array [
"argumentsObjectFromField",
"asyncMap",
"buildQueryFromSelectionSet",
"canUseAsyncIteratorSymbol",
"canUseDOM",
"canUseLayoutEffect",
"canUseSymbol",
Expand Down
2 changes: 1 addition & 1 deletion src/core/ObservableQuery.ts
Expand Up @@ -482,7 +482,7 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`);
},
});

return fetchMoreResult as ApolloQueryResult<TFetchData>;
return fetchMoreResult;

}).finally(() => {
// In case the cache writes above did not generate a broadcast
Expand Down
27 changes: 25 additions & 2 deletions src/core/QueryInfo.ts
Expand Up @@ -2,6 +2,7 @@ import { DocumentNode, GraphQLError } from 'graphql';
import { equal } from "@wry/equality";

import { Cache, ApolloCache } from '../cache';
import { DeepMerger } from "../utilities"
import { WatchQueryOptions, ErrorPolicy } from './watchQueryOptions';
import { ObservableQuery, reobserveCacheFirst } from './ObservableQuery';
import { QueryListener } from './types';
Expand Down Expand Up @@ -153,7 +154,6 @@ export class QueryInfo {

reset() {
cancelNotifyTimeout(this);
this.lastDiff = void 0;
this.dirty = false;
}

Expand Down Expand Up @@ -362,12 +362,35 @@ export class QueryInfo {
| "errorPolicy">,
cacheWriteBehavior: CacheWriteBehavior,
) {
this.graphQLErrors = isNonEmptyArray(result.errors) ? result.errors : [];
const graphQLErrors = isNonEmptyArray(result.errors)
? result.errors.slice(0)
: [];

// Cancel the pending notify timeout (if it exists) to prevent extraneous network
// requests. To allow future notify timeouts, diff and dirty are reset as well.
this.reset();

if ('incremental' in result && isNonEmptyArray(result.incremental)) {
let mergedData = this.getDiff().result;
const merger = new DeepMerger();
result.incremental.forEach(({ data, path, errors }) => {
for (let i = path.length - 1; i >= 0; --i) {
const key = path[i];
const isNumericKey = !isNaN(+key);
const parent: Record<string | number, any> = isNumericKey ? [] : {};
parent[key] = data;
data = parent as typeof data;
}
if (errors) {
graphQLErrors.push(...errors);
}
mergedData = merger.merge(mergedData, data);
});
result.data = mergedData;
}

this.graphQLErrors = graphQLErrors;

if (options.fetchPolicy === 'no-cache') {
this.updateLastDiff(
{ result: result.data, complete: true },
Expand Down
21 changes: 17 additions & 4 deletions src/core/QueryManager.ts
Expand Up @@ -6,6 +6,7 @@ type OperationTypeNode = any;
import { equal } from '@wry/equality';

import { ApolloLink, execute, FetchResult } from '../link/core';
import { isExecutionPatchIncrementalResult } from '../utilities/common/incrementalResult';
import { Cache, ApolloCache, canonicalStringify } from '../cache';

import {
Expand Down Expand Up @@ -434,7 +435,7 @@ export class QueryManager<TStore> {
returnPartialData: true,
});

if (diff.complete) {
if (diff.complete && !(isExecutionPatchIncrementalResult(result))) {
result = { ...result, data: diff.result };
}
}
Expand Down Expand Up @@ -1047,15 +1048,27 @@ export class QueryManager<TStore> {
),

result => {
const hasErrors = isNonEmptyArray(result.errors);
const graphQLErrors = isNonEmptyArray(result.errors)
? result.errors.slice(0)
: [];

if ('incremental' in result && isNonEmptyArray(result.incremental)) {
result.incremental.forEach(incrementalResult => {
if (incrementalResult.errors) {
graphQLErrors.push(...incrementalResult.errors);
}
});
}

const hasErrors = isNonEmptyArray(graphQLErrors);

// If we interrupted this request by calling getResultsFromLink again
// with the same QueryInfo object, we ignore the old results.
if (requestId >= queryInfo.lastRequestId) {
if (hasErrors && options.errorPolicy === "none") {
// Throwing here effectively calls observer.error.
throw queryInfo.markError(new ApolloError({
graphQLErrors: result.errors,
graphQLErrors,
}));
}
queryInfo.markResult(result, options, cacheWriteBehavior);
Expand All @@ -1069,7 +1082,7 @@ export class QueryManager<TStore> {
};

if (hasErrors && options.errorPolicy !== "ignore") {
aqr.errors = result.errors;
aqr.errors = graphQLErrors;
aqr.networkStatus = NetworkStatus.error;
}

Expand Down

0 comments on commit f2bb099

Please sign in to comment.