GraphQl
and mongosoe
each perform their tasks and my question is about retrieving data from database by mongoose
and relationship system with graphQl
.
in graphQl
if there is no scaler types means i have custom types like object or array of objects, so for this type i have to write relation between parent and custom type. i personally think this approach for retrieve a lot of data with async await
tasks can be slowly.
i wrote a nodejs server with some data and related relationships as simple as possible. is this the best approach to retreve a lot of data from database?
graphQl schema and relationship:
type User {
_id: ID!
name: String!
email: String!
posts: [Post!]!
comments: [Comment!]!
}
type Post {
_id: ID!
title: String!
body: String!
user: User!
comments: [Comment!]!
}
type Comment {
_id: ID!
text: String!
post: Post!
user: User!
}
type Mutation {
createUser(name: String!, email: String!): User!
createPost(title: String!, body: String!, user: String!): Post!
createComment(text: String!, post: String!, user: String!): Comment!
}
type Query {
getUsers: [User!]!
getPosts: [Post!]!
getComments: [Comment!]!
}
mongoose schema and relationship:
user, post and commnet:
const User = mongoose.model(
"User",
new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
posts: [
{
type: mongoose.Types.ObjectId,
ref: "Post"
}
],
comments: [
{
type: mongoose.Types.ObjectId,
ref: "Comment"
}
]
})
);
const Post = mongoose.model(
"Post",
new Schema({
title: {
type: String,
required: true
},
body: {
type: String,
required: true
},
user: {
type: mongoose.Types.ObjectId,
ref: "User"
},
comments: [
{
type: mongoose.Types.ObjectId,
ref: "Comment"
}
]
})
);
const Comment = mongoose.model(
"Comment",
new Schema({
text: {
type: String,
required: true
},
post: {
type: mongoose.Types.ObjectId,
ref: "Post"
},
user: {
type: mongoose.Types.ObjectId,
ref: "User"
}
})
);
resolvers:
const resolvers = {
Query: {
async getUsers() {
const users = await User.find({});
return users;
},
async getPosts() {
const posts = await Post.find({});
return posts;
},
async getComments() {
const comments = await Comment.find({});
return comments;
}
},
Mutation: {
async createUser(_, { name, email }) {
const newUser = new User({ name, email });
const createdUser = await newUser.save();
return createdUser;
},
async createPost(_, { title, body, user: userId }) {
const newPost = new Post({ title, body, user: userId });
const createdPost = await newPost.save();
const user = await User.findById(mongoose.Types.ObjectId(userId));
user.posts.push(createdPost._id);
await user.save();
return createdPost;
},
async createComment(_, { text, post: postId, user: userId }) {
const newComment = new Comment({ text, post: postId, user: userId });
const createdComment = await newComment.save();
const user = await User.findById(mongoose.Types.ObjectId(userId));
const post = await Post.findById(mongoose.Types.ObjectId(postId));
user.comments.push(createdComment._id);
post.comments.push(createdComment._id);
await user.save();
await post.save();
return createdComment;
}
},
User: {
async posts(parent) {
return await Post.find({ user: parent._id });
},
async comments(parent) {
return await Comment.find({ user: parent._id });
}
},
Post: {
async user(parent) {
return await User.findById(parent.user);
},
async comments(parent) {
return await Comment.find({ post: parent._id });
}
},
Comment: {
async post(parent) {
return await Post.findById(parent.post);
},
async user(parent) {
return await User.findById(parent.user);
}
}
};