Skip to content

Commit dec8142

Browse files
committed
Better tests and some bugfixes
1 parent 1efa48b commit dec8142

File tree

11 files changed

+161
-132
lines changed

11 files changed

+161
-132
lines changed

dist/vuex-orm-graphql.esm.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27397,14 +27397,16 @@ var Transformer = /** @class */ (function () {
2739727397
// Ignore hasMany/One connections, empty fields and internal fields ($)
2739827398
if ((!relations.has(key) || relations.get(key) instanceof context.components.BelongsTo) &&
2739927399
!key.startsWith('$') && value !== null) {
27400+
var relatedModel = relations.get(key)
27401+
? context.getModel(inflection$1.singularize(relations.get(key).parent.entity), true)
27402+
: null;
2740027403
if (value instanceof Array) {
2740127404
// Iterate over all fields and transform them if value is an array
2740227405
var arrayModel_1 = context.getModel(inflection$1.singularize(key));
2740327406
returnValue[key] = value.map(function (v) { return _this.transformOutgoingData(arrayModel_1 || model, v); });
2740427407
}
27405-
else if (typeof value === 'object' && context.getModel(inflection$1.singularize(key), true)) {
27408+
else if (typeof value === 'object' && relatedModel) {
2740627409
// Value is a record, transform that too
27407-
var relatedModel = context.getModel(inflection$1.singularize(key));
2740827410
returnValue[key] = _this.transformOutgoingData(relatedModel || model, value);
2740927411
}
2741027412
else {
@@ -28204,7 +28206,7 @@ var QueryBuilder = /** @class */ (function () {
2820428206
}
2820528207
else {
2820628208
// Case 1 (String!)
28207-
typeOrValue = _this.determineAttributeType(model, key, value);
28209+
typeOrValue = _this.determineAttributeType(model, key, value, field || undefined);
2820828210
typeOrValue = typeOrValue + '!';
2820928211
}
2821028212
}
@@ -28230,12 +28232,25 @@ var QueryBuilder = /** @class */ (function () {
2823028232
* @param {Model} model
2823128233
* @param {string} key
2823228234
* @param {string} value
28235+
* @param {GraphQLField} query Pass when we have to detect the type of an argument
2823328236
* @returns {string}
2823428237
*/
28235-
QueryBuilder.determineAttributeType = function (model, key, value) {
28238+
QueryBuilder.determineAttributeType = function (model, key, value, query) {
2823628239
var context = Context.getInstance();
2823728240
var field = model.fields.get(key);
28238-
var schemaField = context.schema.getType(model.singularName).fields.find(function (f) { return f.name === key; });
28241+
var schemaField;
28242+
if (query) {
28243+
schemaField = query.args.find(function (f) { return f.name === key; });
28244+
if (!schemaField) {
28245+
var filterField = query.args.find(function (f) { return f.name === 'filter'; });
28246+
if (filterField) {
28247+
schemaField = this.findSchemaFieldForArgument(key, null, model, true);
28248+
}
28249+
}
28250+
}
28251+
else {
28252+
schemaField = context.schema.getType(model.singularName).fields.find(function (f) { return f.name === key; });
28253+
}
2823928254
if (schemaField && Schema.getTypeNameOfField(schemaField)) {
2824028255
return Schema.getTypeNameOfField(schemaField);
2824128256
}
@@ -28303,12 +28318,11 @@ var QueryBuilder = /** @class */ (function () {
2830328318
relatedModel = context.getModel(name);
2830428319
context.logger.log('WARNING: field has neither parent nor related property. Fallback to attribute name', field);
2830528320
}
28306-
var singularizedFieldName = inflection$3.singularize(name);
28307-
var ignore = path.includes(singularizedFieldName);
28321+
var ignore = path.includes(relatedModel.singularName);
2830828322
// console.log(`-----> Will ${ignore ? '' : 'not'} ignore ${model.singularName}.${name}, path: ${path.join('.')}`);
2830928323
if (model.shouldEagerLoadRelation(name, field, relatedModel) && !ignore) {
2831028324
var newPath = path.slice(0);
28311-
newPath.push(singularizedFieldName);
28325+
newPath.push(relatedModel.singularName);
2831228326
relationQueries.push(_this.buildField(relatedModel, Model.isConnection(field), undefined, newPath, name, false));
2831328327
}
2831428328
});

src/graphql/query-builder.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export default class QueryBuilder {
196196
typeOrValue = 'ID!';
197197
} else {
198198
// Case 1 (String!)
199-
typeOrValue = this.determineAttributeType(model, key, value);
199+
typeOrValue = this.determineAttributeType(model, key, value, field || undefined);
200200
typeOrValue = typeOrValue + '!';
201201
}
202202
} else {
@@ -225,13 +225,27 @@ export default class QueryBuilder {
225225
* @param {Model} model
226226
* @param {string} key
227227
* @param {string} value
228+
* @param {GraphQLField} query Pass when we have to detect the type of an argument
228229
* @returns {string}
229230
*/
230-
public static determineAttributeType (model: Model, key: string, value: any): string {
231+
public static determineAttributeType (model: Model, key: string, value: any, query?: GraphQLField): string {
231232
const context: Context = Context.getInstance();
232233
const field: undefined | Field = model.fields.get(key);
234+
let schemaField: undefined | GraphQLField;
233235

234-
const schemaField = context.schema!.getType(model.singularName)!.fields!.find(f => f.name === key);
236+
if (query) {
237+
schemaField = query.args.find(f => f.name === key);
238+
239+
if (!schemaField) {
240+
const filterField = query.args.find(f => f.name === 'filter');
241+
242+
if (filterField) {
243+
schemaField = this.findSchemaFieldForArgument(key, null, model, true);
244+
}
245+
}
246+
} else {
247+
schemaField = context.schema!.getType(model.singularName)!.fields!.find(f => f.name === key);
248+
}
235249

236250
if (schemaField && Schema.getTypeNameOfField(schemaField)) {
237251
return Schema.getTypeNameOfField(schemaField);
@@ -301,14 +315,13 @@ export default class QueryBuilder {
301315
context.logger.log('WARNING: field has neither parent nor related property. Fallback to attribute name', field);
302316
}
303317

304-
const singularizedFieldName = inflection.singularize(name);
305-
const ignore = path.includes(singularizedFieldName);
318+
const ignore = path.includes(relatedModel.singularName);
306319

307320
// console.log(`-----> Will ${ignore ? '' : 'not'} ignore ${model.singularName}.${name}, path: ${path.join('.')}`);
308321

309322
if (model.shouldEagerLoadRelation(name, field, relatedModel) && !ignore) {
310323
const newPath = path.slice(0);
311-
newPath.push(singularizedFieldName);
324+
newPath.push(relatedModel.singularName);
312325

313326
relationQueries.push(this.buildField(relatedModel, Model.isConnection(field), undefined, newPath, name, false));
314327
}

src/graphql/transformer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ export default class Transformer {
3131
if ((!relations.has(key) || relations.get(key) instanceof context.components.BelongsTo) &&
3232
!key.startsWith('$') && value !== null) {
3333

34+
let relatedModel = relations.get(key)
35+
? context.getModel(inflection.singularize(relations.get(key)!.parent!.entity), true)
36+
: null;
37+
3438
if (value instanceof Array) {
3539
// Iterate over all fields and transform them if value is an array
3640
const arrayModel = context.getModel(inflection.singularize(key));
3741
returnValue[key] = value.map((v) => this.transformOutgoingData(arrayModel || model, v));
38-
} else if (typeof value === 'object' && context.getModel(inflection.singularize(key), true)) {
42+
} else if (typeof value === 'object' && relatedModel) {
3943
// Value is a record, transform that too
40-
const relatedModel = context.getModel(inflection.singularize(key));
4144
returnValue[key] = this.transformOutgoingData(relatedModel || model, value);
4245
} else {
4346
// In any other case just let the value be what ever it is

test/integration/VuexORMGraphQL.spec.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ query Post($id: ID!) {
4141
title
4242
otherId
4343
published
44-
user {
44+
author {
4545
id
4646
name
4747
profile {
@@ -57,7 +57,7 @@ query Post($id: ID!) {
5757
content
5858
subjectId
5959
subjectType
60-
user {
60+
author {
6161
id
6262
name
6363
profile {
@@ -177,7 +177,7 @@ query Users {
177177
content: "This is a test!",
178178
published: false,
179179
otherId: 15,
180-
user: User.find(1)
180+
author: User.find(1)
181181
}});
182182

183183
let post = insertedData.posts[0];
@@ -195,8 +195,8 @@ query Users {
195195
otherId: 15,
196196
published: false,
197197
title: "It works!",
198-
userId: 1,
199-
user: {
198+
authorId: 1,
199+
author: {
200200
id: 1,
201201
name: 'Charlie Brown',
202202
profileId: 1,
@@ -219,7 +219,7 @@ mutation CreatePost($post: PostInput!) {
219219
title
220220
otherId
221221
published
222-
user {
222+
author {
223223
id
224224
name
225225
profile {
@@ -235,7 +235,7 @@ mutation CreatePost($post: PostInput!) {
235235
content
236236
subjectId
237237
subjectType
238-
user {
238+
author {
239239
id
240240
name
241241
profile {
@@ -301,7 +301,7 @@ mutation DeletePost($id: ID!) {
301301
title
302302
otherId
303303
published
304-
user {
304+
author {
305305
id
306306
name
307307
profile {
@@ -317,7 +317,7 @@ mutation DeletePost($id: ID!) {
317317
content
318318
subjectId
319319
subjectType
320-
user {
320+
author {
321321
id
322322
name
323323
profile {
@@ -339,20 +339,20 @@ mutation DeletePost($id: ID!) {
339339
describe('custom query', () => {
340340
it('via Model method sends the correct query to the API', async () => {
341341
const request = await recordGraphQLRequest(async () => {
342-
await Post.customQuery({name: 'unpublishedPosts', filter: {userId: 3}});
342+
await Post.customQuery({name: 'unpublishedPosts', filter: {authorId: 3}});
343343
});
344344

345-
expect(request.variables.userId).toEqual(3);
345+
expect(request.variables.authorId).toEqual(3);
346346
expect(request.query).toEqual(`
347-
query UnpublishedPosts($userId: ID!) {
348-
unpublishedPosts(userId: $userId) {
347+
query UnpublishedPosts($authorId: ID!) {
348+
unpublishedPosts(authorId: $authorId) {
349349
nodes {
350350
id
351351
content
352352
title
353353
otherId
354354
published
355-
user {
355+
author {
356356
id
357357
name
358358
profile {
@@ -368,7 +368,7 @@ query UnpublishedPosts($userId: ID!) {
368368
content
369369
subjectId
370370
subjectType
371-
user {
371+
author {
372372
id
373373
name
374374
profile {
@@ -391,21 +391,21 @@ query UnpublishedPosts($userId: ID!) {
391391
const post = Post.find(1);
392392

393393
const request = await recordGraphQLRequest(async () => {
394-
await post.$customQuery({name: 'unpublishedPosts', filter: {userId: 2}});
394+
await post.$customQuery({name: 'unpublishedPosts', filter: {authorId: 2}});
395395
});
396396

397-
expect(request.variables.userId).toEqual(2);
397+
expect(request.variables.authorId).toEqual(2);
398398
expect(request.variables.id).toEqual(1);
399399
expect(request.query).toEqual(`
400-
query UnpublishedPosts($userId: ID!, $id: ID!) {
401-
unpublishedPosts(userId: $userId, id: $id) {
400+
query UnpublishedPosts($authorId: ID!, $id: ID!) {
401+
unpublishedPosts(authorId: $authorId, id: $id) {
402402
nodes {
403403
id
404404
content
405405
title
406406
otherId
407407
published
408-
user {
408+
author {
409409
id
410410
name
411411
profile {
@@ -421,7 +421,7 @@ query UnpublishedPosts($userId: ID!, $id: ID!) {
421421
content
422422
subjectId
423423
subjectType
424-
user {
424+
author {
425425
id
426426
name
427427
profile {
@@ -460,7 +460,7 @@ mutation UpvotePost($captchaToken: String!, $postId: ID!) {
460460
title
461461
otherId
462462
published
463-
user {
463+
author {
464464
id
465465
name
466466
profile {
@@ -476,7 +476,7 @@ mutation UpvotePost($captchaToken: String!, $postId: ID!) {
476476
content
477477
subjectId
478478
subjectType
479-
user {
479+
author {
480480
id
481481
name
482482
profile {
@@ -646,10 +646,10 @@ query Status {
646646
const result = await Post.fetch(1, true);
647647

648648
const post = Post.query().withAllRecursive().find(1);
649-
expect(post.user).not.toEqual(null);
649+
expect(post.author).not.toEqual(null);
650650
expect(post.comments).not.toEqual(null);
651651
expect(post.comments.length).not.toEqual(0);
652-
expect(post.user.name).toEqual('Charlie Brown');
652+
expect(post.author.name).toEqual('Charlie Brown');
653653
expect(post.comments[0].content).toEqual('Yes!!!!');
654654
});
655655
});

test/support/mock-data.js

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export class User extends ORMModel {
1111
id: this.increment(0),
1212
name: this.string(''),
1313
profileId: this.number(0),
14-
posts: this.hasMany(Post, 'userId'),
15-
comments: this.hasMany(Comment, 'userId'),
14+
posts: this.hasMany(Post, 'authorId'),
15+
comments: this.hasMany(Comment, 'authorId'),
1616
profile: this.belongsTo(Profile, 'profileId')
1717
};
1818
}
@@ -41,10 +41,10 @@ export class Video extends ORMModel {
4141
id: this.increment(null),
4242
content: this.string(''),
4343
title: this.string(''),
44-
userId: this.number(0),
44+
authorId: this.number(0),
4545
otherId: this.number(0), // This is a field which ends with `Id` but doesn't belong to any relation
4646
ignoreMe: this.string(''),
47-
user: this.belongsTo(User, 'userId'),
47+
author: this.belongsTo(User, 'authorId'),
4848
comments: this.morphMany(Comment, 'subjectId', 'subjectType')
4949
};
5050
}
@@ -59,10 +59,10 @@ export class Post extends ORMModel {
5959
id: this.increment(null),
6060
content: this.string(''),
6161
title: this.string(''),
62-
userId: this.number(0),
62+
authorId: this.number(0),
6363
otherId: this.number(0), // This is a field which ends with `Id` but doesn't belong to any relation
6464
published: this.boolean(true),
65-
user: this.belongsTo(User, 'userId'),
65+
author: this.belongsTo(User, 'authorId'),
6666
comments: this.morphMany(Comment, 'subjectId', 'subjectType')
6767
};
6868
}
@@ -76,8 +76,8 @@ export class Comment extends ORMModel {
7676
return {
7777
id: this.increment(0),
7878
content: this.string(''),
79-
userId: this.number(0),
80-
user: this.belongsTo(User, 'userId'),
79+
authorId: this.number(0),
80+
author: this.belongsTo(User, 'authorId'),
8181

8282
subjectId: this.number(0),
8383
subjectType: this.string('')
@@ -147,18 +147,5 @@ export async function setupMockData() {
147147
{ model: TariffTariffOption }
148148
]);
149149

150-
/*await Profile.insert({ data: { id: 1, age: 8, sex: true, email: 'charly@peanuts.com' }});
151-
await Profile.insert({ data: { id: 2, age: 9, sex: false, email: 'pepper@peanuts.com' }});
152-
153-
await User.insert({ data: { id: 1, name: 'Charlie Brown', profileId: 1 }});
154-
await User.insert({ data: { id: 2, name: 'Peppermint Patty', profileId: 2 }});
155-
156-
await Post.insert({ data: { id: 1, otherId: 9, userId: 1, title: 'Example post 1', content: 'Foo' }});
157-
await Post.insert({ data: { id: 2, otherId: 10, userId: 1, title: 'Example post 2', content: 'Bar' }});
158-
await Video.insert({ data: { id: 1, otherId: 11, userId: 1, title: 'Example video', content: 'Video' }});
159-
await Comment.insert({ data: { id: 1, userId: 1, subjectId: 1, subjectType: 'videos', content: 'Example comment 1' }});
160-
await Comment.insert({ data: { id: 2, userId: 2, subjectId: 1, subjectType: 'posts', content: 'Example comment 2' }});
161-
await Comment.insert({ data: { id: 3, userId: 2, subjectId: 2, subjectType: 'posts', content: 'Example comment 3' }});*/
162-
163150
return [store, vuexOrmGraphQL];
164151
}

0 commit comments

Comments
 (0)