Skip to content

Commit 3e7a4d0

Browse files
committed
fix after review
1 parent f0c0edf commit 3e7a4d0

File tree

4 files changed

+45
-44
lines changed

4 files changed

+45
-44
lines changed

packages/plugins/apollo-usage-report/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@apollo/server-gateway-interface": "^1.1.1",
4545
"@apollo/usage-reporting-protobuf": "^4.1.1",
4646
"@apollo/utils.usagereporting": "^2.1.0",
47+
"@graphql-tools/utils": "^10.8.6",
4748
"@graphql-yoga/plugin-apollo-inline-trace": "workspace:^",
4849
"@whatwg-node/promise-helpers": "^1.2.4",
4950
"tslib": "^2.8.1"

packages/plugins/apollo-usage-report/src/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DocumentNode, getOperationAST, GraphQLSchema, Kind, printSchema } from 'graphql';
1+
import { DocumentNode, getOperationAST, GraphQLSchema, Kind } from 'graphql';
22
import {
33
isAsyncIterable,
44
YogaLogger,
@@ -12,6 +12,7 @@ import {
1212
calculateReferencedFieldsByType,
1313
usageReportingSignature,
1414
} from '@apollo/utils.usagereporting';
15+
import { printSchemaWithDirectives } from '@graphql-tools/utils';
1516
import {
1617
ApolloInlineGraphqlTraceContext,
1718
ApolloInlineRequestTraceContext,
@@ -129,7 +130,7 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
129130
];
130131

131132
let schemaIdSet$: MaybePromise<void> | undefined;
132-
let schema: { id: string; schema: GraphQLSchema } | undefined;
133+
let currentSchema: { id: string; schema: GraphQLSchema } | undefined;
133134
let yoga: YogaServer<Record<string, unknown>, Record<string, unknown>>;
134135
let reporter: Reporter;
135136

@@ -174,9 +175,9 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
174175

175176
onSchemaChange({ schema }) {
176177
if (schema) {
177-
schemaIdSet$ = hashSHA256(printSchema(schema), yoga.fetchAPI)
178+
schemaIdSet$ = hashSHA256(printSchemaWithDirectives(schema), yoga.fetchAPI)
178179
.then(id => {
179-
schema = { id, schema };
180+
currentSchema = { id, schema };
180181
schemaIdSet$ = undefined;
181182
})
182183
.catch(error => {
@@ -191,7 +192,7 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
191192

192193
onParse() {
193194
return function onParseEnd({ result, context }) {
194-
if (!schema) {
195+
if (!currentSchema) {
195196
throw new Error("should not happen: schema doesn't exists");
196197
}
197198
const ctx = ctxForReq.get(context.request)?.traces.get(context);
@@ -211,11 +212,11 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
211212

212213
ctx.referencedFieldsByType = calculateReferencedFieldsByType({
213214
document: result,
214-
schema: schema.schema,
215+
schema: currentSchema.schema,
215216
resolvedOperationName: operationName ?? null,
216217
});
217218
ctx.operationKey = `# ${operationName || '-'}\n${signature}`;
218-
ctx.schemaId = schema!.id;
219+
ctx.schemaId = currentSchema!.id;
219220
};
220221
},
221222

@@ -249,7 +250,7 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
249250
}
250251

251252
serverContext.waitUntil(
252-
reporter.addTrace(schema!.id, {
253+
reporter.addTrace(currentSchema!.id, {
253254
statsReportKey: trace.operationKey,
254255
trace: trace.trace,
255256
referencedFieldsByType: trace.referencedFieldsByType,

packages/plugins/apollo-usage-report/src/reporter.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ export class Reporter {
3939
this.#yoga = yoga;
4040
this.#options = {
4141
...options,
42-
maxBatchDelay: options.maxBatchDelay ?? 20 * 60 * 1000, // 20min
42+
maxBatchDelay: options.maxBatchDelay ?? 20_000, // 20s
4343
maxBatchUncompressedSize: options.maxBatchUncompressedSize ?? 4 * 1024 * 1024, // 4mb
4444
maxTraceSize: options.maxTraceSize ?? 10 * 1024 * 1024, // 10mb
45-
exportTimeout: options.exportTimeout ?? 30 * 1000, // 60s
45+
exportTimeout: options.exportTimeout ?? 30_000, // 30s
4646
};
4747
this.#reportHeaders = {
4848
graphRef: getGraphRef(options),
@@ -94,33 +94,26 @@ export class Reporter {
9494

9595
if (this.#nextSendAfterDelay != null) {
9696
clearTimeout(this.#nextSendAfterDelay);
97+
this.#nextSendAfterDelay = undefined;
9798
}
9899

99100
delete this.#reportsBySchema[schemaId];
100101
report.endTime = dateToProtoTimestamp(new Date());
101102
report.ensureCountsAreIntegers();
102103

103104
const validationError = Report.verify(report);
104-
if (!validationError) {
105+
if (validationError) {
105106
throw new TypeError(`Invalid report: ${validationError}`);
106107
}
107108

108-
const encodedReport = Report.encode(report).finish();
109-
const compressedReport = new ReadableStream({
110-
start(controller) {
111-
controller.enqueue(encodedReport);
112-
controller.close();
113-
},
114-
}).pipeThrough(new CompressionStream('gzip'));
115-
116109
const { apiKey = getEnvVar('APOLLO_KEY'), endpoint = DEFAULT_REPORTING_ENDPOINT } =
117110
this.#options;
118111

112+
const encodedReport = Report.encode(report).finish();
113+
119114
for (let tries = 0; tries < 5; tries++) {
120115
try {
121-
const abortCtl = new AbortController();
122116
this.#logger?.debug(`Sending report (try ${tries}/5)`);
123-
const timeout = setTimeout(() => abortCtl.abort(), this.#options.exportTimeout);
124117
const response = await fetch(endpoint, {
125118
method: 'POST',
126119
headers: {
@@ -130,10 +123,14 @@ export class Reporter {
130123
'x-api-key': apiKey!,
131124
accept: 'application/json',
132125
},
133-
body: compressedReport,
134-
signal: abortCtl.signal,
126+
body: new ReadableStream({
127+
start(controller) {
128+
controller.enqueue(encodedReport);
129+
controller.close();
130+
},
131+
}).pipeThrough(new CompressionStream('gzip')),
132+
signal: AbortSignal.timeout(this.#options.exportTimeout),
135133
});
136-
clearTimeout(timeout);
137134

138135
const result = await response.text();
139136
if (response.ok) {

pnpm-lock.yaml

Lines changed: 22 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)