Skip to content

Commit 8db1f19

Browse files
committed
Bugfixes
1 parent d216ec5 commit 8db1f19

File tree

4 files changed

+46
-32
lines changed

4 files changed

+46
-32
lines changed

dist/vuex-orm-graphql.esm.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27385,19 +27385,21 @@ var Transformer = /** @class */ (function () {
2738527385
*
2738627386
* @param model
2738727387
* @param {Data} data
27388+
* @param {Array<String>} whitelist of fields
2738827389
* @returns {Data}
2738927390
*/
27390-
Transformer.transformOutgoingData = function (model, data) {
27391+
Transformer.transformOutgoingData = function (model, data, whitelist) {
2739127392
var _this = this;
2739227393
var context = Context.getInstance();
2739327394
var relations = model.getRelations();
2739427395
var returnValue = {};
2739527396
Object.keys(data).forEach(function (key) {
2739627397
var value = data[key];
27397-
// Ignore hasMany/One connections, empty fields and internal fields ($)
27398-
if ((!relations.has(key) || relations.get(key) instanceof context.components.BelongsTo) &&
27399-
!key.startsWith('$') && value !== null) {
27400-
var relatedModel = relations.get(key)
27398+
// Always add fields on the whitelist. Ignore hasMany/One connections, empty fields and internal fields ($)
27399+
if ((whitelist && whitelist.includes(key)) ||
27400+
((!relations.has(key) || relations.get(key) instanceof context.components.BelongsTo) &&
27401+
!key.startsWith('$') && value !== null)) {
27402+
var relatedModel = relations.get(key) && relations.get(key).parent
2740127403
? context.getModel(inflection$1.singularize(relations.get(key).parent.entity), true)
2740227404
: null;
2740327405
if (value instanceof Array) {
@@ -28194,9 +28196,9 @@ var QueryBuilder = /** @class */ (function () {
2819428196
typeOrValue = value.__type + 'Input!';
2819528197
}
2819628198
else if (isArray(value) && field) {
28197-
var arg = field.args.find(function (f) { return f.name === key; });
28199+
var arg = QueryBuilder.findSchemaFieldForArgument(key, field, model, filter);
2819828200
if (!arg)
28199-
throw new Error("A argument is of type array but it's not possible to determine the type");
28201+
throw new Error("The argument " + key + " is of type array but it's not possible to determine the type, because it's not in the field " + field.name);
2820028202
typeOrValue = Schema.getTypeNameOfField(arg) + '!';
2820128203
}
2820228204
else if (schemaField && Schema.getTypeNameOfField(schemaField)) {
@@ -28283,15 +28285,16 @@ var QueryBuilder = /** @class */ (function () {
2828328285
var schemaField;
2828428286
if (field) {
2828528287
schemaField = field.args.find(function (f) { return f.name === name; });
28286-
if (!schemaField) {
28287-
Context.getInstance().logger.warn("Could find the argument with name " + name + " for the mutation/query " + field.name);
28288-
}
28288+
if (schemaField)
28289+
return schemaField;
2828928290
}
28290-
else {
28291-
// We try to find the FilterType or at least the Type this query belongs to.
28292-
var type = schema.getType(model.singularName + (isFilter ? 'Filter' : ''));
28293-
// Next we try to find the field from the type
28294-
schemaField = type ? (isFilter ? type.inputFields : type.fields).find(function (f) { return f.name === name; }) : undefined;
28291+
// We try to find the FilterType or at least the Type this query belongs to.
28292+
var type = schema.getType(model.singularName + (isFilter ? 'Filter' : ''));
28293+
// Next we try to find the field from the type
28294+
schemaField = type ? (isFilter ? type.inputFields : type.fields).find(function (f) { return f.name === name; }) : undefined;
28295+
// Warn before we return null
28296+
if (!schemaField) {
28297+
Context.getInstance().logger.warn("Couldn't find the argument with name " + name + " for the mutation/query " + (field ? field.name : '(?)'));
2829528298
}
2829628299
return schemaField;
2829728300
};
@@ -28749,7 +28752,8 @@ var Fetch = /** @class */ (function (_super) {
2874928752
case 1:
2875028753
_b.sent();
2875128754
model = this.getModelFromState(state);
28752-
filter = params && params.filter ? Transformer.transformOutgoingData(model, params.filter) : {};
28755+
filter = params && params.filter ?
28756+
Transformer.transformOutgoingData(model, params.filter, Object.keys(params.filter)) : {};
2875328757
bypassCache = params && params.bypassCache;
2875428758
multiple = !filter['id'];
2875528759
name = NameGenerator.getNameForFetch(model, multiple);

src/actions/fetch.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export default class Fetch extends Action {
2323
const model = this.getModelFromState(state);
2424

2525
// Filter
26-
const filter = params && params.filter ? Transformer.transformOutgoingData(model, params.filter) : {};
26+
const filter = params && params.filter ?
27+
Transformer.transformOutgoingData(model, params.filter, Object.keys(params.filter)) : {};
28+
2729
const bypassCache = params && params.bypassCache;
2830

2931
// When the filter contains an id, we query in singular mode

src/graphql/query-builder.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ export default class QueryBuilder {
185185
// Case 2 (User!)
186186
typeOrValue = value.__type + 'Input!';
187187
} else if (_.isArray(value) && field) {
188-
const arg = field.args.find(f => f.name === key);
189-
if (!arg) throw new Error("A argument is of type array but it's not possible to determine the type");
188+
const arg = QueryBuilder.findSchemaFieldForArgument(key, field, model, filter);
189+
if (!arg) throw new Error(`The argument ${key} is of type array but it's not possible to determine the type, because it's not in the field ${field.name}`);
190190
typeOrValue = Schema.getTypeNameOfField(arg) + '!';
191191
} else if (schemaField && Schema.getTypeNameOfField(schemaField)) {
192192
// Case 1, 3 and 4
@@ -275,16 +275,18 @@ export default class QueryBuilder {
275275

276276
if (field) {
277277
schemaField = field.args.find(f => f.name === name);
278+
if (schemaField) return schemaField;
279+
}
278280

279-
if (!schemaField) {
280-
Context.getInstance().logger.warn(`Could find the argument with name ${name} for the mutation/query ${field.name}`);
281-
}
282-
} else {
283-
// We try to find the FilterType or at least the Type this query belongs to.
284-
const type = schema.getType(model.singularName + (isFilter ? 'Filter' : ''));
281+
// We try to find the FilterType or at least the Type this query belongs to.
282+
const type = schema.getType(model.singularName + (isFilter ? 'Filter' : ''));
283+
284+
// Next we try to find the field from the type
285+
schemaField = type ? (isFilter ? type.inputFields! : type.fields!).find(f => f.name === name) : undefined;
285286

286-
// Next we try to find the field from the type
287-
schemaField = type ? (isFilter ? type.inputFields! : type.fields!).find(f => f.name === name) : undefined;
287+
// Warn before we return null
288+
if (!schemaField) {
289+
Context.getInstance().logger.warn(`Couldn't find the argument with name ${name} for the mutation/query ${field ? field.name : '(?)'}`);
288290
}
289291

290292
return schemaField;

src/graphql/transformer.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,27 @@ export default class Transformer {
1818
*
1919
* @param model
2020
* @param {Data} data
21+
* @param {Array<String>} whitelist of fields
2122
* @returns {Data}
2223
*/
23-
public static transformOutgoingData (model: Model, data: Data): Data {
24+
public static transformOutgoingData (model: Model, data: Data, whitelist?: Array<String>): Data {
2425
const context = Context.getInstance();
2526
const relations: Map<string, Field> = model.getRelations();
2627
const returnValue: Data = {};
2728

2829
Object.keys(data).forEach((key) => {
2930
const value = data[key];
3031

31-
// Ignore hasMany/One connections, empty fields and internal fields ($)
32-
if ((!relations.has(key) || relations.get(key) instanceof context.components.BelongsTo) &&
33-
!key.startsWith('$') && value !== null) {
32+
// Always add fields on the whitelist. Ignore hasMany/One connections, empty fields and internal fields ($)
33+
if (
34+
(whitelist && whitelist.includes(key)) ||
35+
(
36+
(!relations.has(key) || relations.get(key) instanceof context.components.BelongsTo) &&
37+
!key.startsWith('$') && value !== null
38+
)
39+
) {
3440

35-
let relatedModel = relations.get(key)
41+
let relatedModel = relations.get(key) && relations.get(key)!.parent
3642
? context.getModel(inflection.singularize(relations.get(key)!.parent!.entity), true)
3743
: null;
3844

0 commit comments

Comments
 (0)