Skip to content

Commit 421822a

Browse files
committed
Allow all objects to be passed as filter
1 parent 8865339 commit 421822a

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

dist/vuex-orm-apollo.esm.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -9318,9 +9318,11 @@ var QueryBuilder = /** @class */ (function () {
93189318
args = args ? JSON.parse(JSON.stringify(args)) : {};
93199319
if (!args)
93209320
throw new Error('args is undefined');
9321-
if (args[model.singularName] && typeof args[model.singularName] === 'object') {
9322-
args[model.singularName] = { __type: upcaseFirstLetter(model.singularName) };
9323-
}
9321+
Object.keys(args).forEach(function (key) {
9322+
if (args && args[key] && typeof args[key] === 'object') {
9323+
args[key] = { __type: upcaseFirstLetter(key) };
9324+
}
9325+
});
93249326
// multiple
93259327
multiple = multiple === undefined ? !args['id'] : multiple;
93269328
// name
@@ -9351,14 +9353,17 @@ var QueryBuilder = /** @class */ (function () {
93519353
if ((!relations.has(key) || relations.get(key) instanceof _this.context.components.BelongsTo) &&
93529354
!key.startsWith('$') && value !== null) {
93539355
if (value instanceof Array) {
9356+
// Iterate over all fields and transform them if value is an array
93549357
var arrayModel_1 = _this.getModel(inflection.singularize(key));
93559358
returnValue[key] = value.map(function (v) { return _this.transformOutgoingData(arrayModel_1 || model, v); });
93569359
}
9357-
else if (relations.get(key) instanceof _this.context.components.BelongsTo) {
9360+
else if (typeof value === 'object' && _this.context.getModel(inflection.singularize(key), true)) {
9361+
// Value is a record, transform that too
93589362
var relatedModel = _this.getModel(inflection.singularize(key));
93599363
returnValue[key] = _this.transformOutgoingData(relatedModel || model, value);
93609364
}
93619365
else {
9366+
// In any other case just let the value be what ever it is
93629367
returnValue[key] = value;
93639368
}
93649369
}
@@ -9939,16 +9944,16 @@ var VuexORMApollo = /** @class */ (function () {
99399944
VuexORMApollo.prototype.fetch = function (_a, params) {
99409945
var state = _a.state, dispatch = _a.dispatch;
99419946
return __awaiter(this, void 0, void 0, function () {
9942-
var filter, bypassCache, multiple, model, name, query, data;
9947+
var model, filter, bypassCache, multiple, name, query, data;
99439948
return __generator(this, function (_b) {
99449949
switch (_b.label) {
99459950
case 0:
9946-
filter = params ? params.filter || {} : {};
9951+
model = this.context.getModel(state.$name);
9952+
filter = params && params.filter ? this.queryBuilder.transformOutgoingData(model, params.filter) : {};
99479953
bypassCache = params && params.bypassCache;
99489954
multiple = !filter['id'];
9949-
model = this.context.getModel(state.$name);
99509955
name = "" + (multiple ? model.pluralName : model.singularName);
9951-
query = this.queryBuilder.buildQuery('query', model, name, filter);
9956+
query = this.queryBuilder.buildQuery('query', model, name, filter, multiple);
99529957
return [4 /*yield*/, this.apolloRequest(model, query, filter, false, bypassCache)];
99539958
case 1:
99549959
data = _b.sent();

src/queryBuilder.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,15 @@ export default class QueryBuilder {
145145
!key.startsWith('$') && value !== null) {
146146

147147
if (value instanceof Array) {
148+
// Iterate over all fields and transform them if value is an array
148149
const arrayModel = this.getModel(inflection.singularize(key));
149150
returnValue[key] = value.map((v) => this.transformOutgoingData(arrayModel || model, v));
150-
} else if (relations.get(key) instanceof this.context.components.BelongsTo) {
151+
} else if (typeof value === 'object' && this.context.getModel(inflection.singularize(key), true)) {
152+
// Value is a record, transform that too
151153
const relatedModel = this.getModel(inflection.singularize(key));
152154
returnValue[key] = this.transformOutgoingData(relatedModel || model, value);
153155
} else {
156+
// In any other case just let the value be what ever it is
154157
returnValue[key] = value;
155158
}
156159
}

src/vuex-orm-apollo.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,16 @@ export default class VuexORMApollo {
8585
* @returns {Promise<void>}
8686
*/
8787
private async fetch ({ state, dispatch }: ActionParams, params?: ActionParams): Promise<void> {
88-
const filter = params ? params.filter || {} : {};
88+
const model: Model = this.context.getModel(state.$name);
89+
90+
// Filter
91+
const filter = params && params.filter ? this.queryBuilder.transformOutgoingData(model, params.filter) : {};
8992
const bypassCache = params && params.bypassCache;
9093

9194
// When the filter contains an id, we query in singular mode
9295
const multiple: boolean = !filter['id'];
93-
const model: Model = this.context.getModel(state.$name);
9496
const name: string = `${multiple ? model.pluralName : model.singularName}`;
95-
const query = this.queryBuilder.buildQuery('query', model, name, filter);
97+
const query = this.queryBuilder.buildQuery('query', model, name, filter, multiple);
9698

9799
// Send the request to the GraphQL API
98100
const data = await this.apolloRequest(model, query, filter, false, bypassCache as boolean);

0 commit comments

Comments
 (0)