Skip to content

Commit 835e455

Browse files
committed
feat(apollo-usage-report): report referenced feild by types
1 parent 91ceb35 commit 835e455

File tree

3 files changed

+100
-14
lines changed

3 files changed

+100
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
},
4343
"dependencies": {
4444
"@apollo/usage-reporting-protobuf": "^4.1.1",
45+
"@apollo/utils.usagereporting": "^2.1.0",
4546
"@graphql-yoga/plugin-apollo-inline-trace": "workspace:^",
4647
"@whatwg-node/promise-helpers": "^1.2.4",
4748
"tslib": "^2.8.1"

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DocumentNode, getOperationAST, Kind, printSchema, stripIgnoredCharacters } from 'graphql';
1+
import { DocumentNode, getOperationAST, GraphQLSchema, Kind, printSchema } from 'graphql';
22
import {
33
isAsyncIterable,
44
YogaLogger,
@@ -10,6 +10,10 @@ import {
1010
type YogaInitialContext,
1111
} from 'graphql-yoga';
1212
import { Report } from '@apollo/usage-reporting-protobuf';
13+
import {
14+
calculateReferencedFieldsByType,
15+
usageReportingSignature,
16+
} from '@apollo/utils.usagereporting';
1317
import {
1418
ApolloInlineGraphqlTraceContext,
1519
ApolloInlineRequestTraceContext,
@@ -62,6 +66,7 @@ export interface ApolloUsageReportRequestContext extends ApolloInlineRequestTrac
6266
}
6367

6468
export interface ApolloUsageReportGraphqlContext extends ApolloInlineGraphqlTraceContext {
69+
referencedFieldsByType: ReturnType<typeof calculateReferencedFieldsByType>;
6570
operationKey?: string;
6671
schemaId?: string;
6772
}
@@ -82,7 +87,7 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
8287
];
8388

8489
let schemaIdSet$: MaybePromise<void> | undefined;
85-
let schemaId: string;
90+
let schema: { id: string; schema: GraphQLSchema } | undefined;
8691
let yoga: YogaServer<Record<string, unknown>, Record<string, unknown>>;
8792
const logger = Object.fromEntries(
8893
(['error', 'warn', 'info', 'debug'] as const).map(level => [
@@ -126,7 +131,7 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
126131
schemaIdSet$ = handleMaybePromise(
127132
() => hashSHA256(printSchema(schema), yoga.fetchAPI),
128133
id => {
129-
schemaId = id;
134+
schema = { id, schema };
130135
schemaIdSet$ = undefined;
131136
},
132137
);
@@ -139,6 +144,9 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
139144

140145
onParse() {
141146
return function onParseEnd({ result, context }) {
147+
if (!schema) {
148+
throw new Error("should not happen: schema doesn't exists");
149+
}
142150
const ctx = ctxForReq.get(context.request)?.traces.get(context);
143151
if (!ctx) {
144152
logger.debug(
@@ -150,12 +158,17 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
150158
const operationName =
151159
context.params.operationName ??
152160
(isDocumentNode(result) ? getOperationAST(result)?.name?.value : undefined);
153-
const signature = context.params.query
154-
? stripIgnoredCharacters(context.params.query)
155-
: '';
156-
161+
const signature = operationName
162+
? usageReportingSignature(result, operationName)
163+
: (context.params.query ?? '');
164+
165+
ctx.referencedFieldsByType = calculateReferencedFieldsByType({
166+
document: result,
167+
schema: schema.schema,
168+
resolvedOperationName: operationName ?? null,
169+
});
157170
ctx.operationKey = `# ${operationName || '-'}\n${signature}`;
158-
ctx.schemaId = schemaId;
171+
ctx.schemaId = schema!.id;
159172
};
160173
},
161174

@@ -194,7 +207,9 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
194207

195208
tracesPerSchema[trace.schemaId] ||= {};
196209
tracesPerSchema[trace.schemaId]![trace.operationKey] ||= { trace: [] };
197-
tracesPerSchema[trace.schemaId]![trace.operationKey]!.trace?.push(trace.trace);
210+
const stats = tracesPerSchema[trace.schemaId]![trace.operationKey]!;
211+
stats.trace?.push(trace.trace);
212+
stats.referencedFieldsByType = trace.referencedFieldsByType;
198213
}
199214

200215
for (const schemaId in tracesPerSchema) {

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)