Skip to content

Commit 21811f7

Browse files
committed
Release 0.0.10
1 parent 946c8ad commit 21811f7

File tree

1 file changed

+86
-74
lines changed

1 file changed

+86
-74
lines changed

dist/vuex-orm-apollo.esm.js

Lines changed: 86 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7763,6 +7763,10 @@ gql.disableFragmentWarnings = disableFragmentWarnings;
77637763
var src = gql;
77647764

77657765
var inflection$1 = require('inflection');
7766+
/**
7767+
* Contains all logic to build GraphQL queries and transform variables between the format Vuex-ORM requires and the
7768+
* format of the GraphQL API.
7769+
*/
77667770
var QueryBuilder = /** @class */ (function () {
77677771
/**
77687772
* Constructor.
@@ -7774,7 +7778,7 @@ var QueryBuilder = /** @class */ (function () {
77747778
this.getModel = getModel;
77757779
}
77767780
/**
7777-
* Takes a string with a graphql query and formats it
7781+
* Takes a string with a graphql query and formats it. Useful for debug output and the tests.
77787782
* @param {string} query
77797783
* @returns {string}
77807784
*/
@@ -7784,13 +7788,17 @@ var QueryBuilder = /** @class */ (function () {
77847788
/**
77857789
* Builds a field for the GraphQL query and a specific model
77867790
*
7787-
* @param {Model|string} model
7788-
* @param {boolean} multiple
7789-
* @param {Arguments} args
7790-
* @param {Model} rootModel
7791-
* @param {string} name
7792-
* @param allowIdFields
7791+
* @param {Model|string} model The model to use
7792+
* @param {boolean} multiple Determines whether plural/nodes syntax or singular syntax is used.
7793+
* @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
7795+
* @param {string} name Optional name of the field. If not provided, this will be the model name
7796+
* @param {boolean} allowIdFields Optional. Determines if id fields will be ignored for the argument generation.
7797+
* See buildArguments
77937798
* @returns {string}
7799+
*
7800+
* @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?
77947802
*/
77957803
QueryBuilder.prototype.buildField = function (model, multiple, args, rootModel, name, allowIdFields) {
77967804
if (multiple === void 0) { multiple = true; }
@@ -7805,21 +7813,36 @@ var QueryBuilder = /** @class */ (function () {
78057813
return "\n " + (name ? name : model.singularName) + params + " {\n " + fields + "\n }\n ";
78067814
}
78077815
};
7808-
QueryBuilder.prototype.buildQuery = function (type, name, args, model, fields, multiple) {
7809-
model = model ? this.getModel(model) : null;
7816+
/**
7817+
* Generates a query.
7818+
* Currently only one root field for the query is possible.
7819+
* @param {string} type 'mutation' or 'query'
7820+
* @param {Model | string} model The model this query or mutation affects. This mainly determines the query fields.
7821+
* @param {string} name Optional name of the query/mutation. Will overwrite the name from the model.
7822+
* @param {Arguments} args Arguments for the query
7823+
* @param {boolean} multiple Determines if the root query field is a connection or not (will be passed to buildField)
7824+
* @returns {any}
7825+
*/
7826+
QueryBuilder.prototype.buildQuery = function (type, model, name, args, multiple) {
7827+
// model
7828+
model = this.getModel(model);
7829+
if (!model)
7830+
throw new Error('No model provided to build the query!');
7831+
// args
78107832
args = args ? JSON.parse(JSON.stringify(args)) : {};
78117833
if (!args)
78127834
throw new Error('args is undefined');
7813-
if (model && args[model.singularName] && typeof args[model.singularName] === 'object') {
7835+
if (args[model.singularName] && typeof args[model.singularName] === 'object') {
78147836
args[model.singularName] = { __type: upcaseFirstLetter(model.singularName) };
78157837
}
7838+
// multiple
78167839
multiple = multiple === undefined ? !args['id'] : multiple;
7817-
if (!name && model)
7818-
name = (multiple ? model.pluralName : model.singularName);
7840+
// name
78197841
if (!name)
7820-
throw new Error("Can't determine name for the query! Please provide either name or model");
7842+
name = (multiple ? model.pluralName : model.singularName);
7843+
// build query
78217844
var query = type + " " + upcaseFirstLetter(name) + this.buildArguments(args, true) + " {\n" +
7822-
(" " + (model ? this.buildField(model, multiple, args, model, name, true) : fields) + "\n") +
7845+
(" " + this.buildField(model, multiple, args, model, name, true) + "\n") +
78237846
"}";
78247847
return src(query);
78257848
};
@@ -7905,7 +7928,7 @@ var QueryBuilder = /** @class */ (function () {
79057928
* 2) Signatures with object types (signature = true, args = { user: { __type: 'User' }})
79067929
* mutation createUser($user: UserInput!)
79077930
*
7908-
* 3) Fields with variables (signature = false, valuesAsVariables = true)
7931+
* 3) Fields with variables (signature = false)
79097932
* query user(id: $id)
79107933
*
79117934
* @param {Arguments | undefined} args
@@ -8081,7 +8104,7 @@ var VuexORMApollo = /** @class */ (function () {
80818104
this.collectModels();
80828105
this.setupMethods();
80838106
this.httpLink = new HttpLink({
8084-
uri: '/graphql'
8107+
uri: options.url ? options.url : '/graphql'
80858108
});
80868109
this.apolloClient = new ApolloClient({
80878110
link: this.httpLink,
@@ -8115,7 +8138,7 @@ var VuexORMApollo = /** @class */ (function () {
81158138
});
81168139
};
81178140
/**
8118-
* This method will setup following Vuex action: fetch, persist, push, destroy
8141+
* This method will setup following Vuex action: fetch, persist, push, destroy, mutate
81198142
*/
81208143
VuexORMApollo.prototype.setupMethods = function () {
81218144
this.components.subActions.fetch = this.fetch.bind(this);
@@ -8128,9 +8151,9 @@ var VuexORMApollo = /** @class */ (function () {
81288151
/**
81298152
* Will be called, when dispatch('entities/something/fetch') is called.
81308153
*
8131-
* @param {Arguments} args
8132-
* @param {any} state
8133-
* @param {any} dispatch
8154+
* @param {any} state The Vuex State from Vuex-ORM
8155+
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
8156+
* @param {ActionParams} filter Filter params for the query
81348157
* @returns {Promise<void>}
81358158
*/
81368159
VuexORMApollo.prototype.fetch = function (_a, filter) {
@@ -8143,7 +8166,7 @@ var VuexORMApollo = /** @class */ (function () {
81438166
multiple = !(filter && filter['id']);
81448167
model = this.getModel(state.$name);
81458168
name = "" + (multiple ? model.pluralName : model.singularName);
8146-
query = this.queryBuilder.buildQuery('query', name, filter, model.singularName);
8169+
query = this.queryBuilder.buildQuery('query', model, name, filter);
81478170
return [4 /*yield*/, this.apolloRequest(query, filter)];
81488171
case 1:
81498172
data = _b.sent();
@@ -8160,9 +8183,9 @@ var VuexORMApollo = /** @class */ (function () {
81608183
/**
81618184
* Will be called, when dispatch('entities/something/persist') is called.
81628185
*
8163-
* @param {any} state
8164-
* @param {any} dispatch
8165-
* @param {any} id
8186+
* @param {any} state The Vuex State from Vuex-ORM
8187+
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
8188+
* @param {string} id ID of the record to persist
81668189
* @returns {Promise<void>}
81678190
*/
81688191
VuexORMApollo.prototype.persist = function (_a, _b) {
@@ -8191,36 +8214,28 @@ var VuexORMApollo = /** @class */ (function () {
81918214
/**
81928215
* Will be called, when dispatch('entities/something/mutate') is called.
81938216
* For custom mutations.
8194-
*
8195-
* @param {any} state
8196-
* @param {any} dispatch
8197-
* @param {Data} data
8198-
* @returns {Promise<Data | {}>}
8217+
* @param {any} state The Vuex State from Vuex-ORM
8218+
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
8219+
* @param {Arguments} args Arguments for the mutation. Must contain a 'mutation' field.
8220+
* @returns {Promise<any>}
81998221
*/
82008222
VuexORMApollo.prototype.customMutation = function (_a, args) {
82018223
var state = _a.state, dispatch = _a.dispatch;
82028224
return __awaiter(this, void 0, void 0, function () {
82038225
var name, model;
82048226
return __generator(this, function (_b) {
8205-
switch (_b.label) {
8206-
case 0:
8207-
name = args['mutation'];
8208-
delete args['mutation'];
8209-
model = this.getModel(state.$name);
8210-
return [4 /*yield*/, this.mutate(name, args, dispatch, model)];
8211-
case 1:
8212-
_b.sent();
8213-
// TODO What would make sense here?
8214-
return [2 /*return*/, true];
8215-
}
8227+
name = args['mutation'];
8228+
delete args['mutation'];
8229+
model = this.getModel(state.$name);
8230+
return [2 /*return*/, this.mutate(name, args, dispatch, model)];
82168231
});
82178232
});
82188233
};
82198234
/**
82208235
* Will be called, when dispatch('entities/something/push') is called.
8221-
* @param {any} state
8222-
* @param {any} dispatch
8223-
* @param {Data} data
8236+
* @param {any} state The Vuex State from Vuex-ORM
8237+
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
8238+
* @param {Arguments} data New data to save
82248239
* @returns {Promise<Data | {}>}
82258240
*/
82268241
VuexORMApollo.prototype.push = function (_a, _b) {
@@ -8250,9 +8265,9 @@ var VuexORMApollo = /** @class */ (function () {
82508265
/**
82518266
* Will be called, when dispatch('entities/something/destroy') is called.
82528267
*
8253-
* @param {any} state
8254-
* @param {any} dispatch
8255-
* @param {Data} id
8268+
* @param {any} state The Vuex State from Vuex-ORM
8269+
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
8270+
* @param {string} id ID of the record to delete
82568271
* @returns {Promise<void>}
82578272
*/
82588273
VuexORMApollo.prototype.destroy = function (_a, _b) {
@@ -8261,52 +8276,50 @@ var VuexORMApollo = /** @class */ (function () {
82618276
return __awaiter(this, void 0, void 0, function () {
82628277
var model, mutationName;
82638278
return __generator(this, function (_c) {
8264-
switch (_c.label) {
8265-
case 0:
8266-
if (!id) return [3 /*break*/, 2];
8267-
model = this.getModel(state.$name);
8268-
mutationName = "delete" + upcaseFirstLetter(model.singularName);
8269-
return [4 /*yield*/, this.mutate(mutationName, { id: id }, dispatch, model, false)];
8270-
case 1:
8271-
_c.sent();
8272-
// TODO what would make sense here?
8273-
return [2 /*return*/, true];
8274-
case 2: return [2 /*return*/];
8279+
if (id) {
8280+
model = this.getModel(state.$name);
8281+
mutationName = "delete" + upcaseFirstLetter(model.singularName);
8282+
return [2 /*return*/, this.mutate(mutationName, { id: id }, dispatch, model, false)];
82758283
}
8284+
return [2 /*return*/];
82768285
});
82778286
});
82788287
};
82798288
/**
8280-
* Contains the logic to save (persist or push) data.
8289+
* Sends a mutation.
82818290
*
8282-
* @param {string} action
8283-
* @param {Data | undefined} data
8284-
* @param {Function} dispatch
8285-
* @param {Model} model
8291+
* @param {string} name Name of the mutation like 'createUser'
8292+
* @param {Data | undefined} variables Variables to send with the mutation
8293+
* @param {Function} dispatch Vuex Dispatch method for the model
8294+
* @param {Model} model The model this mutation affects.
8295+
* @param {boolean} multiple See QueryBuilder.buildQuery()
82868296
* @returns {Promise<any>}
82878297
*/
8288-
VuexORMApollo.prototype.mutate = function (action, variables, dispatch, model, multiple) {
8298+
VuexORMApollo.prototype.mutate = function (name, variables, dispatch, model, multiple) {
82898299
return __awaiter(this, void 0, void 0, function () {
82908300
var id, query, newData;
82918301
return __generator(this, function (_a) {
82928302
switch (_a.label) {
82938303
case 0:
82948304
if (!variables) return [3 /*break*/, 2];
82958305
id = variables.id ? variables.id : undefined;
8296-
query = this.queryBuilder.buildQuery('mutation', action, variables, model, undefined, multiple);
8306+
query = this.queryBuilder.buildQuery('mutation', model, name, variables, multiple);
82978307
return [4 /*yield*/, this.apolloRequest(query, variables, true)];
82988308
case 1:
82998309
newData = _a.sent();
8300-
// TODO: What if there is no id?
8301-
return [2 /*return*/, this.updateData(newData, dispatch, id)];
8310+
if (id)
8311+
return [2 /*return*/, this.updateData(newData, dispatch, id)];
8312+
return [2 /*return*/, null];
83028313
case 2: return [2 /*return*/];
83038314
}
83048315
});
83058316
});
83068317
};
83078318
/**
83088319
* Sends a request to the GraphQL API via apollo
8309-
* @param query
8320+
* @param {any} query The query to send (result from gql())
8321+
* @param {Arguments} variables Optional. The variables to send with the query
8322+
* @param {boolean} mutation Optional. If this is a mutation (true) or a query (false, default)
83108323
* @returns {Promise<Data>}
83118324
*/
83128325
VuexORMApollo.prototype.apolloRequest = function (query, variables, mutation) {
@@ -8317,11 +8330,11 @@ var VuexORMApollo = /** @class */ (function () {
83178330
switch (_a.label) {
83188331
case 0:
83198332
if (!mutation) return [3 /*break*/, 2];
8320-
return [4 /*yield*/, (this.apolloClient).mutate({ mutation: query, variables: variables })];
8333+
return [4 /*yield*/, this.apolloClient.mutate({ mutation: query, variables: variables })];
83218334
case 1:
83228335
response = _a.sent();
83238336
return [3 /*break*/, 4];
8324-
case 2: return [4 /*yield*/, (this.apolloClient).query({ query: query, variables: variables })];
8337+
case 2: return [4 /*yield*/, this.apolloClient.query({ query: query, variables: variables })];
83258338
case 3:
83268339
response = _a.sent();
83278340
_a.label = 4;
@@ -8335,9 +8348,8 @@ var VuexORMApollo = /** @class */ (function () {
83358348
/**
83368349
* Inserts incoming data into the store.
83378350
*
8338-
* @param {Data} data
8339-
* @param {Function} dispatch
8340-
* @param {boolean} update
8351+
* @param {Data} data New data to insert/update
8352+
* @param {Function} dispatch Vuex Dispatch method for the model
83418353
*/
83428354
VuexORMApollo.prototype.insertData = function (data, dispatch) {
83438355
return __awaiter(this, void 0, void 0, function () {
@@ -8361,8 +8373,8 @@ var VuexORMApollo = /** @class */ (function () {
83618373
* Updates an existing record in the store with new data. This method can only update one single record, so
83628374
* it takes the first record of the first field from the data object!
83638375
* @param {Data} data
8364-
* @param {Function} dispatch
8365-
* @param id
8376+
* @param {Function} dispatch Vuex Dispatch method for the model
8377+
* @param {string|number} id ID of the record to update
83668378
*/
83678379
VuexORMApollo.prototype.updateData = function (data, dispatch, id) {
83688380
return __awaiter(this, void 0, void 0, function () {

0 commit comments

Comments
 (0)