Skip to content

Commit 5cc49d4

Browse files
authored
Don't fetch schema multiple times (#52)
1 parent bee9936 commit 5cc49d4

File tree

2 files changed

+61
-42
lines changed

2 files changed

+61
-42
lines changed

dist/vuex-orm-graphql.esm.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27964,32 +27964,39 @@ var Context = /** @class */ (function () {
2796427964
};
2796527965
Context.prototype.loadSchema = function () {
2796627966
return __awaiter$1(this, void 0, void 0, function () {
27967-
var context, result;
27967+
var _this = this;
2796827968
return __generator$1(this, function (_a) {
27969-
switch (_a.label) {
27970-
case 0:
27971-
if (!!this.schema) return [3 /*break*/, 2];
27972-
this.logger.log('Fetching GraphQL Schema initially ...');
27973-
if (this.options.connectionQueryMode) {
27974-
this.connectionQueryMode = this.options.connectionQueryMode;
27975-
}
27976-
else {
27977-
this.connectionQueryMode = 'auto';
27978-
}
27979-
context = {
27980-
headers: { 'X-GraphQL-Introspection-Query': 'true' }
27981-
};
27982-
return [4 /*yield*/, this.apollo.simpleQuery(introspectionQuery, {}, true, context)];
27983-
case 1:
27984-
result = _a.sent();
27985-
this.schema = new Schema(result.data.__schema);
27986-
this.logger.log('GraphQL Schema successful fetched', result);
27987-
this.logger.log('Starting to process the schema ...');
27988-
this.processSchema();
27989-
this.logger.log('Schema procession done!');
27990-
_a.label = 2;
27991-
case 2: return [2 /*return*/, this.schema];
27969+
if (!this.schemaWillBeLoaded) {
27970+
this.schemaWillBeLoaded = new Promise(function (resolve, reject) { return __awaiter$1(_this, void 0, void 0, function () {
27971+
var context, result;
27972+
return __generator$1(this, function (_a) {
27973+
switch (_a.label) {
27974+
case 0:
27975+
this.logger.log('Fetching GraphQL Schema initially ...');
27976+
if (this.options.connectionQueryMode) {
27977+
this.connectionQueryMode = this.options.connectionQueryMode;
27978+
}
27979+
else {
27980+
this.connectionQueryMode = 'auto';
27981+
}
27982+
context = {
27983+
headers: { 'X-GraphQL-Introspection-Query': 'true' }
27984+
};
27985+
return [4 /*yield*/, this.apollo.simpleQuery(introspectionQuery, {}, true, context)];
27986+
case 1:
27987+
result = _a.sent();
27988+
this.schema = new Schema(result.data.__schema);
27989+
this.logger.log('GraphQL Schema successful fetched', result);
27990+
this.logger.log('Starting to process the schema ...');
27991+
this.processSchema();
27992+
this.logger.log('Schema procession done!');
27993+
resolve(this.schema);
27994+
return [2 /*return*/];
27995+
}
27996+
});
27997+
}); });
2799227998
}
27999+
return [2 /*return*/, this.schemaWillBeLoaded];
2799328000
});
2799428001
});
2799528002
};

src/common/context.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)