Skip to content

Commit

Permalink
fix nextFetchPolicy behaviour with transformed documents (#11235)
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas authored Sep 28, 2023
1 parent 76539d9 commit 6cddaaf
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-snakes-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Fix nextFetchPolicy behaviour with transformed documents by keeping `options` reference stable when passing it through QueryManager.
17 changes: 7 additions & 10 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,15 +728,17 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`,

private fetch(
options: WatchQueryOptions<TVariables, TData>,
newNetworkStatus?: NetworkStatus
newNetworkStatus?: NetworkStatus,
query?: DocumentNode
) {
// TODO Make sure we update the networkStatus (and infer fetchVariables)
// before actually committing to the fetch.
this.queryManager.setObservableQuery(this);
return this.queryManager["fetchConcastWithInfo"](
this.queryId,
options,
newNetworkStatus
newNetworkStatus,
query
);
}

Expand Down Expand Up @@ -883,20 +885,15 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`,
}
}

// If the transform doesn't change the document, leave `options` alone and
// use the original object.
const fetchOptions =
query === options.query ? options : { ...options, query };

this.waitForOwnResult &&= skipCacheDataFor(fetchOptions.fetchPolicy);
this.waitForOwnResult &&= skipCacheDataFor(options.fetchPolicy);
const finishWaitingForOwnResult = () => {
if (this.concast === concast) {
this.waitForOwnResult = false;
}
};

const variables = fetchOptions.variables && { ...fetchOptions.variables };
const { concast, fromLink } = this.fetch(fetchOptions, newNetworkStatus);
const variables = options.variables && { ...options.variables };
const { concast, fromLink } = this.fetch(options, newNetworkStatus, query);
const observer: Observer<ApolloQueryResult<TData>> = {
next: (result) => {
finishWaitingForOwnResult();
Expand Down
4 changes: 2 additions & 2 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1220,9 +1220,9 @@ export class QueryManager<TStore> {
// The initial networkStatus for this fetch, most often
// NetworkStatus.loading, but also possibly fetchMore, poll, refetch,
// or setVariables.
networkStatus = NetworkStatus.loading
networkStatus = NetworkStatus.loading,
query = options.query
): ConcastAndInfo<TData> {
const { query } = options;
const variables = this.getVariables(query, options.variables) as TVars;
const queryInfo = this.getQuery(queryId);

Expand Down
85 changes: 57 additions & 28 deletions src/core/__tests__/fetchPolicies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,16 +814,24 @@ describe("cache-and-network", function () {

describe("nextFetchPolicy", () => {
type TData = {
linkCounter: number;
opName: string;
opVars: Record<string, any>;
echo: {
linkCounter: number;
opName: string;
opVars: Record<string, any>;
};
};

type TVars = {
refetching?: boolean;
};

const EchoQuery: TypedDocumentNode<TData> = gql`
query EchoQuery {
linkCounter
opName
opVars
echo {
linkCounter
opName
opVars
}
}
`;

Expand All @@ -835,9 +843,12 @@ describe("nextFetchPolicy", () => {
setTimeout(() => {
observer.next({
data: {
linkCounter: ++linkCounter,
opName: request.operationName,
opVars: request.variables,
echo: {
__typename: "Echo",
linkCounter: ++linkCounter,
opName: request.operationName,
opVars: request.variables,
},
},
});
observer.complete();
Expand All @@ -846,9 +857,9 @@ describe("nextFetchPolicy", () => {
);
}

const checkNextFetchPolicy = <TData, TVars extends object>(args: {
const checkNextFetchPolicy = (args: {
fetchPolicy: WatchQueryFetchPolicy;
nextFetchPolicy: WatchQueryOptions<TVars, TData>["nextFetchPolicy"];
nextFetchPolicy: WatchQueryOptions<{}, TData>["nextFetchPolicy"];
useDefaultOptions: boolean;
onResult(info: {
count: number;
Expand All @@ -867,7 +878,9 @@ describe("nextFetchPolicy", () => {
(resolve, reject) => {
const client = new ApolloClient({
link: makeLink(),
cache: new InMemoryCache(),
cache: new InMemoryCache({
addTypename: true,
}),
defaultOptions: {
watchQuery: args.useDefaultOptions
? {
Expand Down Expand Up @@ -917,7 +930,8 @@ describe("nextFetchPolicy", () => {
}) => {
if (count === 1) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 1,
opName: "EchoQuery",
opVars: {},
Expand All @@ -930,7 +944,8 @@ describe("nextFetchPolicy", () => {
refetching: true,
})
.then((result) => {
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -941,7 +956,8 @@ describe("nextFetchPolicy", () => {
.catch(reject);
} else if (count === 2) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -959,7 +975,8 @@ describe("nextFetchPolicy", () => {
})
.then((result) => {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 3,
opName: "EchoQuery",
opVars: {
Expand All @@ -973,7 +990,8 @@ describe("nextFetchPolicy", () => {
expect(observable.options.fetchPolicy).toBe("cache-first");
} else if (count === 3) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 3,
opName: "EchoQuery",
opVars: {
Expand Down Expand Up @@ -1044,7 +1062,8 @@ describe("nextFetchPolicy", () => {
}) => {
if (count === 1) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 1,
opName: "EchoQuery",
opVars: {},
Expand All @@ -1057,7 +1076,8 @@ describe("nextFetchPolicy", () => {
refetching: true,
})
.then((result) => {
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -1068,7 +1088,8 @@ describe("nextFetchPolicy", () => {
.catch(reject);
} else if (count === 2) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -1086,7 +1107,8 @@ describe("nextFetchPolicy", () => {
})
.then((result) => {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 3,
opName: "EchoQuery",
opVars: {
Expand All @@ -1100,7 +1122,8 @@ describe("nextFetchPolicy", () => {
// expect(observable.options.fetchPolicy).toBe("cache-and-network");
} else if (count === 3) {
expect(result.loading).toBe(true);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -1112,7 +1135,8 @@ describe("nextFetchPolicy", () => {
expect(observable.options.fetchPolicy).toBe("cache-first");
} else if (count === 4) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 3,
opName: "EchoQuery",
opVars: {
Expand Down Expand Up @@ -1191,7 +1215,8 @@ describe("nextFetchPolicy", () => {
}) => {
if (count === 1) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 1,
opName: "EchoQuery",
opVars: {},
Expand All @@ -1204,7 +1229,8 @@ describe("nextFetchPolicy", () => {
refetching: true,
})
.then((result) => {
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -1215,7 +1241,8 @@ describe("nextFetchPolicy", () => {
.catch(reject);
} else if (count === 2) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -1233,7 +1260,8 @@ describe("nextFetchPolicy", () => {
})
.then((result) => {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -1250,7 +1278,8 @@ describe("nextFetchPolicy", () => {
expect(observable.options.fetchPolicy).toBe("cache-first");
} else if (count === 3) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand Down

0 comments on commit 6cddaaf

Please sign in to comment.