Skip to content

Commit 4f9d20f

Browse files
committed
Multiple fixes and optimizations
1 parent 8063195 commit 4f9d20f

File tree

5 files changed

+77
-26
lines changed

5 files changed

+77
-26
lines changed

src/queryBuilder.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,15 @@ export default class QueryBuilder {
155155
return relationQueries;
156156
}
157157

158-
public buildQuery (type: string, name?: string, args?: Arguments, model?: (Model | null | string), fields?: string, addModelToArgs: boolean = false, multiple?: boolean) {
158+
public buildQuery (type: string, name?: string, args?: Arguments, model?: (Model | null | string), fields?: string, multiple?: boolean) {
159159
model = model ? this.getModel(model) : null;
160160

161161
args = args ? JSON.parse(JSON.stringify(args)) : {};
162162
if (!args) throw new Error("args is undefined");
163-
if (addModelToArgs && model) args[model.singularName] = { __type: upcaseFirstLetter(model.singularName) };
163+
164+
if (model && args[model.singularName] && typeof args[model.singularName] === 'object') {
165+
args[model.singularName] = { __type: upcaseFirstLetter(model.singularName) };
166+
}
164167

165168
multiple = multiple === undefined ? !args['id'] : multiple;
166169

src/vuex-orm-apollo.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,12 @@ export default class VuexORMApollo {
130130
const model = this.getModel(state.$name);
131131
const data = model.baseModel.getters('find')(id);
132132

133+
const variables: Data = {
134+
[model.singularName]: this.queryBuilder.transformOutgoingData(data)
135+
};
136+
133137
const mutationName = `create${upcaseFirstLetter(model.singularName)}`;
134-
await this.mutate(mutationName, data, dispatch, model, true, false);
138+
await this.mutate(mutationName, variables, dispatch, model, false);
135139

136140
// TODO is this really necessary?
137141
return model.baseModel.getters('find')(id);
@@ -170,9 +174,13 @@ export default class VuexORMApollo {
170174
if (data) {
171175
const model = this.getModel(state.$name);
172176

177+
const variables: Data = {
178+
id: data.id,
179+
[model.singularName]: this.queryBuilder.transformOutgoingData(data)
180+
};
181+
173182
const mutationName = `update${upcaseFirstLetter(model.singularName)}`;
174-
// TODO whats in data? can we determine addModelToArgs from that?
175-
await this.mutate(mutationName, data, dispatch, model, true, false);
183+
await this.mutate(mutationName, variables, dispatch, model, false);
176184

177185
// TODO is this really necessary?
178186
return model.baseModel.getters('find')(data.id);
@@ -187,16 +195,16 @@ export default class VuexORMApollo {
187195
* @param {Data} id
188196
* @returns {Promise<void>}
189197
*/
190-
private async destroy ({ state, dispatch }: ActionParams, { id }: ActionParams): Promise<void> {
198+
private async destroy ({ state, dispatch }: ActionParams, { id }: ActionParams): Promise<any> {
191199

192200
if (id) {
193201
const model = this.getModel(state.$name);
194202
// TODO use mutate here too
195203
const mutationName = `delete${upcaseFirstLetter(model.singularName)}`;
196-
const query = this.queryBuilder.buildQuery('mutation', mutationName, { id }, model);
204+
await this.mutate(mutationName, { id }, dispatch, model, false)
197205

198-
// Send GraphQL Mutation
199-
await this.apolloRequest(query, { where: id }, true);
206+
// TODO what would make sense here?
207+
return true;
200208
}
201209
}
202210

@@ -209,22 +217,19 @@ export default class VuexORMApollo {
209217
* @param {Model} model
210218
* @returns {Promise<any>}
211219
*/
212-
private async mutate (action: string, data: Data | undefined, dispatch: DispatchFunction, model: Model, addModelToArgs: boolean = false, multiple?: boolean): Promise<any> {
213-
if (data) {
214-
const id = data.id ? data.id : undefined;
215-
const variables: Data = {
216-
[model.singularName]: this.queryBuilder.transformOutgoingData(data)
217-
};
220+
private async mutate (action: string, variables: Data | undefined, dispatch: DispatchFunction, model: Model, multiple?: boolean): Promise<any> {
221+
if (variables) {
222+
const id = variables.id ? variables.id : undefined;
218223

219224
// TODO what about the query fields?
220-
const query = this.queryBuilder.buildQuery('mutation', action, variables, model, undefined, addModelToArgs, multiple);
225+
const query = this.queryBuilder.buildQuery('mutation', action, variables, model, undefined, multiple);
221226

222-
// TODO don't add id for persist
223-
if (id) variables['id'] = id;
224227

225228
// Send GraphQL Mutation
226229
const newData = await this.apolloRequest(query, variables, true);
227-
return this.updateData(newData, dispatch, data.id);
230+
231+
// TODO: What if there is no id?
232+
return this.updateData(newData, dispatch, id);
228233
}
229234
}
230235

test/integration/VuexORMApollo.spec.js

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ query Users {
184184
await store.dispatch('entities/users/persist', { id: 1 });
185185
});
186186

187-
expect(request.variables).toEqual({ id: 1, user: { name: 'Johnny Imba' } });
187+
expect(request.variables).toEqual({ user: { name: 'Johnny Imba' } });
188188
expect(request.query).toEqual(`
189189
mutation CreateUser($user: UserInput!) {
190190
createUser(user: $user) {
@@ -232,8 +232,8 @@ mutation CreateUser($user: UserInput!) {
232232

233233
expect(request.variables).toEqual({ id: 1, user: { name: 'Charlie Brown' } });
234234
expect(request.query).toEqual(`
235-
mutation UpdateUser($user: UserInput!) {
236-
updateUser(user: $user) {
235+
mutation UpdateUser($id: ID!, $user: UserInput!) {
236+
updateUser(id: $id, user: $user) {
237237
id
238238
name
239239
posts {
@@ -273,7 +273,7 @@ mutation UpdateUser($user: UserInput!) {
273273
await store.dispatch('entities/users/destroy', { id: 1 });
274274
});
275275

276-
expect(request.variables).toEqual({ where: 1 });
276+
expect(request.variables).toEqual({ id: 1 });
277277
expect(request.query).toEqual(`
278278
mutation DeleteUser($id: ID!) {
279279
deleteUser(id: $id) {
@@ -294,4 +294,47 @@ mutation DeleteUser($id: ID!) {
294294
`.trim() + "\n");
295295
});
296296
});
297+
298+
299+
describe('customMutation', () => {
300+
it('sends the correct query to the API', async () => {
301+
const response = {
302+
data: {
303+
activateUser: {
304+
__typename: 'user',
305+
id: 1,
306+
name: 'Johnny Imba',
307+
posts: {
308+
__typename: 'post',
309+
nodes: []
310+
}
311+
}
312+
}
313+
};
314+
315+
const request = await sendWithMockFetch(response, async () => {
316+
await store.dispatch('entities/users/mutate', { mutation: 'activateUser', id: 1 });
317+
});
318+
319+
expect(request.variables).toEqual({ id: 1 });
320+
expect(request.query).toEqual(`
321+
mutation ActivateUser($id: ID!) {
322+
activateUser(id: $id) {
323+
id
324+
name
325+
posts {
326+
nodes {
327+
id
328+
title
329+
content
330+
__typename
331+
}
332+
__typename
333+
}
334+
__typename
335+
}
336+
}
337+
`.trim() + "\n");
338+
});
339+
});
297340
});

test/support/Helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ export function createStore (entities) {
3434

3535

3636
export async function sendWithMockFetch(response, callback) {
37+
fetchMock.config.overwriteRoutes = true;
3738
fetchMock.post('/graphql', response);
3839

3940
try {
4041
await callback();
41-
4242
} catch (error) {
4343
console.error("An error occured:");
4444
console.error(error);

test/unit/QueryBuilder.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ query Users {
317317
});
318318

319319
it('generates a complete create mutation query for a model', () => {
320-
let query = queryBuilder.buildQuery('mutation', 'createUser', { user: { id: 15, name: 'test' } }, new Model(User), undefined, true, false);
320+
let query = queryBuilder.buildQuery('mutation', 'createUser', { user: { id: 15, name: 'test' } }, new Model(User), undefined, false);
321321
query = QueryBuilder.prettify(query.loc.source.body);
322322

323323
expect(query).toEqual(`
@@ -338,7 +338,7 @@ mutation CreateUser($user: UserInput!) {
338338
});
339339

340340
it('generates a complete update mutation query for a model', () => {
341-
let query = queryBuilder.buildQuery('mutation', 'updateUser', { id: 15, user: { name: 'test' } }, new Model(User), undefined, true);
341+
let query = queryBuilder.buildQuery('mutation', 'updateUser', { id: 15, user: { name: 'test' } }, new Model(User), undefined, false);
342342
query = QueryBuilder.prettify(query.loc.source.body);
343343

344344
expect(query).toEqual(`

0 commit comments

Comments
 (0)