Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: queryDeduplication from context #6261

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 111 additions & 11 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1356,20 +1356,23 @@ describe('client', () => {
},
};

// we have two responses for identical queries, but only the first should be requested.
// the second one should never make it through to the network interface.
const link = mockSingleLink({
request: { query: queryDoc },
result: { data },
delay: 10,
}, {
request: { query: queryDoc },
result: { data: data2 },
}).setOnError(reject);
// we have two responses for identical queries, and both should be requested.
// the second one should make it through to the network interface.
const link = mockSingleLink(
{
request: { query: queryDoc },
result: { data },
delay: 10,
},
{
request: { query: queryDoc },
result: { data: data2 },
},
).setOnError(reject);

const client = new ApolloClient({
link,
cache: new InMemoryCache({ addTypename: false }),

queryDeduplication: false,
});

Expand Down Expand Up @@ -1426,6 +1429,103 @@ describe('client', () => {
}).then(resolve, reject);
});

it('deduplicates queries if query context.queryDeduplication is set to true', () => {
const queryDoc = gql`
query {
author {
name
}
}
`;
const data = {
author: {
name: 'Jonas',
},
};
const data2 = {
author: {
name: 'Dhaivat',
},
};

// we have two responses for identical queries, but only the first should be requested.
// the second one should never make it through to the network interface.
const link = mockSingleLink(
{
request: { query: queryDoc },
result: { data },
delay: 10,
},
{
request: { query: queryDoc },
result: { data: data2 },
},
);
const client = new ApolloClient({
link,
cache: new InMemoryCache({ addTypename: false }),
queryDeduplication: false,
});

// Both queries need to be deduplicated, otherwise only one gets tracked
const q1 = client.query({ query: queryDoc, context: { queryDeduplication: true } });
const q2 = client.query({ query: queryDoc, context: { queryDeduplication: true } });

// if deduplication happened, result2.data will equal data.
return Promise.all([q1, q2]).then(([result1, result2]) => {
expect(stripSymbols(result1.data)).toEqual(data);
expect(stripSymbols(result2.data)).toEqual(data);
});
});

it('does not deduplicate queries if query context.queryDeduplication is set to false', () => {
const queryDoc = gql`
query {
author {
name
}
}
`;
const data = {
author: {
name: 'Jonas',
},
};
const data2 = {
author: {
name: 'Dhaivat',
},
};

// we have two responses for identical queries, and both should be requested.
// the second one should make it through to the network interface.
const link = mockSingleLink(
{
request: { query: queryDoc },
result: { data },
delay: 10,
},
{
request: { query: queryDoc },
result: { data: data2 },
},
);
const client = new ApolloClient({
link,
cache: new InMemoryCache({ addTypename: false }),
});

// The first query gets tracked in the dedup logic, the second one ignores it and runs anyways
const q1 = client.query({ query: queryDoc });
const q2 = client.query({ query: queryDoc, context: { queryDeduplication: false } });

// if deduplication happened, result2.data will equal data.
return Promise.all([q1, q2]).then(([result1, result2]) => {
expect(stripSymbols(result1.data)).toEqual(data);
expect(stripSymbols(result2.data)).toEqual(data2);
});
});

itAsync('unsubscribes from deduplicated observables only once', (resolve, reject) => {
const document: DocumentNode = gql`
query test1($x: String) {
Expand Down
5 changes: 4 additions & 1 deletion src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,10 @@ export class QueryManager<TStore> {
query: DocumentNode,
context: any,
variables?: OperationVariables,
deduplication: boolean = this.queryDeduplication,
deduplication: boolean =
// Prefer context.queryDeduplication if specified.
context?.queryDeduplication ??
this.queryDeduplication,
): Observable<FetchResult<T>> {
let observable: Observable<FetchResult<T>>;

Expand Down
45 changes: 45 additions & 0 deletions src/core/__tests__/QueryManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ describe('QueryManager', () => {
link,
config = {},
clientAwareness = {},
queryDeduplication = false,
}: {
link: ApolloLink;
config?: Partial<InMemoryCacheConfig>;
clientAwareness?: { [key: string]: string };
queryDeduplication?: boolean;
}) => {
return new QueryManager({
link,
cache: new InMemoryCache({ addTypename: false, ...config }),
clientAwareness,
queryDeduplication,
});
};

Expand Down Expand Up @@ -5039,4 +5042,46 @@ describe('QueryManager', () => {
});
});
});

describe('queryDeduplication', () => {
it('should be true when context is true, default is false and argument not provided', () => {
const query = gql`
query {
author {
firstName
}
}
`;
const queryManager = createQueryManager({
link: mockSingleLink({
request: { query },
result: { author: { firstName: 'John' } },
}),
});

queryManager.query({ query, context: { queryDeduplication: true } })

expect(queryManager['inFlightLinkObservables'].size).toBe(1)
});
it('should allow overriding global queryDeduplication: true to false', () => {
const query = gql`
query {
author {
firstName
}
}
`;
const queryManager = createQueryManager({
link: mockSingleLink({
request: { query },
result: { author: { firstName: 'John' } },
}),
queryDeduplication: true,
});

queryManager.query({ query, context: { queryDeduplication: false } })

expect(queryManager['inFlightLinkObservables'].size).toBe(0)
});
})
});