Skip to content

Commit bc05700

Browse files
committed
allow additional args
1 parent 0410124 commit bc05700

File tree

3 files changed

+88
-49
lines changed

3 files changed

+88
-49
lines changed

dist/vuex-orm-apollo.esm.js

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,9 @@ function isListValue(value) {
803803
function isEnumValue(value) {
804804
return value.kind === 'EnumValue';
805805
}
806+
function isNullValue(value) {
807+
return value.kind === 'NullValue';
808+
}
806809
function valueToObjectRepresentation(argObj, name, value, variables) {
807810
if (isIntValue(value) || isFloatValue(value)) {
808811
argObj[name.value] = Number(value.value);
@@ -831,6 +834,9 @@ function valueToObjectRepresentation(argObj, name, value, variables) {
831834
else if (isEnumValue(value)) {
832835
argObj[name.value] = value.value;
833836
}
837+
else if (isNullValue(value)) {
838+
argObj[name.value] = null;
839+
}
834840
else {
835841
throw new Error("The inline argument \"" + name.value + "\" of kind \"" + value.kind + "\" is not supported.\n Use variables instead of inline arguments to overcome this limitation.");
836842
}
@@ -859,6 +865,14 @@ function storeKeyNameFromField(field, variables) {
859865
}
860866
return getStoreKeyName(field.name.value, argObj, directivesObj);
861867
}
868+
var KNOWN_DIRECTIVES = [
869+
'connection',
870+
'include',
871+
'skip',
872+
'client',
873+
'rest',
874+
'export',
875+
];
862876
function getStoreKeyName(fieldName, args, directives) {
863877
if (directives &&
864878
directives['connection'] &&
@@ -880,11 +894,24 @@ function getStoreKeyName(fieldName, args, directives) {
880894
return directives['connection']['key'];
881895
}
882896
}
897+
var completeFieldName = fieldName;
883898
if (args) {
884899
var stringifiedArgs = JSON.stringify(args);
885-
return fieldName + "(" + stringifiedArgs + ")";
900+
completeFieldName += "(" + stringifiedArgs + ")";
901+
}
902+
if (directives) {
903+
Object.keys(directives).forEach(function (key) {
904+
if (KNOWN_DIRECTIVES.indexOf(key) !== -1)
905+
return;
906+
if (directives[key] && Object.keys(directives[key]).length) {
907+
completeFieldName += "@" + key + "(" + JSON.stringify(directives[key]) + ")";
908+
}
909+
else {
910+
completeFieldName += "@" + key;
911+
}
912+
});
886913
}
887-
return fieldName;
914+
return completeFieldName;
888915
}
889916
function argumentsObjectFromField(field, variables) {
890917
if (field.arguments && field.arguments.length) {
@@ -956,7 +983,7 @@ function shouldInclude(selection, variables) {
956983
var evaledValue = false;
957984
if (!ifValue || ifValue.kind !== 'BooleanValue') {
958985
if (ifValue.kind !== 'Variable') {
959-
throw new Error("Argument for the @" + directiveName + " directive must be a variable or a bool ean value.");
986+
throw new Error("Argument for the @" + directiveName + " directive must be a variable or a boolean value.");
960987
}
961988
else {
962989
evaledValue = variables[ifValue.name.value];
@@ -1203,6 +1230,24 @@ var TYPENAME_FIELD = {
12031230
value: '__typename',
12041231
},
12051232
};
1233+
function isNotEmpty(op, fragments) {
1234+
return (op.selectionSet.selections.filter(function (selectionSet) {
1235+
return !(selectionSet &&
1236+
selectionSet.kind === 'FragmentSpread' &&
1237+
!isNotEmpty(fragments[selectionSet.name.value], fragments));
1238+
}).length > 0);
1239+
}
1240+
function getDirectiveMatcher(directives) {
1241+
return function directiveMatcher(directive) {
1242+
return directives.some(function (dir) {
1243+
if (dir.name && dir.name === directive.name.value)
1244+
return true;
1245+
if (dir.test && dir.test(directive))
1246+
return true;
1247+
return false;
1248+
});
1249+
};
1250+
}
12061251
function addTypenameToSelectionSet(selectionSet, isRoot) {
12071252
if (isRoot === void 0) { isRoot = false; }
12081253
if (selectionSet.selections) {
@@ -1240,15 +1285,10 @@ function removeDirectivesFromSelectionSet(directives, selectionSet) {
12401285
!selection ||
12411286
!selection.directives)
12421287
return selection;
1288+
var directiveMatcher = getDirectiveMatcher(directives);
12431289
var remove;
12441290
selection.directives = selection.directives.filter(function (directive) {
1245-
var shouldKeep = !directives.some(function (dir) {
1246-
if (dir.name && dir.name === directive.name.value)
1247-
return true;
1248-
if (dir.test && dir.test(directive))
1249-
return true;
1250-
return false;
1251-
});
1291+
var shouldKeep = !directiveMatcher(directive);
12521292
if (!remove && !shouldKeep && agressiveRemove)
12531293
remove = true;
12541294
return shouldKeep;
@@ -1271,14 +1311,7 @@ function removeDirectivesFromDocument(directives, doc) {
12711311
});
12721312
var operation = getOperationDefinitionOrDie(docClone);
12731313
var fragments = createFragmentMap(getFragmentDefinitions(docClone));
1274-
var isNotEmpty = function (op) {
1275-
return op.selectionSet.selections.filter(function (selectionSet) {
1276-
return !(selectionSet &&
1277-
selectionSet.kind === 'FragmentSpread' &&
1278-
!isNotEmpty(fragments[selectionSet.name.value]));
1279-
}).length > 0;
1280-
};
1281-
return isNotEmpty(operation) ? docClone : null;
1314+
return isNotEmpty(operation, fragments) ? docClone : null;
12821315
}
12831316
var added$1 = new Map();
12841317
function addTypenameToDocument(doc) {
@@ -8223,20 +8256,21 @@ var VuexORMApollo = /** @class */ (function () {
82238256
*/
82248257
VuexORMApollo.prototype.persist = function (_a, _b) {
82258258
var state = _a.state, dispatch = _a.dispatch;
8226-
var id = _b.id;
8259+
var id = _b.id, args = _b.args;
82278260
return __awaiter(this, void 0, void 0, function () {
8228-
var model, data, variables, mutationName, _c;
8229-
return __generator(this, function (_d) {
8230-
switch (_d.label) {
8261+
var model, data, mutationName;
8262+
return __generator(this, function (_c) {
8263+
switch (_c.label) {
82318264
case 0:
82328265
if (!id) return [3 /*break*/, 2];
82338266
model = this.context.getModel(state.$name);
82348267
data = model.baseModel.getters('find')(id);
8235-
variables = (_c = {}, _c[model.singularName] = this.queryBuilder.transformOutgoingData(data), _c);
8268+
args = args || {};
8269+
args[model.singularName] = this.queryBuilder.transformOutgoingData(data);
82368270
mutationName = "create" + upcaseFirstLetter(model.singularName);
8237-
return [4 /*yield*/, this.mutate(mutationName, variables, dispatch, model, false)];
8271+
return [4 /*yield*/, this.mutate(mutationName, args, dispatch, model, false)];
82388272
case 1:
8239-
_d.sent();
8273+
_c.sent();
82408274
// TODO is this really necessary?
82418275
return [2 /*return*/, model.baseModel.getters('find')(id)];
82428276
case 2: return [2 /*return*/];
@@ -8284,21 +8318,21 @@ var VuexORMApollo = /** @class */ (function () {
82848318
*/
82858319
VuexORMApollo.prototype.push = function (_a, _b) {
82868320
var state = _a.state, dispatch = _a.dispatch;
8287-
var data = _b.data;
8321+
var data = _b.data, args = _b.args;
82888322
return __awaiter(this, void 0, void 0, function () {
8289-
var model, variables, mutationName, _c;
8290-
return __generator(this, function (_d) {
8291-
switch (_d.label) {
8323+
var model, mutationName;
8324+
return __generator(this, function (_c) {
8325+
switch (_c.label) {
82928326
case 0:
82938327
if (!data) return [3 /*break*/, 2];
82948328
model = this.context.getModel(state.$name);
8295-
variables = (_c = {
8296-
id: data.id
8297-
}, _c[model.singularName] = this.queryBuilder.transformOutgoingData(data), _c);
8329+
args = args || {};
8330+
args['id'] = data.id;
8331+
args[model.singularName] = this.queryBuilder.transformOutgoingData(data);
82988332
mutationName = "update" + upcaseFirstLetter(model.singularName);
8299-
return [4 /*yield*/, this.mutate(mutationName, variables, dispatch, model, false)];
8333+
return [4 /*yield*/, this.mutate(mutationName, args, dispatch, model, false)];
83008334
case 1:
8301-
_d.sent();
8335+
_c.sent();
83028336
// TODO is this really necessary?
83038337
return [2 /*return*/, model.baseModel.getters('find')(data.id)];
83048338
case 2: return [2 /*return*/];
@@ -8316,14 +8350,16 @@ var VuexORMApollo = /** @class */ (function () {
83168350
*/
83178351
VuexORMApollo.prototype.destroy = function (_a, _b) {
83188352
var state = _a.state, dispatch = _a.dispatch;
8319-
var id = _b.id;
8353+
var id = _b.id, args = _b.args;
83208354
return __awaiter(this, void 0, void 0, function () {
83218355
var model, mutationName;
83228356
return __generator(this, function (_c) {
83238357
if (id) {
83248358
model = this.context.getModel(state.$name);
83258359
mutationName = "delete" + upcaseFirstLetter(model.singularName);
8326-
return [2 /*return*/, this.mutate(mutationName, { id: id }, dispatch, model, false)];
8360+
args = args || {};
8361+
args['id'] = id;
8362+
return [2 /*return*/, this.mutate(mutationName, args, dispatch, model, false)];
83278363
}
83288364
return [2 /*return*/];
83298365
});

src/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface ActionParams {
1212
filter?: Filter;
1313
id?: number;
1414
data?: Data;
15+
args?: Arguments;
1516
bypassCache?: boolean;
1617
}
1718

src/vuex-orm-apollo.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,16 @@ export default class VuexORMApollo {
107107
* @param {string} id ID of the record to persist
108108
* @returns {Promise<void>}
109109
*/
110-
private async persist ({ state, dispatch }: ActionParams, { id }: ActionParams): Promise<any> {
110+
private async persist ({ state, dispatch }: ActionParams, { id, args }: ActionParams): Promise<any> {
111111
if (id) {
112112
const model = this.context.getModel(state.$name);
113113
const data = model.baseModel.getters('find')(id);
114114

115-
const variables: Data = {
116-
[model.singularName]: this.queryBuilder.transformOutgoingData(data)
117-
};
115+
args = args || {};
116+
args[model.singularName] = this.queryBuilder.transformOutgoingData(data);
118117

119118
const mutationName = `create${upcaseFirstLetter(model.singularName)}`;
120-
await this.mutate(mutationName, variables, dispatch, model, false);
119+
await this.mutate(mutationName, args, dispatch, model, false);
121120

122121
// TODO is this really necessary?
123122
return model.baseModel.getters('find')(id);
@@ -160,17 +159,16 @@ export default class VuexORMApollo {
160159
* @param {Arguments} data New data to save
161160
* @returns {Promise<Data | {}>}
162161
*/
163-
private async push ({ state, dispatch }: ActionParams, { data }: ActionParams) {
162+
private async push ({ state, dispatch }: ActionParams, { data, args }: ActionParams) {
164163
if (data) {
165164
const model = this.context.getModel(state.$name);
166165

167-
const variables: Data = {
168-
id: data.id,
169-
[model.singularName]: this.queryBuilder.transformOutgoingData(data)
170-
};
166+
args = args || {};
167+
args['id'] = data.id;
168+
args[model.singularName] = this.queryBuilder.transformOutgoingData(data);
171169

172170
const mutationName = `update${upcaseFirstLetter(model.singularName)}`;
173-
await this.mutate(mutationName, variables, dispatch, model, false);
171+
await this.mutate(mutationName, args, dispatch, model, false);
174172

175173
// TODO is this really necessary?
176174
return model.baseModel.getters('find')(data.id);
@@ -185,11 +183,15 @@ export default class VuexORMApollo {
185183
* @param {string} id ID of the record to delete
186184
* @returns {Promise<void>}
187185
*/
188-
private async destroy ({ state, dispatch }: ActionParams, { id }: ActionParams): Promise<any> {
186+
private async destroy ({ state, dispatch }: ActionParams, { id, args }: ActionParams): Promise<any> {
189187
if (id) {
190188
const model = this.context.getModel(state.$name);
191189
const mutationName = `delete${upcaseFirstLetter(model.singularName)}`;
192-
return this.mutate(mutationName, { id }, dispatch, model, false);
190+
191+
args = args || {};
192+
args['id'] = id;
193+
194+
return this.mutate(mutationName, args, dispatch, model, false);
193195
}
194196
}
195197

0 commit comments

Comments
 (0)