1
- import { DocumentNode , getOperationAST , Kind , printSchema , stripIgnoredCharacters } from 'graphql' ;
1
+ import { DocumentNode , getOperationAST , GraphQLSchema , Kind , printSchema } from 'graphql' ;
2
2
import {
3
3
isAsyncIterable ,
4
4
YogaLogger ,
@@ -10,6 +10,10 @@ import {
10
10
type YogaInitialContext ,
11
11
} from 'graphql-yoga' ;
12
12
import { Report } from '@apollo/usage-reporting-protobuf' ;
13
+ import {
14
+ calculateReferencedFieldsByType ,
15
+ usageReportingSignature ,
16
+ } from '@apollo/utils.usagereporting' ;
13
17
import {
14
18
ApolloInlineGraphqlTraceContext ,
15
19
ApolloInlineRequestTraceContext ,
@@ -62,6 +66,7 @@ export interface ApolloUsageReportRequestContext extends ApolloInlineRequestTrac
62
66
}
63
67
64
68
export interface ApolloUsageReportGraphqlContext extends ApolloInlineGraphqlTraceContext {
69
+ referencedFieldsByType : ReturnType < typeof calculateReferencedFieldsByType > ;
65
70
operationKey ?: string ;
66
71
schemaId ?: string ;
67
72
}
@@ -82,7 +87,7 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
82
87
] ;
83
88
84
89
let schemaIdSet$ : MaybePromise < void > | undefined ;
85
- let schemaId : string ;
90
+ let schema : { id : string ; schema : GraphQLSchema } | undefined ;
86
91
let yoga : YogaServer < Record < string , unknown > , Record < string , unknown > > ;
87
92
const logger = Object . fromEntries (
88
93
( [ 'error' , 'warn' , 'info' , 'debug' ] as const ) . map ( level => [
@@ -126,7 +131,7 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
126
131
schemaIdSet$ = handleMaybePromise (
127
132
( ) => hashSHA256 ( printSchema ( schema ) , yoga . fetchAPI ) ,
128
133
id => {
129
- schemaId = id ;
134
+ schema = { id , schema } ;
130
135
schemaIdSet$ = undefined ;
131
136
} ,
132
137
) ;
@@ -139,6 +144,9 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
139
144
140
145
onParse ( ) {
141
146
return function onParseEnd ( { result, context } ) {
147
+ if ( ! schema ) {
148
+ throw new Error ( "should not happen: schema doesn't exists" ) ;
149
+ }
142
150
const ctx = ctxForReq . get ( context . request ) ?. traces . get ( context ) ;
143
151
if ( ! ctx ) {
144
152
logger . debug (
@@ -150,12 +158,17 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
150
158
const operationName =
151
159
context . params . operationName ??
152
160
( 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
+ } ) ;
157
170
ctx . operationKey = `# ${ operationName || '-' } \n${ signature } ` ;
158
- ctx . schemaId = schemaId ;
171
+ ctx . schemaId = schema ! . id ;
159
172
} ;
160
173
} ,
161
174
@@ -194,7 +207,9 @@ export function useApolloUsageReport(options: ApolloUsageReportOptions = {}): Pl
194
207
195
208
tracesPerSchema [ trace . schemaId ] ||= { } ;
196
209
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 ;
198
213
}
199
214
200
215
for ( const schemaId in tracesPerSchema ) {
0 commit comments