@@ -83,7 +83,7 @@ export default class VuexORMApollo {
83
83
}
84
84
85
85
/**
86
- * This method will setup following Vuex action: fetch, persist, push, destroy
86
+ * This method will setup following Vuex action: fetch, persist, push, destroy, mutate
87
87
*/
88
88
private setupMethods ( ) {
89
89
this . components . subActions . fetch = this . fetch . bind ( this ) ;
@@ -98,12 +98,12 @@ export default class VuexORMApollo {
98
98
/**
99
99
* Will be called, when dispatch('entities/something/fetch') is called.
100
100
*
101
- * @param {Arguments } args
102
- * @param {any } state
103
- * @param {any } dispatch
101
+ * @param {any } state The Vuex State from Vuex-ORM
102
+ * @param {DispatchFunction } dispatch Vuex Dispatch method for the model
103
+ * @param {ActionParams } filter Filter params for the query
104
104
* @returns {Promise<void> }
105
105
*/
106
- private async fetch ( { state, dispatch } : ActionParams , filter : ActionParams ) {
106
+ private async fetch ( { state, dispatch } : ActionParams , filter : ActionParams ) : Promise < void > {
107
107
// When the filter contains an id, we query in singular mode
108
108
const multiple : boolean = ! ( filter && filter [ 'id' ] ) ;
109
109
const model : Model = this . getModel ( state . $name ) ;
@@ -120,12 +120,12 @@ export default class VuexORMApollo {
120
120
/**
121
121
* Will be called, when dispatch('entities/something/persist') is called.
122
122
*
123
- * @param {any } state
124
- * @param {any } dispatch
125
- * @param {any } id
123
+ * @param {any } state The Vuex State from Vuex-ORM
124
+ * @param {DispatchFunction } dispatch Vuex Dispatch method for the model
125
+ * @param {string } id ID of the record to persist
126
126
* @returns {Promise<void> }
127
127
*/
128
- private async persist ( { state, dispatch } : ActionParams , { id } : ActionParams ) {
128
+ private async persist ( { state, dispatch } : ActionParams , { id } : ActionParams ) : Promise < any > {
129
129
if ( id ) {
130
130
const model = this . getModel ( state . $name ) ;
131
131
const data = model . baseModel . getters ( 'find' ) ( id ) ;
@@ -145,26 +145,25 @@ export default class VuexORMApollo {
145
145
/**
146
146
* Will be called, when dispatch('entities/something/mutate') is called.
147
147
* For custom mutations.
148
- *
149
- * @param {any } state
150
- * @param {any } dispatch
151
- * @param {Data } data
152
- * @returns {Promise<Data | {}> }
148
+ * @param {any } state The Vuex State from Vuex-ORM
149
+ * @param {DispatchFunction } dispatch Vuex Dispatch method for the model
150
+ * @param {Arguments } args Arguments for the mutation. Must contain a 'mutation' field.
151
+ * @returns {Promise<any> }
153
152
*/
154
- private async customMutation ( { state, dispatch } : ActionParams , args : Arguments ) {
153
+ private async customMutation ( { state, dispatch } : ActionParams , args : Arguments ) : Promise < any > {
155
154
const name : string = args [ 'mutation' ] ;
156
155
delete args [ 'mutation' ] ;
157
156
158
157
const model = this . getModel ( state . $name ) ;
159
158
160
- return await this . mutate ( name , args , dispatch , model ) ;
159
+ return this . mutate ( name , args , dispatch , model ) ;
161
160
}
162
161
163
162
/**
164
163
* Will be called, when dispatch('entities/something/push') is called.
165
- * @param {any } state
166
- * @param {any } dispatch
167
- * @param {Data } data
164
+ * @param {any } state The Vuex State from Vuex-ORM
165
+ * @param {DispatchFunction } dispatch Vuex Dispatch method for the model
166
+ * @param {Arguments } data New data to save
168
167
* @returns {Promise<Data | {}> }
169
168
*/
170
169
private async push ( { state, dispatch } : ActionParams , { data } : ActionParams ) {
@@ -187,32 +186,33 @@ export default class VuexORMApollo {
187
186
/**
188
187
* Will be called, when dispatch('entities/something/destroy') is called.
189
188
*
190
- * @param {any } state
191
- * @param {any } dispatch
192
- * @param {Data } id
189
+ * @param {any } state The Vuex State from Vuex-ORM
190
+ * @param {DispatchFunction } dispatch Vuex Dispatch method for the model
191
+ * @param {string } id ID of the record to delete
193
192
* @returns {Promise<void> }
194
193
*/
195
194
private async destroy ( { state, dispatch } : ActionParams , { id } : ActionParams ) : Promise < any > {
196
195
if ( id ) {
197
196
const model = this . getModel ( state . $name ) ;
198
197
const mutationName = `delete${ upcaseFirstLetter ( model . singularName ) } ` ;
199
- return await this . mutate ( mutationName , { id } , dispatch , model , false ) ;
198
+ return this . mutate ( mutationName , { id } , dispatch , model , false ) ;
200
199
}
201
200
}
202
201
203
202
/**
204
- * Contains the logic to save (persist or push) data .
203
+ * Sends a mutation .
205
204
*
206
- * @param {string } action
207
- * @param {Data | undefined } data
208
- * @param {Function } dispatch
209
- * @param {Model } model
205
+ * @param {string } name Name of the mutation like 'createUser'
206
+ * @param {Data | undefined } variables Variables to send with the mutation
207
+ * @param {Function } dispatch Vuex Dispatch method for the model
208
+ * @param {Model } model The model this mutation affects.
209
+ * @param {boolean } multiple See QueryBuilder.buildQuery()
210
210
* @returns {Promise<any> }
211
211
*/
212
- private async mutate ( action : string , variables : Data | undefined , dispatch : DispatchFunction , model : Model , multiple ?: boolean ) : Promise < any > {
212
+ private async mutate ( name : string , variables : Data | undefined , dispatch : DispatchFunction , model : Model , multiple ?: boolean ) : Promise < any > {
213
213
if ( variables ) {
214
214
const id = variables . id ? variables . id : undefined ;
215
- const query = this . queryBuilder . buildQuery ( 'mutation' , model , action , variables , multiple ) ;
215
+ const query = this . queryBuilder . buildQuery ( 'mutation' , model , name , variables , multiple ) ;
216
216
217
217
// Send GraphQL Mutation
218
218
const newData = await this . apolloRequest ( query , variables , true ) ;
@@ -224,16 +224,18 @@ export default class VuexORMApollo {
224
224
225
225
/**
226
226
* Sends a request to the GraphQL API via apollo
227
- * @param query
227
+ * @param {any } query The query to send (result from gql())
228
+ * @param {Arguments } variables Optional. The variables to send with the query
229
+ * @param {boolean } mutation Optional. If this is a mutation (true) or a query (false, default)
228
230
* @returns {Promise<Data> }
229
231
*/
230
232
private async apolloRequest ( query : any , variables ?: Arguments , mutation : boolean = false ) : Promise < Data > {
231
233
let response ;
232
234
233
235
if ( mutation ) {
234
- response = await ( this . apolloClient ) . mutate ( { mutation : query , variables } ) ;
236
+ response = await this . apolloClient . mutate ( { mutation : query , variables } ) ;
235
237
} else {
236
- response = await ( this . apolloClient ) . query ( { query, variables } ) ;
238
+ response = await this . apolloClient . query ( { query, variables } ) ;
237
239
}
238
240
239
241
// Transform incoming data into something useful
@@ -243,9 +245,8 @@ export default class VuexORMApollo {
243
245
/**
244
246
* Inserts incoming data into the store.
245
247
*
246
- * @param {Data } data
247
- * @param {Function } dispatch
248
- * @param {boolean } update
248
+ * @param {Data } data New data to insert/update
249
+ * @param {Function } dispatch Vuex Dispatch method for the model
249
250
*/
250
251
private async insertData ( data : Data , dispatch : DispatchFunction ) {
251
252
Object . keys ( data ) . forEach ( async ( key ) => {
@@ -257,8 +258,8 @@ export default class VuexORMApollo {
257
258
* Updates an existing record in the store with new data. This method can only update one single record, so
258
259
* it takes the first record of the first field from the data object!
259
260
* @param {Data } data
260
- * @param {Function } dispatch
261
- * @param id
261
+ * @param {Function } dispatch Vuex Dispatch method for the model
262
+ * @param { string|number } id ID of the record to update
262
263
*/
263
264
private async updateData ( data : Data , dispatch : DispatchFunction , id : number | string ) {
264
265
// We only take the first field!
0 commit comments