Skip to content

Commit

Permalink
transport minified string version of query. (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Apr 11, 2024
1 parent b619e76 commit d82ff92
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
Expand Up @@ -74,7 +74,7 @@ export type TransportIdentifier = string & { __transportIdentifier: true };
export type QueryEvent =
| {
type: "started";
options: WatchQueryOptions;
options: { query: string } & Omit<WatchQueryOptions, "query">;
id: TransportIdentifier;
}
| {
Expand Down
Expand Up @@ -7,17 +7,15 @@ import type {
TransportIdentifier,
} from "./DataTransportAbstraction.js";

import type {
TypedDocumentNode,
WatchQueryOptions,
} from "@apollo/client/index.js";
import type { TypedDocumentNode } from "@apollo/client/index.js";
import { MockSubscriptionLink } from "@apollo/client/testing/core/mocking/mockSubscriptionLink.js";
import {
useSuspenseQuery,
gql,
DocumentTransform,
} from "@apollo/client/index.js";
import { visit, Kind, print, isDefinitionNode } from "graphql";
import { printMinified } from "./printMinified.js";

const {
ApolloClient,
Expand Down Expand Up @@ -45,16 +43,15 @@ describe(
me
}
`;
const FIRST_REQUEST: WatchQueryOptions = {
fetchPolicy: "cache-first",
nextFetchPolicy: undefined,
notifyOnNetworkStatusChange: false,
query: QUERY_ME,
};
const EVENT_STARTED: QueryEvent = {
type: "started",
id: "1" as any,
options: FIRST_REQUEST,
options: {
fetchPolicy: "cache-first",
nextFetchPolicy: undefined,
notifyOnNetworkStatusChange: false,
query: printMinified(QUERY_ME),
},
};
const FIRST_RESULT = { me: "User" };
const EVENT_DATA: QueryEvent = {
Expand Down Expand Up @@ -423,7 +420,7 @@ describe("document transforms are applied correctly", async () => {
type: "started",
id: "1" as TransportIdentifier,
options: {
query: untransformedQuery,
query: printMinified(untransformedQuery),
},
});
client.rerunSimulatedQueries!();
Expand All @@ -450,7 +447,7 @@ describe("document transforms are applied correctly", async () => {
type: "started",
id: "1" as TransportIdentifier,
options: {
query: untransformedQuery,
query: printMinified(untransformedQuery),
},
});
client.onQueryProgress!({
Expand Down
Expand Up @@ -9,6 +9,7 @@ import type {
import {
ApolloClient as OrigApolloClient,
Observable,
gql,
} from "@apollo/client/index.js";
import type { QueryManager } from "@apollo/client/core/QueryManager.js";
import { print } from "@apollo/client/utilities/index.js";
Expand All @@ -24,6 +25,7 @@ import type {
TransportIdentifier,
} from "./DataTransportAbstraction.js";
import { bundle } from "../bundleInfo.js";
import { printMinified } from "./printMinified.js";

function getQueryManager<TCacheShape>(
client: OrigApolloClient<unknown>
Expand Down Expand Up @@ -123,7 +125,10 @@ class ApolloClientSSRImpl<TCacheShape> extends ApolloClientBase<TCacheShape> {
this.watchQueryQueue.push({
event: {
type: "started",
options: options as WatchQueryOptions<any>,
options: {
...(options as WatchQueryOptions<any>),
query: printMinified(options.query),
},
id,
},
observable: streamObservable,
Expand Down Expand Up @@ -179,8 +184,13 @@ export class ApolloClientBrowserImpl<
options,
id,
}: Extract<QueryEvent, { type: "started" }>) => {
const { query, varJson, cacheKey } = this.identifyUniqueQuery(options);
this.transportedQueryOptions.set(id, options);
const hydratedOptions = {
...options,
query: gql(options.query),
};
const { query, varJson, cacheKey } =
this.identifyUniqueQuery(hydratedOptions);
this.transportedQueryOptions.set(id, hydratedOptions);

if (!query) return;
const printedServerQuery = print(query);
Expand Down Expand Up @@ -209,7 +219,11 @@ export class ApolloClientBrowserImpl<
const promise = new Promise<FetchResult>((resolve, reject) => {
this.simulatedStreamingQueries.set(
id,
(simulatedStreamingQuery = { resolve, reject, options })
(simulatedStreamingQuery = {
resolve,
reject,
options: hydratedOptions,
})
);
});

Expand Down
@@ -0,0 +1,7 @@
import type { DocumentNode } from "@apollo/client/index.js";
import { print } from "@apollo/client/utilities/index.js";
import { stripIgnoredCharacters } from "graphql";

export function printMinified(query: DocumentNode): string {
return stripIgnoredCharacters(print(query));
}

0 comments on commit d82ff92

Please sign in to comment.