Collect usage only when Apollo Server did resolve source (#3017)

This commit is contained in:
Kamil Kisiela 2023-10-10 14:53:17 +02:00 committed by GitHub
parent dc683c2e68
commit 6023be2a0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'@graphql-hive/client': patch
---
Do not collect usage when Apollo Server did not resolve source

View file

@ -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);

View file

@ -19,6 +19,7 @@ export type AsyncIterableOrValue<T> = AsyncIterable<T> | T;
export type AbortAction = {
action: 'abort';
reason: string;
logging: boolean;
};
export type CollectUsageCallback = (

View file

@ -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;
}