@@ -141,14 +141,22 @@ export default class Context {
141141
142142 /**
143143 * Instance of Apollo which cares about the communication with the graphql endpoint.
144+ * @type {Apollo }
144145 */
145146 public apollo ! : Apollo ;
146147
147148 /**
148149 * The graphql schema. Is null until the first request.
150+ * @type {Schema }
149151 */
150152 public schema : Schema | undefined ;
151153
154+ /**
155+ * Tells if the schema is already loaded or the loading is currently processed.
156+ * @type {boolean }
157+ */
158+ private schemaWillBeLoaded : Promise < Schema > | undefined ;
159+
152160 /**
153161 * Defines how to query connections. 'auto' | 'nodes' | 'edges' | 'plain'
154162 */
@@ -205,31 +213,35 @@ export default class Context {
205213 }
206214
207215 public async loadSchema ( ) : Promise < Schema > {
208- if ( ! this . schema ) {
209- this . logger . log ( 'Fetching GraphQL Schema initially ...' ) ;
216+ if ( ! this . schemaWillBeLoaded ) {
217+ this . schemaWillBeLoaded = new Promise ( async ( resolve , reject ) => {
218+ this . logger . log ( 'Fetching GraphQL Schema initially ...' ) ;
219+
220+ if ( this . options . connectionQueryMode ) {
221+ this . connectionQueryMode = this . options . connectionQueryMode ;
222+ } else {
223+ this . connectionQueryMode = 'auto' ;
224+ }
210225
211- if ( this . options . connectionQueryMode ) {
212- this . connectionQueryMode = this . options . connectionQueryMode ;
213- } else {
214- this . connectionQueryMode = 'auto' ;
215- }
226+ // We send a custom header along with the request. This is required for our test suite to mock the schema request.
227+ const context = {
228+ headers : { 'X-GraphQL-Introspection-Query' : 'true' }
229+ } ;
216230
217- // We send a custom header along with the request. This is required for our test suite to mock the schema request.
218- const context = {
219- headers : { 'X-GraphQL-Introspection-Query' : 'true' }
220- } ;
231+ const result = await this . apollo . simpleQuery ( introspectionQuery , { } , true , context ) ;
232+ this . schema = new Schema ( result . data . __schema ) ;
221233
222- const result = await this . apollo . simpleQuery ( introspectionQuery , { } , true , context ) ;
223- this . schema = new Schema ( result . data . __schema ) ;
234+ this . logger . log ( 'GraphQL Schema successful fetched' , result ) ;
224235
225- this . logger . log ( 'GraphQL Schema successful fetched' , result ) ;
236+ this . logger . log ( 'Starting to process the schema ...' ) ;
237+ this . processSchema ( ) ;
238+ this . logger . log ( 'Schema procession done!' ) ;
226239
227- this . logger . log ( 'Starting to process the schema ...' ) ;
228- this . processSchema ( ) ;
229- this . logger . log ( 'Schema procession done!' ) ;
240+ resolve ( this . schema ) ;
241+ } ) ;
230242 }
231243
232- return this . schema ;
244+ return this . schemaWillBeLoaded ;
233245 }
234246
235247 public processSchema ( ) {
0 commit comments