Skip to content

Commit

Permalink
Merge pull request #6526 from Kujawadl/version-2.6
Browse files Browse the repository at this point in the history
Use context.queryDeduplication if provided. Similar to #6261, but for AC2 instead of AC3.
  • Loading branch information
benjamn authored Jul 20, 2020
2 parents e831fbb + f5a8bcf commit 173f4ce
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 3 deletions.
101 changes: 99 additions & 2 deletions packages/apollo-client/src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1508,8 +1508,8 @@ 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.
// 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 },
Expand Down Expand Up @@ -1584,6 +1584,103 @@ describe('client', () => {
});
});

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);
});
});

it('unsubscribes from deduplicated observables only once', done => {
const document: DocumentNode = gql`
query test1($x: String) {
Expand Down
5 changes: 4 additions & 1 deletion packages/apollo-client/src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,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 packages/apollo-client/src/core/__tests__/QueryManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,20 @@ describe('QueryManager', () => {
link,
config = {},
clientAwareness = {},
queryDeduplication = false,
}: {
link?: ApolloLink;
config?: ApolloReducerConfig;
clientAwareness?: { [key: string]: string };
queryDeduplication?: boolean;
}) => {
return new QueryManager({
link: link || mockSingleLink(),
store: new DataStore(
new InMemoryCache({ addTypename: false, ...config }),
),
clientAwareness,
queryDeduplication,
});
};

Expand Down Expand Up @@ -5157,4 +5160,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)
});
})
});

0 comments on commit 173f4ce

Please sign in to comment.