Skip to content

Commit 0752f24

Browse files
committed
Fixes
1 parent 4f9d20f commit 0752f24

File tree

2 files changed

+84
-85
lines changed

2 files changed

+84
-85
lines changed

src/queryBuilder.ts

Lines changed: 82 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -31,66 +31,6 @@ export default class QueryBuilder {
3131
return print(parse(query));
3232
}
3333

34-
/**
35-
* Generates the arguments string for a graphql query based on a given map.
36-
*
37-
* There are three types of arguments:
38-
*
39-
* 1) Signatures with simple types (signature = true)
40-
* mutation createUser($name: String!)
41-
*
42-
* 2) Signatures with object types (signature = true, args = { user: { __type: 'User' }})
43-
* mutation createUser($user: UserInput!)
44-
*
45-
* 3) Fields with variables (signature = false, valuesAsVariables = true)
46-
* query user(id: $id)
47-
*
48-
* @param {Arguments | undefined} args
49-
* @param {boolean} signature When true, then this method generates a query signature instead of key/value pairs
50-
* @param {boolean} allowIdFields If true, ID fields will be included in the arguments list
51-
* @returns {String}
52-
*/
53-
private buildArguments (args?: Arguments, signature: boolean = false, allowIdFields: boolean = true): string {
54-
if (args === null) return '';
55-
56-
let returnValue: string = '';
57-
let first: boolean = true;
58-
59-
if (args) {
60-
Object.keys(args).forEach((key: string) => {
61-
let value: any = args[key];
62-
63-
// Ignore ids and connections
64-
if (!(value instanceof Array || (key === 'id' && !allowIdFields))) {
65-
let typeOrValue: any = '';
66-
67-
if (signature) {
68-
if (typeof value === 'object' && value.__type) {
69-
// Case 2 (User!)
70-
typeOrValue = value.__type + 'Input!';
71-
} else if (key === 'id') {
72-
// Case 1 (ID!)
73-
typeOrValue = 'ID!';
74-
} else {
75-
// Case 1 (String!)
76-
typeOrValue = typeof value === 'number' ? 'Number!' : 'String!';
77-
}
78-
} else {
79-
// Case 3 (user: $user)
80-
typeOrValue = `$${key}`;
81-
}
82-
83-
returnValue = `${returnValue}${first ? '' : ', '}${(signature ? '$' : '') + key}: ${typeOrValue}`;
84-
first = false;
85-
}
86-
});
87-
88-
if (!first) returnValue = `(${returnValue})`;
89-
}
90-
91-
return returnValue;
92-
}
93-
9434
/**
9535
* Builds a field for the GraphQL query and a specific model
9636
*
@@ -134,32 +74,11 @@ export default class QueryBuilder {
13474
}
13575
}
13676

137-
/**
138-
*
139-
* @param {Model} model
140-
* @param {Model} rootModel
141-
* @returns {Array<String>}
142-
*/
143-
private buildRelationsQuery (model: (null | Model), rootModel?: Model) {
144-
if (model === null) return '';
145-
146-
const relationQueries: Array<string> = [];
147-
148-
model.getRelations().forEach((field: Field, name: string) => {
149-
if (!rootModel || (name !== rootModel.singularName && name !== rootModel.pluralName)) {
150-
const multiple: boolean = field.constructor.name !== 'BelongsTo';
151-
relationQueries.push(this.buildField(name, multiple, undefined, rootModel || model));
152-
}
153-
});
154-
155-
return relationQueries;
156-
}
157-
15877
public buildQuery (type: string, name?: string, args?: Arguments, model?: (Model | null | string), fields?: string, multiple?: boolean) {
15978
model = model ? this.getModel(model) : null;
16079

16180
args = args ? JSON.parse(JSON.stringify(args)) : {};
162-
if (!args) throw new Error("args is undefined");
81+
if (!args) throw new Error('args is undefined');
16382

16483
if (model && args[model.singularName] && typeof args[model.singularName] === 'object') {
16584
args[model.singularName] = { __type: upcaseFirstLetter(model.singularName) };
@@ -252,4 +171,85 @@ export default class QueryBuilder {
252171

253172
return result;
254173
}
174+
175+
/**
176+
* Generates the arguments string for a graphql query based on a given map.
177+
*
178+
* There are three types of arguments:
179+
*
180+
* 1) Signatures with simple types (signature = true)
181+
* mutation createUser($name: String!)
182+
*
183+
* 2) Signatures with object types (signature = true, args = { user: { __type: 'User' }})
184+
* mutation createUser($user: UserInput!)
185+
*
186+
* 3) Fields with variables (signature = false, valuesAsVariables = true)
187+
* query user(id: $id)
188+
*
189+
* @param {Arguments | undefined} args
190+
* @param {boolean} signature When true, then this method generates a query signature instead of key/value pairs
191+
* @param {boolean} allowIdFields If true, ID fields will be included in the arguments list
192+
* @returns {String}
193+
*/
194+
private buildArguments (args?: Arguments, signature: boolean = false, allowIdFields: boolean = true): string {
195+
if (args === undefined) return '';
196+
197+
let returnValue: string = '';
198+
let first: boolean = true;
199+
200+
if (args) {
201+
Object.keys(args).forEach((key: string) => {
202+
let value: any = args[key];
203+
204+
// Ignore ids and connections
205+
if (!(value instanceof Array || (key === 'id' && !allowIdFields))) {
206+
let typeOrValue: any = '';
207+
208+
if (signature) {
209+
if (typeof value === 'object' && value.__type) {
210+
// Case 2 (User!)
211+
typeOrValue = value.__type + 'Input!';
212+
} else if (key === 'id') {
213+
// Case 1 (ID!)
214+
typeOrValue = 'ID!';
215+
} else {
216+
// Case 1 (String!)
217+
typeOrValue = typeof value === 'number' ? 'Number!' : 'String!';
218+
}
219+
} else {
220+
// Case 3 (user: $user)
221+
typeOrValue = `$${key}`;
222+
}
223+
224+
returnValue = `${returnValue}${first ? '' : ', '}${(signature ? '$' : '') + key}: ${typeOrValue}`;
225+
first = false;
226+
}
227+
});
228+
229+
if (!first) returnValue = `(${returnValue})`;
230+
}
231+
232+
return returnValue;
233+
}
234+
235+
/**
236+
*
237+
* @param {Model} model
238+
* @param {Model} rootModel
239+
* @returns {Array<String>}
240+
*/
241+
private buildRelationsQuery (model: (null | Model), rootModel?: Model) {
242+
if (model === null) return '';
243+
244+
const relationQueries: Array<string> = [];
245+
246+
model.getRelations().forEach((field: Field, name: string) => {
247+
if (!rootModel || (name !== rootModel.singularName && name !== rootModel.pluralName)) {
248+
const multiple: boolean = field.constructor.name !== 'BelongsTo';
249+
relationQueries.push(this.buildField(name, multiple, undefined, rootModel || model));
250+
}
251+
});
252+
253+
return relationQueries;
254+
}
255255
}

src/vuex-orm-apollo.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export default class VuexORMApollo {
201201
const model = this.getModel(state.$name);
202202
// TODO use mutate here too
203203
const mutationName = `delete${upcaseFirstLetter(model.singularName)}`;
204-
await this.mutate(mutationName, { id }, dispatch, model, false)
204+
await this.mutate(mutationName, { id }, dispatch, model, false);
205205

206206
// TODO what would make sense here?
207207
return true;
@@ -224,7 +224,6 @@ export default class VuexORMApollo {
224224
// TODO what about the query fields?
225225
const query = this.queryBuilder.buildQuery('mutation', action, variables, model, undefined, multiple);
226226

227-
228227
// Send GraphQL Mutation
229228
const newData = await this.apolloRequest(query, variables, true);
230229

@@ -238,7 +237,7 @@ export default class VuexORMApollo {
238237
* @param query
239238
* @returns {Promise<Data>}
240239
*/
241-
private async apolloRequest (query: any, variables?: Arguments, mutation:boolean = false): Promise<Data> {
240+
private async apolloRequest (query: any, variables?: Arguments, mutation: boolean = false): Promise<Data> {
242241
let response;
243242

244243
if (mutation) {

0 commit comments

Comments
 (0)