@@ -52,6 +52,7 @@ export type DocumentUri = string;
5252import "core-js/fn/array/flat-map" ;
5353import { rangeForASTNode } from "./utilities/source" ;
5454import { formatMS } from "./format" ;
55+ import { LoadingHandler } from "./server" ;
5556declare global {
5657 interface Array < T > {
5758 flatMap < U > (
@@ -114,11 +115,22 @@ export class GraphQLProject {
114115 Map < string , Map < string , number > >
115116 > = new Map ( ) ;
116117
117- constructor ( config : ApolloConfig , public configFile : string ) {
118+ constructor (
119+ config : ApolloConfig ,
120+ public configFile : string ,
121+ private loadingHandler : LoadingHandler
122+ ) {
118123 this . updateConfig ( config ) ;
119124 }
120125
121126 updateConfig ( newConfig : ApolloConfig ) {
127+ if (
128+ this . config &&
129+ JSON . stringify ( this . config ) === JSON . stringify ( newConfig )
130+ ) {
131+ return ;
132+ }
133+
122134 this . needsValidation = true ;
123135 this . config = newConfig ;
124136
@@ -169,85 +181,96 @@ export class GraphQLProject {
169181 }
170182
171183 async loadEngineStats ( ) {
172- await Promise . all (
173- Object . values ( this . config . schemas ! ) . map ( async schemaDef => {
174- if ( schemaDef . engineKey ) {
175- const engineData = await toPromise (
176- execute ( engineLink , {
177- query : engineStatsQuery ,
178- variables : {
179- id : getIdFromKey ( schemaDef . engineKey ! )
180- } ,
181- context : {
182- headers : { [ "x-api-key" ] : schemaDef . engineKey } ,
183- ...( this . config . engineEndpoint && {
184- uri : this . config . engineEndpoint
185- } )
184+ await this . loadingHandler . handle (
185+ `Loading Apollo Engine stats for ${ this . config . name ! } ` ,
186+ Promise . all (
187+ Object . values ( this . config . schemas ! ) . map ( async schemaDef => {
188+ if ( schemaDef . engineKey ) {
189+ const engineData = await toPromise (
190+ execute ( engineLink , {
191+ query : engineStatsQuery ,
192+ variables : {
193+ id : getIdFromKey ( schemaDef . engineKey ! )
194+ } ,
195+ context : {
196+ headers : { [ "x-api-key" ] : schemaDef . engineKey } ,
197+ ...( this . config . engineEndpoint && {
198+ uri : this . config . engineEndpoint
199+ } )
200+ }
201+ } )
202+ ) ;
203+
204+ const schemaEngineStats = new Map < string , Map < string , number > > ( ) ;
205+ engineData . data ! . service . report . usageStats . types . forEach (
206+ ( typ : any ) => {
207+ const fieldsMap = new Map < string , number > ( ) ;
208+ typ . fields . forEach ( ( field : any ) => {
209+ fieldsMap . set (
210+ field . name ,
211+ field . latencyHistogram . serviceTimeP95
212+ ) ;
213+ } ) ;
214+
215+ schemaEngineStats . set ( typ . name , fieldsMap ) ;
186216 }
187- } )
188- ) ;
217+ ) ;
189218
190- const schemaEngineStats = new Map < string , Map < string , number > > ( ) ;
191- engineData . data ! . service . report . usageStats . types . forEach (
192- ( typ : any ) => {
193- const fieldsMap = new Map < string , number > ( ) ;
194- typ . fields . forEach ( ( field : any ) => {
195- fieldsMap . set (
196- field . name ,
197- field . latencyHistogram . serviceTimeP95
198- ) ;
199- } ) ;
200-
201- schemaEngineStats . set ( typ . name , fieldsMap ) ;
202- }
203- ) ;
204-
205- this . engineStats . set ( schemaDef . engineKey , schemaEngineStats ) ;
219+ this . engineStats . set ( schemaDef . engineKey , schemaEngineStats ) ;
206220
207- return ;
208- } else {
209- return ;
210- }
211- } )
221+ return ;
222+ } else {
223+ return ;
224+ }
225+ } )
226+ )
212227 ) ;
213228
214229 return ;
215230 }
216231
217232 async scanAllIncludedFiles ( ) {
218- console . time ( `scanAllIncludedFiles - ${ this . displayName } ` ) ;
219-
220- this . documentSets = await resolveDocumentSets ( this . config , true ) ;
221-
222- for ( const set of this . documentSets ) {
223- if ( ! set . schema ! . getQueryType ( ) ! . astNode ) {
224- const schemaSource = printSchema ( set . schema ! ) ;
225-
226- set . schema = buildSchema (
227- new Source (
228- schemaSource ,
229- `graphql-schema:/schema.graphql?${ encodeURIComponent ( schemaSource ) } `
230- )
231- ) ;
232- }
233+ await this . loadingHandler . handle (
234+ `Loading queries and schemas for ${ this . config . name ! } ` ,
235+ ( async ( ) => {
236+ console . time ( `scanAllIncludedFiles - ${ this . displayName } ` ) ;
237+ this . documentSets = await resolveDocumentSets ( this . config , true ) ;
238+
239+ for ( const set of this . documentSets ) {
240+ if ( ! set . schema ! . getQueryType ( ) ! . astNode ) {
241+ const schemaSource = printSchema ( set . schema ! ) ;
242+
243+ set . schema = buildSchema (
244+ // rebuild the schema from a generated source file and attach the source to a graphql-schema
245+ // URI that can be loaded as an in-memory file by VSCode
246+ new Source (
247+ schemaSource ,
248+ `graphql-schema:/schema.graphql?${ encodeURIComponent (
249+ schemaSource
250+ ) } `
251+ )
252+ ) ;
253+ }
233254
234- this . setToResolved . set ( set . originalSet , set ) ;
255+ this . setToResolved . set ( set . originalSet , set ) ;
235256
236- for ( const filePath of set . documentPaths ) {
237- const uri = Uri . file ( filePath ) . toString ( ) ;
257+ for ( const filePath of set . documentPaths ) {
258+ const uri = Uri . file ( filePath ) . toString ( ) ;
238259
239- // If we already have query documents for this file, that means it was either
240- // opened or changed before we got a chance to read it.
241- if ( this . documentsByFile . has ( uri ) ) continue ;
260+ // If we already have query documents for this file, that means it was either
261+ // opened or changed before we got a chance to read it.
262+ if ( this . documentsByFile . has ( uri ) ) continue ;
242263
243- this . fileDidChange ( uri , set ) ;
244- }
245- }
264+ this . fileDidChange ( uri , set ) ;
265+ }
266+ }
246267
247- console . timeEnd ( `scanAllIncludedFiles - ${ this . displayName } ` ) ;
268+ console . timeEnd ( `scanAllIncludedFiles - ${ this . displayName } ` ) ;
248269
249- this . isReady = true ;
250- this . validateIfNeeded ( ) ;
270+ this . isReady = true ;
271+ this . validateIfNeeded ( ) ;
272+ } ) ( )
273+ ) ;
251274 }
252275
253276 fileDidChange ( uri : DocumentUri , set ?: ResolvedDocumentSet ) {
0 commit comments