From 6023be2a0bcd18c59d85c819ad6604b89484a6fc Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Tue, 10 Oct 2023 14:53:17 +0200 Subject: [PATCH] Collect usage only when Apollo Server did resolve source (#3017) --- .changeset/curvy-maps-deny.md | 5 +++ packages/libraries/client/src/apollo.ts | 39 +++++++++++++++++++ .../libraries/client/src/internal/types.ts | 1 + .../libraries/client/src/internal/usage.ts | 4 +- 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 .changeset/curvy-maps-deny.md diff --git a/.changeset/curvy-maps-deny.md b/.changeset/curvy-maps-deny.md new file mode 100644 index 000000000..7ff93d4f1 --- /dev/null +++ b/.changeset/curvy-maps-deny.md @@ -0,0 +1,5 @@ +--- +'@graphql-hive/client': patch +--- + +Do not collect usage when Apollo Server did not resolve source diff --git a/packages/libraries/client/src/apollo.ts b/packages/libraries/client/src/apollo.ts index 7da1e5e0d..c7e2d857a 100644 --- a/packages/libraries/client/src/apollo.ts +++ b/packages/libraries/client/src/apollo.ts @@ -153,6 +153,7 @@ export function hiveApollo(clientOrOptions: HiveClient | HivePluginOptions): Apo const isLegacyV3 = 'context' in context; let doc: DocumentNode; + let didResolveSource = false; const complete = hive.collectUsage(); const args = { schema: context.schema, @@ -166,7 +167,18 @@ export function hiveApollo(clientOrOptions: HiveClient | HivePluginOptions): Apo if (isLegacyV0) { return { + didResolveSource() { + didResolveSource = true; + }, willSendResponse(ctx: any) { + if (!didResolveSource) { + complete(args, { + action: 'abort', + reason: 'Did not resolve source', + logging: false, + }); + return; + } doc = ctx.document; complete(args, ctx.response); }, @@ -175,12 +187,25 @@ export function hiveApollo(clientOrOptions: HiveClient | HivePluginOptions): Apo if (isLegacyV3) { return Promise.resolve({ + didResolveSource() { + didResolveSource = true; + }, async willSendResponse(ctx) { + if (!didResolveSource) { + complete(args, { + action: 'abort', + reason: 'Did not resolve source', + logging: false, + }); + return; + } + if (!ctx.document) { const details = ctx.operationName ? `operationName: ${ctx.operationName}` : ''; complete(args, { action: 'abort', reason: 'Document is not available' + (details ? ` (${details})` : ''), + logging: true, }); return; } @@ -193,12 +218,25 @@ export function hiveApollo(clientOrOptions: HiveClient | HivePluginOptions): Apo // v4 return Promise.resolve({ + didResolveSource() { + didResolveSource = true; + }, async willSendResponse(ctx) { + if (!didResolveSource) { + complete(args, { + action: 'abort', + reason: 'Did not resolve source', + logging: false, + }); + return; + } + if (!ctx.document) { const details = ctx.operationName ? `operationName: ${ctx.operationName}` : ''; complete(args, { action: 'abort', reason: 'Document is not available' + (details ? ` (${details})` : ''), + logging: true, }); return; } @@ -208,6 +246,7 @@ export function hiveApollo(clientOrOptions: HiveClient | HivePluginOptions): Apo complete(args, { action: 'abort', reason: '@defer and @stream is not supported by Hive', + logging: true, }); } else { complete(args, ctx.response.body.singleResult); diff --git a/packages/libraries/client/src/internal/types.ts b/packages/libraries/client/src/internal/types.ts index 22e13ddf1..962b33d05 100644 --- a/packages/libraries/client/src/internal/types.ts +++ b/packages/libraries/client/src/internal/types.ts @@ -19,6 +19,7 @@ export type AsyncIterableOrValue = AsyncIterable | T; export type AbortAction = { action: 'abort'; reason: string; + logging: boolean; }; export type CollectUsageCallback = ( diff --git a/packages/libraries/client/src/internal/usage.ts b/packages/libraries/client/src/internal/usage.ts index 5e263fccb..544e22d9b 100644 --- a/packages/libraries/client/src/internal/usage.ts +++ b/packages/libraries/client/src/internal/usage.ts @@ -157,7 +157,9 @@ export function createUsage(pluginOptions: HivePluginOptions): UsageCollector { let providedOperationName: string | undefined = undefined; try { if (isAbortAction(result)) { - logger.info(result.reason); + if (result.logging) { + logger.info(result.reason); + } return; }