Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(web): handle async-graphql legacy projection errors (#6060)

Sean Aye382a8d56869c
diff --git a/apps/web/src/lib/service-clients/service-storage/graphql-soup.test.ts b/apps/web/src/lib/service-clients/service-storage/graphql-soup.test.ts
index 2dc0f98ffa..dfceae62bf 100644
--- a/apps/web/src/lib/service-clients/service-storage/graphql-soup.test.ts
+++ b/apps/web/src/lib/service-clients/service-storage/graphql-soup.test.ts
@@ -216,48 +216,78 @@ describe('GraphQL Soup browser cache session gate', () => {
expect(soup.graphqlSoupProjectionSupported()).toBe(true);
});
- it('retries a new client against an old server without projection local authority', async () => {
- mocks.platformFetch
- .mockResolvedValueOnce(
- new Response(
- JSON.stringify({
- errors: [
- {
- message:
- 'Cannot query field "cacheProjection" on type "GraphqlSoupEntity".',
- },
- ],
- }),
- { status: 200, headers: { 'content-type': 'application/json' } }
- )
- )
- .mockResolvedValueOnce(
- new Response(
- JSON.stringify({ data: { user: { soup: { items: [] } } } }),
- {
- status: 200,
- headers: { 'content-type': 'application/json' },
- }
- )
- );
+ it('strips client-only directives from GraphQL transport documents', async () => {
+ mocks.platformFetch.mockResolvedValueOnce(
+ new Response(JSON.stringify({ data: { user: { id: 'user-1' } } }), {
+ status: 200,
+ headers: { 'content-type': 'application/json' },
+ })
+ );
const soup = await import('./graphql-soup');
- const query = `query Soup {
- user { soup { items { __typename id cacheProjection @cacheOnly } } }
+ const query = `query Soup($includeId: Boolean!) {
+ user {
+ id @include(if: $includeId)
+ soup { items { cacheProjection @cacheOnly } }
+ }
}`;
- const response = await soup.dssGraphqlFetch('http://dss.test/graphql', {
+ await soup.dssGraphqlFetch('http://dss.test/graphql', {
method: 'POST',
- body: JSON.stringify({ query }),
+ body: JSON.stringify({ query, variables: { includeId: true } }),
});
- expect(response.status).toBe(200);
- expect(mocks.platformFetch).toHaveBeenCalledTimes(2);
- const retry = mocks.platformFetch.mock.calls[1]?.[1] as RequestInit;
- expect(JSON.parse(retry.body as string).query).not.toContain(
- 'cacheProjection'
- );
- expect(soup.graphqlSoupProjectionSupported()).toBe(false);
+ expect(mocks.platformFetch).toHaveBeenCalledOnce();
+ const transport = mocks.platformFetch.mock.calls[0]?.[1] as RequestInit;
+ const payload = JSON.parse(transport.body as string) as {
+ query: string;
+ variables: { includeId: boolean };
+ };
+ expect(payload.query).not.toContain('@cacheOnly');
+ expect(payload.query).toContain('cacheProjection');
+ expect(payload.query).toContain('@include');
+ expect(payload.variables).toEqual({ includeId: true });
});
+ it.each([
+ 'Cannot query field "cacheProjection" on type "GraphqlSoupEntity".',
+ 'Unknown field "cacheProjection" on type "GraphqlSoupEntity".',
+ ])(
+ 'retries a new client against an old server without projection local authority: %s',
+ async (validationMessage) => {
+ mocks.platformFetch
+ .mockResolvedValueOnce(
+ new Response(
+ JSON.stringify({ errors: [{ message: validationMessage }] }),
+ { status: 200, headers: { 'content-type': 'application/json' } }
+ )
+ )
+ .mockResolvedValueOnce(
+ new Response(
+ JSON.stringify({ data: { user: { soup: { items: [] } } } }),
+ {
+ status: 200,
+ headers: { 'content-type': 'application/json' },
+ }
+ )
+ );
+ const soup = await import('./graphql-soup');
+ const query = `query Soup {
+ user { soup { items { __typename id cacheProjection @cacheOnly } } }
+ }`;
+ const response = await soup.dssGraphqlFetch('http://dss.test/graphql', {
+ method: 'POST',
+ body: JSON.stringify({ query }),
+ });
+
+ expect(response.status).toBe(200);
+ expect(mocks.platformFetch).toHaveBeenCalledTimes(2);
+ const retry = mocks.platformFetch.mock.calls[1]?.[1] as RequestInit;
+ expect(JSON.parse(retry.body as string).query).not.toContain(
+ 'cacheProjection'
+ );
+ expect(soup.graphqlSoupProjectionSupported()).toBe(false);
+ }
+ );
+
it('keeps GraphQL notification subscriptions active when the cache is disabled', async () => {
mocks.enabled = false;
const soup = await import('./graphql-soup');
diff --git a/apps/web/src/lib/service-clients/service-storage/graphql-soup.ts b/apps/web/src/lib/service-clients/service-storage/graphql-soup.ts
index ac9d7a2c53..4f94213f0d 100644
--- a/apps/web/src/lib/service-clients/service-storage/graphql-soup.ts
+++ b/apps/web/src/lib/service-clients/service-storage/graphql-soup.ts
@@ -39,7 +39,7 @@ import {
type RequestPolicy,
subscriptionExchange,
} from '@urql/core';
-import { parse, print, visit } from 'graphql';
+import { type DocumentNode, parse, print, visit } from 'graphql';
import {
createClient as createGraphqlWsClient,
type Client as GraphqlWsClient,
@@ -117,6 +117,48 @@ export function graphqlSoupProjectionSupported(): boolean {
return soupProjectionServerSupported;
}
+function stripGraphqlSoupClientDirectives(
+ document: DocumentNode
+): DocumentNode {
+ return visit(document, {
+ Directive(node) {
+ return node.name.value === 'cacheOnly' ? null : undefined;
+ },
+ });
+}
+
+/** Removes client-only directives from a document before network transport. */
+export function graphqlSoupTransportDocument(query: string): string {
+ return print(stripGraphqlSoupClientDirectives(parse(query)));
+}
+
+function graphqlSoupTransportRequest(
+ init: RequestInit | undefined
+): RequestInit | undefined {
+ if (typeof init?.body !== 'string') return init;
+ try {
+ const payload: unknown = JSON.parse(init.body);
+ if (
+ payload === null ||
+ typeof payload !== 'object' ||
+ !('query' in payload) ||
+ typeof payload.query !== 'string' ||
+ !payload.query.includes('@cacheOnly')
+ ) {
+ return init;
+ }
+ return {
+ ...init,
+ body: JSON.stringify({
+ ...payload,
+ query: graphqlSoupTransportDocument(payload.query),
+ }),
+ };
+ } catch {
+ return init;
+ }
+}
+
/** Removes only the additive cache metadata field for a legacy-server retry. */
export function legacySoupProjectionDocument(query: string): string {
return print(
@@ -176,7 +218,9 @@ async function isLegacyProjectionValidationError(
typeof error === 'object' &&
'message' in error &&
typeof error.message === 'string' &&
- /Cannot query field ["']cacheProjection["']/.test(error.message)
+ /(?:Cannot query|Unknown) field ["']cacheProjection["']/.test(
+ error.message
+ )
)
);
} catch {
@@ -188,8 +232,9 @@ export async function dssGraphqlFetch(
input: RequestInfo | URL,
init?: RequestInit
): Promise<Response> {
- const response = await authorizedDssGraphqlFetch(input, init);
- const legacyInit = legacyProjectionRequest(init);
+ const transportInit = graphqlSoupTransportRequest(init);
+ const response = await authorizedDssGraphqlFetch(input, transportInit);
+ const legacyInit = legacyProjectionRequest(transportInit);
if (
legacyInit === undefined ||
!(await isLegacyProjectionValidationError(response))
@@ -237,7 +282,7 @@ function graphqlSoupSubscriptionExchange(websocketClient: GraphqlWsClient) {
return subscriptionExchange({
forwardSubscription(payload, request) {
const graphqlWsPayload = {
- query: print(request.query),
+ query: print(stripGraphqlSoupClientDirectives(request.query)),
operationName: payload.operationName,
variables: payload.variables,
extensions: payload.extensions,