Skip to content

Commit 4882069

Browse files
author
Lucas Bento
authored
Support mongoose array fields (#94)
2 parents 81e0ded + a093882 commit 4882069

File tree

6 files changed

+268
-15
lines changed

6 files changed

+268
-15
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// @flow
2+
import mongoose from 'mongoose';
3+
4+
import User from './User';
5+
const { ObjectId } = mongoose.Schema.Types;
6+
7+
const Schema = new mongoose.Schema({
8+
author: {
9+
type: ObjectId,
10+
ref: 'User',
11+
description: 'User that created this comment',
12+
required: true,
13+
},
14+
score: {
15+
type: Number,
16+
description: 'Sum of all upvotes/downvotes this comment has',
17+
required: false,
18+
},
19+
text: {
20+
type: String,
21+
required: true,
22+
},
23+
}, {
24+
timestamps: {
25+
createdAt: 'createdAt',
26+
updatedAt: 'updatedAt',
27+
},
28+
collection: 'comment',
29+
});
30+
31+
export default mongoose.model('Comment', Schema);

packages/generator/fixtures/Post.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import mongoose from 'mongoose';
22

33
import User from './User';
4+
import Comment from './Comment';
45
const { ObjectId } = mongoose.Schema.Types;
56

67
const Schema = new mongoose.Schema({
@@ -20,6 +21,22 @@ const Schema = new mongoose.Schema({
2021
indexed: true,
2122
description: 'Used for SEO',
2223
},
24+
tags: [String],
25+
oldSlugs: {
26+
type: [String],
27+
description: 'Old slugs used by this post'
28+
},
29+
comments: [
30+
{
31+
type: ObjectId,
32+
ref: 'Comment',
33+
},
34+
],
35+
externalComments: {
36+
type: [ObjectId],
37+
ref: 'Comment',
38+
description: 'Comments from external source'
39+
},
2340
}, {
2441
timestamps: {
2542
createdAt: 'createdAt',

packages/generator/src/loader/__tests__/__snapshots__/LoaderGenerator.spec.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ type PostType = {
7676
title: string,
7777
user: string,
7878
slug: string,
79+
tags: array,
80+
oldSlugs: array,
81+
comments: array,
82+
externalComments: array,
7983
createdAt: string,
8084
updatedAt: string,
8185
}
@@ -86,6 +90,10 @@ export default class Post {
8690
title: string;
8791
user: string;
8892
slug: string;
93+
tags: array;
94+
oldSlugs: array;
95+
comments: array;
96+
externalComments: array;
8997
createdAt: string;
9098
updatedAt: string;
9199
@@ -97,6 +105,10 @@ export default class Post {
97105
this.title = data.title;
98106
this.user = data.user;
99107
this.slug = data.slug;
108+
this.tags = data.tags;
109+
this.oldSlugs = data.oldSlugs;
110+
this.comments = data.comments;
111+
this.externalComments = data.externalComments;
100112
this.createdAt = data.createdAt;
101113
this.updatedAt = data.updatedAt;
102114
}

packages/generator/src/mutation/__tests__/__snapshots__/MutationGenerator.spec.js.snap

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ Object {
261261
import {
262262
GraphQLString,
263263
GraphQLID,
264+
GraphQLList,
264265
GraphQLNonNull,
265266
} from 'graphql';
266267
import {
@@ -287,6 +288,18 @@ export default mutationWithClientMutationId({
287288
slug: {
288289
type: GraphQLString,
289290
},
291+
tags: {
292+
type: new GraphQLList(GraphQLString),
293+
},
294+
oldSlugs: {
295+
type: new GraphQLList(GraphQLString),
296+
},
297+
comments: {
298+
type: new GraphQLList(GraphQLID),
299+
},
300+
externalComments: {
301+
type: new GraphQLList(GraphQLID),
302+
},
290303
},
291304
mutateAndGetPayload: async (args, { user }) => {
292305
// Verify if user is authorized
@@ -298,13 +311,21 @@ export default mutationWithClientMutationId({
298311
title,
299312
user,
300313
slug,
314+
tags,
315+
oldSlugs,
316+
comments,
317+
externalComments,
301318
} = args;
302319
303320
// Create new record
304321
const post = await new Post({
305322
title,
306323
user,
307324
slug,
325+
tags,
326+
oldSlugs,
327+
comments,
328+
externalComments,
308329
}).save();
309330
310331
// TODO: mutation logic
@@ -361,12 +382,20 @@ it('should not allow anonymous user', async () => {
361382
title: \\"Example value\\"
362383
user: \\"Example value\\"
363384
slug: \\"Example value\\"
385+
tags: \\"Example value\\"
386+
oldSlugs: \\"Example value\\"
387+
comments: \\"Example value\\"
388+
externalComments: \\"Example value\\"
364389
}) {
365390
postEdge {
366391
node {
367392
title
368393
user
369394
slug
395+
tags
396+
oldSlugs
397+
comments
398+
externalComments
370399
}
371400
}
372401
}
@@ -396,12 +425,20 @@ it('should create a record on database', async () => {
396425
title: \\"Example value\\"
397426
user: \\"Example value\\"
398427
slug: \\"Example value\\"
428+
tags: \\"Example value\\"
429+
oldSlugs: \\"Example value\\"
430+
comments: \\"Example value\\"
431+
externalComments: \\"Example value\\"
399432
}) {
400433
postEdge {
401434
node {
402435
title
403436
user
404437
slug
438+
tags
439+
oldSlugs
440+
comments
441+
externalComments
405442
}
406443
}
407444
}
@@ -422,6 +459,7 @@ import {
422459
GraphQLString,
423460
GraphQLNonNull,
424461
GraphQLID,
462+
GraphQLList,
425463
} from 'graphql';
426464
import {
427465
mutationWithClientMutationId,
@@ -448,6 +486,18 @@ export default mutationWithClientMutationId({
448486
slug: {
449487
type: GraphQLString,
450488
},
489+
tags: {
490+
type: new GraphQLList(GraphQLString),
491+
},
492+
oldSlugs: {
493+
type: new GraphQLList(GraphQLString),
494+
},
495+
comments: {
496+
type: new GraphQLList(GraphQLID),
497+
},
498+
externalComments: {
499+
type: new GraphQLList(GraphQLID),
500+
},
451501
},
452502
mutateAndGetPayload: async (args, context) => {
453503
const { user } = context;
@@ -462,6 +512,10 @@ export default mutationWithClientMutationId({
462512
title,
463513
user,
464514
slug,
515+
tags,
516+
oldSlugs,
517+
comments,
518+
externalComments,
465519
} = args;
466520
467521
// Check if the provided ID is valid
@@ -479,6 +533,10 @@ export default mutationWithClientMutationId({
479533
title,
480534
user,
481535
slug,
536+
tags,
537+
oldSlugs,
538+
comments,
539+
externalComments,
482540
});
483541
484542
// TODO: mutation logic
@@ -519,6 +577,10 @@ it('should not allow anonymous user', async () => {
519577
title: 'Example value',
520578
user: 'Example value',
521579
slug: 'Example value',
580+
tags: 'Example value',
581+
oldSlugs: 'Example value',
582+
comments: 'Example value',
583+
externalComments: 'Example value',
522584
});
523585
524586
await post.save();
@@ -535,6 +597,10 @@ it('should not allow anonymous user', async () => {
535597
title
536598
user
537599
slug
600+
tags
601+
oldSlugs
602+
comments
603+
externalComments
538604
}
539605
}
540606
}
@@ -562,6 +628,10 @@ it('should edit a record on database', async () => {
562628
title: 'Example value',
563629
user: 'Example value',
564630
slug: 'Example value',
631+
tags: 'Example value',
632+
oldSlugs: 'Example value',
633+
comments: 'Example value',
634+
externalComments: 'Example value',
565635
});
566636
567637
await post.save();
@@ -578,6 +648,10 @@ it('should edit a record on database', async () => {
578648
title
579649
user
580650
slug
651+
tags
652+
oldSlugs
653+
comments
654+
externalComments
581655
}
582656
}
583657
}

packages/generator/src/type/__tests__/__snapshots__/TypeGenerator.spec.js.snap

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,15 @@ Object {
6868
import {
6969
GraphQLObjectType,
7070
GraphQLString,
71+
GraphQLList,
7172
} from 'graphql';
7273
import { globalIdField } from 'graphql-relay';
7374
7475
import { NodeInterface } from '../interface/NodeInterface';
7576
import UserType from './UserType';
77+
import CommentType from './CommentType';
7678
import UserLoader from '../loader/UserLoader';
79+
import CommentLoader from '../loader/CommentLoader';
7780
7881
export default new GraphQLObjectType({
7982
name: 'Post',
@@ -95,6 +98,26 @@ export default new GraphQLObjectType({
9598
description: 'Used for SEO',
9699
resolve: obj => obj.slug,
97100
},
101+
tags: {
102+
type: new GraphQLList(GraphQLString),
103+
description: '',
104+
resolve: obj => obj.tags,
105+
},
106+
oldSlugs: {
107+
type: new GraphQLList(GraphQLString),
108+
description: 'Old slugs used by this post',
109+
resolve: obj => obj.oldSlugs,
110+
},
111+
comments: {
112+
type: new GraphQLList(CommentType),
113+
description: '',
114+
resolve: async (obj, args, context) => await CommentLoader.loadCommentsByIds(context, obj.comments),
115+
},
116+
externalComments: {
117+
type: new GraphQLList(CommentType),
118+
description: 'Comments from external source',
119+
resolve: async (obj, args, context) => await CommentLoader.loadExternalCommentsByIds(context, obj.externalComments),
120+
},
98121
createdAt: {
99122
type: GraphQLString,
100123
description: '',

0 commit comments

Comments
 (0)