Skip to content

Commit 788a742

Browse files
committed
Refactorings and Bugfixes
1 parent 36cadc3 commit 788a742

File tree

6 files changed

+340
-247
lines changed

6 files changed

+340
-247
lines changed

dist/vuex-orm-apollo.esm.js

Lines changed: 82 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8083,6 +8083,16 @@ gql.disableFragmentWarnings = disableFragmentWarnings;
80838083

80848084
var src = gql;
80858085

8086+
/**
8087+
* Capitalizes the first letter of the given string.
8088+
*
8089+
* @param {string} input
8090+
* @returns {string}
8091+
*/
8092+
function capitalizeFirstLetter(input) {
8093+
return input.charAt(0).toUpperCase() + input.slice(1);
8094+
}
8095+
80868096
var inflection$1 = require('inflection');
80878097
/**
80888098
* This class takes care of everything GraphQL query related, especially the generation of queries out of models
@@ -8288,10 +8298,29 @@ var QueryBuilder = /** @class */ (function () {
82888298
* @returns {any}
82898299
*/
82908300
QueryBuilder.prototype.buildQuery = function (modelName, args) {
8301+
// Ignore empty args
8302+
if (args && Object.keys(args).length === 0)
8303+
args = undefined;
82918304
var multiple = !(args && args.get('id'));
82928305
var query = "{ " + this.buildField(modelName, multiple, args) + " }";
82938306
return src(query);
82948307
};
8308+
/**
8309+
* Generates a mutation query for a model.
8310+
*
8311+
* @param {Model} model
8312+
* @param {string}prefix
8313+
* @returns {any}
8314+
*/
8315+
QueryBuilder.prototype.buildMutation = function (model, prefix) {
8316+
if (prefix === void 0) { prefix = 'create'; }
8317+
var name = "" + prefix + capitalizeFirstLetter(model.singularName);
8318+
// Send the request to the GraphQL API
8319+
var signature = this.buildArguments({ contract: { __type: 'Contract' } }, true);
8320+
var field = this.buildField(model, false, { contract: { __type: 'Contract' } }, true, undefined, name);
8321+
var query = "\n mutation " + name + signature + " {\n " + field + "\n }\n ";
8322+
return src(query);
8323+
};
82958324
return QueryBuilder;
82968325
}());
82978326

@@ -8341,16 +8370,6 @@ var Logger = /** @class */ (function () {
83418370
return Logger;
83428371
}());
83438372

8344-
/**
8345-
* Capitalizes the first letter of the given string.
8346-
*
8347-
* @param {string} input
8348-
* @returns {string}
8349-
*/
8350-
function capitalizeFirstLetter(input) {
8351-
return input.charAt(0).toUpperCase() + input.slice(1);
8352-
}
8353-
83548373
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
83558374
return new (P || (P = Promise))(function (resolve, reject) {
83568375
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -8420,17 +8439,6 @@ var VuexORMApollo = /** @class */ (function () {
84208439
});
84218440
this.queryBuilder = new QueryBuilder(this.logger, this.getModel.bind(this));
84228441
}
8423-
/**
8424-
* The install method will be called when the plugin should be installed. We create a new instance of the Plugin class
8425-
* here.
8426-
*
8427-
* @param components
8428-
* @param options
8429-
* @returns {VuexORMApollo}
8430-
*/
8431-
VuexORMApollo.install = function (components, options) {
8432-
return new VuexORMApollo(components, options);
8433-
};
84348442
/**
84358443
* Returns a model by name
84368444
*
@@ -8480,9 +8488,6 @@ var VuexORMApollo = /** @class */ (function () {
84808488
return __generator(this, function (_b) {
84818489
switch (_b.label) {
84828490
case 0:
8483-
// Ignore empty filters
8484-
if (filter && Object.keys(filter).length === 0)
8485-
filter = undefined;
84868491
query = this.queryBuilder.buildQuery(state.$name, filter);
84878492
return [4 /*yield*/, this.apolloRequest(query)];
84888493
case 1:
@@ -8504,41 +8509,60 @@ var VuexORMApollo = /** @class */ (function () {
85048509
*/
85058510
VuexORMApollo.prototype.persist = function (_a, _b) {
85068511
var state = _a.state, dispatch = _a.dispatch;
8507-
var id = _b.id;
8512+
var data = _b.data;
8513+
return __awaiter(this, void 0, void 0, function () {
8514+
return __generator(this, function (_c) {
8515+
return [2 /*return*/, this.mutate('create', data, dispatch, this.getModel(state.$name))];
8516+
});
8517+
});
8518+
};
8519+
/**
8520+
* Will be called, when dispatch('entities/something/push') is called.
8521+
* @param {any} state
8522+
* @param {any} dispatch
8523+
* @param {Data} data
8524+
* @returns {Promise<Data | {}>}
8525+
*/
8526+
VuexORMApollo.prototype.push = function (_a, _b) {
8527+
var state = _a.state, dispatch = _a.dispatch;
8528+
var data = _b.data;
85088529
return __awaiter(this, void 0, void 0, function () {
8509-
var model, name, data, signature, query, response, newData, _c;
8510-
return __generator(this, function (_d) {
8511-
switch (_d.label) {
8530+
return __generator(this, function (_c) {
8531+
return [2 /*return*/, this.mutate('update', data, dispatch, this.getModel(state.$name))];
8532+
});
8533+
});
8534+
};
8535+
/**
8536+
* Contains the logic to save (persist or push) data.
8537+
*
8538+
* @param {string} action
8539+
* @param {Data | undefined} data
8540+
* @param {Function} dispatch
8541+
* @param {Model} model
8542+
* @returns {Promise<any>}
8543+
*/
8544+
VuexORMApollo.prototype.mutate = function (action, data, dispatch, model) {
8545+
return __awaiter(this, void 0, void 0, function () {
8546+
var query, response, newData, _a;
8547+
return __generator(this, function (_b) {
8548+
switch (_b.label) {
85128549
case 0:
8513-
model = this.getModel(state.$name);
8514-
name = "create" + capitalizeFirstLetter(model.singularName);
8515-
data = model.baseModel.getters('find', { id: id })();
8516-
signature = this.queryBuilder.buildArguments({ contract: { __type: 'Contract' } }, true);
8517-
query = "\n mutation " + name + signature + " {\n " + this.queryBuilder.buildField(model, false, { contract: { __type: 'Contract' } }, true, undefined, name) + "\n }\n ";
8518-
this.logger.logQuery(query);
8519-
delete data.id;
8550+
if (!data) return [3 /*break*/, 2];
8551+
query = this.queryBuilder.buildMutation(model, action);
85208552
return [4 /*yield*/, this.apolloClient.mutate({
8521-
'mutation': src(query),
8522-
'variables': (_c = {}, _c[model.singularName] = this.queryBuilder.transformOutgoingData(data), _c)
8553+
'mutation': query,
8554+
'variables': (_a = {}, _a[model.singularName] = this.queryBuilder.transformOutgoingData(data), _a)
85238555
})];
85248556
case 1:
8525-
response = _d.sent();
8557+
response = _b.sent();
85268558
newData = this.queryBuilder.transformIncomingData(response.data);
85278559
this.storeData(newData, dispatch);
85288560
return [2 /*return*/, newData];
8561+
case 2: return [2 /*return*/, {}];
85298562
}
85308563
});
85318564
});
85328565
};
8533-
VuexORMApollo.prototype.push = function (_a, _b) {
8534-
var state = _a.state, dispatch = _a.dispatch;
8535-
var id = _b.id;
8536-
return __awaiter(this, void 0, void 0, function () {
8537-
return __generator(this, function (_c) {
8538-
return [2 /*return*/];
8539-
});
8540-
});
8541-
};
85428566
/**
85438567
* Sends a query to the GraphQL API via apollo
85448568
* @param query
@@ -8566,10 +8590,19 @@ var VuexORMApollo = /** @class */ (function () {
85668590
*/
85678591
VuexORMApollo.prototype.storeData = function (data, dispatch) {
85688592
Object.keys(data).forEach(function (key) {
8569-
dispatch('create', { data: data[key] });
8593+
dispatch('insert', { data: data[key] });
85708594
});
85718595
};
85728596
return VuexORMApollo;
85738597
}());
85748598

8575-
export default VuexORMApollo;
8599+
var VuexORMApolloPlugin = /** @class */ (function () {
8600+
function VuexORMApolloPlugin() {
8601+
}
8602+
VuexORMApolloPlugin.install = function (components, options) {
8603+
return new VuexORMApollo(components, options);
8604+
};
8605+
return VuexORMApolloPlugin;
8606+
}());
8607+
8608+
export default VuexORMApolloPlugin;

0 commit comments

Comments
 (0)