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

Use printed query for deduplication, cache print calls #10968

Merged
merged 5 commits into from
Jun 14, 2023
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
5 changes: 5 additions & 0 deletions .changeset/rude-frogs-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apollo/client': patch
---

Use printed query for query deduplication. Cache `print` calls for GraphQL documents to speed up repeated operations.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ src/utilities/globals/*
!src/utilities/graphql
src/utilities/graphql/*
!src/utilities/graphql/operations.ts
!src/utilities/graphql/print.ts
!src/utilities/graphql/DocumentTransform.ts
!src/utilities/graphql/__tests__/
src/utilities/graphql/__tests__/*
Expand Down
4 changes: 2 additions & 2 deletions .size-limit.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const checks = [
{
path: "dist/apollo-client.min.cjs",
limit: "37460"
limit: "37479"
},
{
path: "dist/main.cjs",
Expand All @@ -10,7 +10,7 @@ const checks = [
{
path: "dist/index.js",
import: "{ ApolloClient, InMemoryCache, HttpLink }",
limit: "33243"
limit: "33269"
},
...[
"ApolloProvider",
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/exports.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ Array [
"mergeOptions",
"offsetLimitPagination",
"omitDeep",
"print",
"relayStylePagination",
"removeArgumentsFromDocument",
"removeClientSetsFromDocument",
Expand Down
10 changes: 6 additions & 4 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import {
} from './QueryInfo';
import type { ApolloErrorOptions } from '../errors';
import { PROTOCOL_ERRORS_SYMBOL } from '../errors';
import { print } from '../utilities';

const { hasOwnProperty } = Object.prototype;

Expand Down Expand Up @@ -1027,7 +1028,7 @@ export class QueryManager<TStore> {
}

private inFlightLinkObservables = new Map<
DocumentNode,
string,
Map<string, Observable<FetchResult>>
>();

Expand Down Expand Up @@ -1059,8 +1060,9 @@ export class QueryManager<TStore> {
context = operation.context;

if (deduplication) {
const byVariables = inFlightLinkObservables.get(serverQuery) || new Map();
inFlightLinkObservables.set(serverQuery, byVariables);
const printedServerQuery = print(serverQuery);
const byVariables = inFlightLinkObservables.get(printedServerQuery) || new Map();
inFlightLinkObservables.set(printedServerQuery, byVariables);

const varJson = canonicalStringify(variables);
observable = byVariables.get(varJson);
Expand All @@ -1075,7 +1077,7 @@ export class QueryManager<TStore> {
concast.beforeNext(() => {
if (byVariables.delete(varJson) &&
byVariables.size < 1) {
inFlightLinkObservables.delete(serverQuery);
inFlightLinkObservables.delete(printedServerQuery);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/link/http/selectHttpOptionsAndBody.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ASTNode} from 'graphql';
import { print } from 'graphql';
import { print } from '../../utilities';

import type { Operation } from '../core';

Expand Down
2 changes: 1 addition & 1 deletion src/link/persisted-queries/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { invariant } from '../../utilities/globals';

import { print } from 'graphql';
import { print } from '../../utilities';
import type {
DocumentNode,
ExecutionResult,
Expand Down
2 changes: 1 addition & 1 deletion src/link/subscriptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import { print } from "graphql";
import { print } from '../../utilities';
import type { Client } from "graphql-ws";

import type { Operation, FetchResult } from "../core";
Expand Down
2 changes: 1 addition & 1 deletion src/testing/core/mocking/mockLink.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { invariant } from '../../../utilities/globals';

import { print } from 'graphql';
import { equal } from '@wry/equality';

import type {
Expand All @@ -18,6 +17,7 @@ import {
removeConnectionDirectiveFromDocument,
cloneDeep,
stringifyForDisplay,
print
} from '../../../utilities';

export type ResultFunction<T> = () => T;
Expand Down
3 changes: 1 addition & 2 deletions src/testing/matchers/toMatchDocument.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { checkDocument } from '../../utilities';
import { print } from 'graphql';
import { checkDocument, print } from '../../utilities';
import type { DocumentNode } from '../../core';
import type { MatcherFunction } from 'expect';

Expand Down
14 changes: 14 additions & 0 deletions src/utilities/graphql/print.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding this file to .prettierignore and formatting this? Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do so tomorrow :)

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { print as origPrint } from 'graphql';
import { canUseWeakMap } from '../common/canUse';

const printCache = canUseWeakMap ? new WeakMap() : undefined;
export const print: typeof origPrint = (ast) => {
let result;
result = printCache?.get(ast);

if (!result) {
result = origPrint(ast);
printCache?.set(ast, result);
}
return result;
};
4 changes: 4 additions & 0 deletions src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export {
getDefaultValues,
} from './graphql/getFromAST';

export {
print
} from './graphql/print';

export {
StoreObject,
Reference,
Expand Down