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 failing tests and add new deduplication tests #1

Closed
wants to merge 9 commits into from
95 changes: 93 additions & 2 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1348,8 +1348,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 },
result: { data },
Expand Down Expand Up @@ -1418,6 +1418,97 @@ describe('client', () => {
}).then(resolve, reject);
});

itAsync('deduplicates queries if query context.queryDeduplication is set to true', (resolve, reject) => {
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 },
}).setOnError(reject);
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);
}).then(resolve, reject);
});

itAsync('does not deduplicate queries if query context.queryDeduplication is set to false', (resolve, reject) => {
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 },
}).setOnError(reject);
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);
}).then(resolve, reject);
});

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

// Set default deduplication value if arg was not passed
if(typeof deduplication === 'undefined') {
if(typeof context === 'object' && 'queryDeduplication' in context) {
deduplication = context.queryDeduplication
} else {
deduplication = this.queryDeduplication
}
}

const { serverQuery } = this.transform(query);
if (serverQuery) {
const { inFlightLinkObservables, link } = this;
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 @@ -4926,4 +4929,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)
});
})
});