Skip to content

Commit 609b84f

Browse files
committed
Improvements
1 parent f7d2765 commit 609b84f

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ You can see query examples in the [project wiki](https://github.com/vuex-orm/vue
9797
To test this plugin in your existing project, you can use `yarn link` functionality. Run `yarn link` in your local
9898
vuex-orm-apollo directory and run `yarn link @vuex-orm/plugin-apollo` in your project dir.
9999

100+
Remember to run `yarn build` in your vuex-orm-apollo directory and then again `yarn link` in your project after you have
101+
made changes to the plugin code. You probably have also to restart your webpack server.
102+
100103

101104
```bash
102105
$ yarn run build

dist/vuex-orm-apollo.esm.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7791,21 +7791,22 @@ var QueryBuilder = /** @class */ (function () {
77917791
* @param {Model|string} model The model to use
77927792
* @param {boolean} multiple Determines whether plural/nodes syntax or singular syntax is used.
77937793
* @param {Arguments} args The args that will be passed to the query field ( user(role: $role) )
7794-
* @param {Model} rootModel The model of the root query field. Used to avoid endless recursion
7794+
* @param {Array<Model>} ignoreModels The models in this list are ignored (while traversing relations). Mainly for recursion
77957795
* @param {string} name Optional name of the field. If not provided, this will be the model name
77967796
* @param {boolean} allowIdFields Optional. Determines if id fields will be ignored for the argument generation.
77977797
* See buildArguments
77987798
* @returns {string}
77997799
*
78007800
* @todo Do we need the allowIdFields param?
7801-
* @todo There could be a endless recursion even with rootModel correctly set. We should track an array of models here probably?
78027801
*/
7803-
QueryBuilder.prototype.buildField = function (model, multiple, args, rootModel, name, allowIdFields) {
7802+
QueryBuilder.prototype.buildField = function (model, multiple, args, ignoreModels, name, allowIdFields) {
78047803
if (multiple === void 0) { multiple = true; }
7804+
if (ignoreModels === void 0) { ignoreModels = []; }
78057805
if (allowIdFields === void 0) { allowIdFields = false; }
78067806
model = this.getModel(model);
7807+
ignoreModels.push(model);
78077808
var params = this.buildArguments(args, false, allowIdFields);
7808-
var fields = "\n " + model.getQueryFields().join(' ') + "\n " + this.buildRelationsQuery(model, rootModel) + "\n ";
7809+
var fields = "\n " + model.getQueryFields().join(' ') + "\n " + this.buildRelationsQuery(model, ignoreModels) + "\n ";
78097810
if (multiple) {
78107811
return "\n " + (name ? name : model.pluralName) + params + " {\n nodes {\n " + fields + "\n }\n }\n ";
78117812
}
@@ -7842,7 +7843,7 @@ var QueryBuilder = /** @class */ (function () {
78427843
name = (multiple ? model.pluralName : model.singularName);
78437844
// build query
78447845
var query = type + " " + upcaseFirstLetter(name) + this.buildArguments(args, true) + " {\n" +
7845-
(" " + this.buildField(model, multiple, args, model, name, true) + "\n") +
7846+
(" " + this.buildField(model, multiple, args, [], name, true) + "\n") +
78467847
"}";
78477848
return src(query);
78487849
};
@@ -7979,28 +7980,33 @@ var QueryBuilder = /** @class */ (function () {
79797980
/**
79807981
*
79817982
* @param {Model} model
7982-
* @param {Model} rootModel
7983+
* @param {Array<Model>} ignoreModels The models in this list are ignored (while traversing relations). Mainly for recursion
79837984
* @returns {Array<String>}
79847985
*/
7985-
QueryBuilder.prototype.buildRelationsQuery = function (model, rootModel) {
7986+
QueryBuilder.prototype.buildRelationsQuery = function (model, ignoreModels) {
79867987
var _this = this;
7988+
if (ignoreModels === void 0) { ignoreModels = []; }
79877989
if (model === null)
79887990
return '';
79897991
var relationQueries = [];
79907992
model.getRelations().forEach(function (field, name) {
7991-
if (!rootModel || (name !== rootModel.singularName && name !== rootModel.pluralName)) {
7993+
if (!_this.shouldModelBeIgnored(_this.getModel(name), ignoreModels)) {
79927994
var multiple = field.constructor.name !== 'BelongsTo';
7993-
relationQueries.push(_this.buildField(name, multiple, undefined, rootModel || model));
7995+
relationQueries.push(_this.buildField(name, multiple, undefined, ignoreModels));
79947996
}
79957997
});
79967998
return relationQueries;
79977999
};
8000+
QueryBuilder.prototype.shouldModelBeIgnored = function (model, ignoreModels) {
8001+
return ignoreModels.find(function (m) { return m.singularName === model.singularName; }) !== undefined;
8002+
};
79988003
return QueryBuilder;
79998004
}());
80008005

80018006
var Logger = /** @class */ (function () {
80028007
function Logger(enabled) {
80038008
this.enabled = enabled;
8009+
this.log('Logging is enabled.');
80048010
}
80058011
Logger.prototype.group = function () {
80068012
var messages = [];
@@ -8024,7 +8030,7 @@ var Logger = /** @class */ (function () {
80248030
console.log.apply(console, ['[Vuex-ORM-Apollo]'].concat(messages));
80258031
}
80268032
};
8027-
Logger.prototype.logQuery = function (query) {
8033+
Logger.prototype.logQuery = function (query, variables) {
80288034
if (this.enabled) {
80298035
try {
80308036
this.group('Sending query:');
@@ -8034,6 +8040,8 @@ var Logger = /** @class */ (function () {
80348040
else {
80358041
console.log(QueryBuilder.prettify(query));
80368042
}
8043+
if (variables)
8044+
console.log('VARIABLES:', variables);
80378045
this.groupEnd();
80388046
}
80398047
catch (e) {
@@ -8329,6 +8337,7 @@ var VuexORMApollo = /** @class */ (function () {
83298337
return __generator(this, function (_a) {
83308338
switch (_a.label) {
83318339
case 0:
8340+
this.logger.logQuery(query, variables);
83328341
if (!mutation) return [3 /*break*/, 2];
83338342
return [4 /*yield*/, this.apolloClient.mutate({ mutation: query, variables: variables })];
83348343
case 1:

src/logger.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import QueryBuilder from './queryBuilder';
22
import { DocumentNode } from 'graphql';
3+
import {Arguments} from "./interfaces";
34

45
export default class Logger {
56
private readonly enabled: boolean;
67

78
public constructor (enabled: boolean) {
89
this.enabled = enabled;
10+
this.log('Logging is enabled.');
911
}
1012

1113
public group (...messages: Array<any>): void {
@@ -24,7 +26,7 @@ export default class Logger {
2426
}
2527
}
2628

27-
public logQuery (query: string | DocumentNode) {
29+
public logQuery (query: string | DocumentNode, variables?: Arguments) {
2830
if (this.enabled) {
2931
try {
3032
this.group('Sending query:');
@@ -35,6 +37,8 @@ export default class Logger {
3537
console.log(QueryBuilder.prettify(query as string));
3638
}
3739

40+
if (variables) console.log('VARIABLES:', variables);
41+
3842
this.groupEnd();
3943
} catch (e) {
4044
console.error('[Vuex-ORM-Apollo] There is a syntax error in the query!', e, query);

src/queryBuilder.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,28 @@ export default class QueryBuilder {
5050
* @param {Model|string} model The model to use
5151
* @param {boolean} multiple Determines whether plural/nodes syntax or singular syntax is used.
5252
* @param {Arguments} args The args that will be passed to the query field ( user(role: $role) )
53-
* @param {Model} rootModel The model of the root query field. Used to avoid endless recursion
53+
* @param {Array<Model>} ignoreModels The models in this list are ignored (while traversing relations). Mainly for recursion
5454
* @param {string} name Optional name of the field. If not provided, this will be the model name
5555
* @param {boolean} allowIdFields Optional. Determines if id fields will be ignored for the argument generation.
5656
* See buildArguments
5757
* @returns {string}
5858
*
5959
* @todo Do we need the allowIdFields param?
60-
* @todo There could be a endless recursion even with rootModel correctly set. We should track an array of models here probably?
6160
*/
6261
public buildField (model: Model | string,
6362
multiple: boolean = true,
6463
args?: Arguments,
65-
rootModel?: Model,
64+
ignoreModels: Array<Model> = [],
6665
name?: string,
6766
allowIdFields: boolean = false): string {
6867
model = this.getModel(model);
68+
ignoreModels.push(model);
6969

7070
let params: string = this.buildArguments(args, false, allowIdFields);
7171

7272
const fields = `
7373
${model.getQueryFields().join(' ')}
74-
${this.buildRelationsQuery(model, rootModel)}
74+
${this.buildRelationsQuery(model, ignoreModels)}
7575
`;
7676

7777
if (multiple) {
@@ -123,7 +123,7 @@ export default class QueryBuilder {
123123
// build query
124124
const query: string =
125125
`${type} ${upcaseFirstLetter(name)}${this.buildArguments(args, true)} {\n` +
126-
` ${this.buildField(model, multiple, args, model, name, true)}\n` +
126+
` ${this.buildField(model, multiple, args, [], name, true)}\n` +
127127
`}`;
128128

129129
return gql(query);
@@ -267,21 +267,25 @@ export default class QueryBuilder {
267267
/**
268268
*
269269
* @param {Model} model
270-
* @param {Model} rootModel
270+
* @param {Array<Model>} ignoreModels The models in this list are ignored (while traversing relations). Mainly for recursion
271271
* @returns {Array<String>}
272272
*/
273-
private buildRelationsQuery (model: (null | Model), rootModel?: Model) {
273+
private buildRelationsQuery (model: (null | Model), ignoreModels: Array<Model> = []) {
274274
if (model === null) return '';
275275

276276
const relationQueries: Array<string> = [];
277277

278278
model.getRelations().forEach((field: Field, name: string) => {
279-
if (!rootModel || (name !== rootModel.singularName && name !== rootModel.pluralName)) {
279+
if (!this.shouldModelBeIgnored(this.getModel(name), ignoreModels)) {
280280
const multiple: boolean = field.constructor.name !== 'BelongsTo';
281-
relationQueries.push(this.buildField(name, multiple, undefined, rootModel || model));
281+
relationQueries.push(this.buildField(name, multiple, undefined, ignoreModels));
282282
}
283283
});
284284

285285
return relationQueries;
286286
}
287+
288+
private shouldModelBeIgnored (model: Model, ignoreModels: Array<Model>) {
289+
return ignoreModels.find((m) => m.singularName === model.singularName) !== undefined;
290+
}
287291
}

src/vuex-orm-apollo.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ export default class VuexORMApollo {
232232
private async apolloRequest (query: any, variables?: Arguments, mutation: boolean = false): Promise<Data> {
233233
let response;
234234

235+
this.logger.logQuery(query, variables);
236+
235237
if (mutation) {
236238
response = await this.apolloClient.mutate({ mutation: query, variables });
237239
} else {

0 commit comments

Comments
 (0)