Commit 8fa8779d by qiuzhi99

post model

1 parent 0f154540
......@@ -4,15 +4,31 @@ const gql = require("graphql-tag");
const mongoose = require("mongoose");
const Post = require("./models/Post");
const typeDefs = gql`
type Post {
id: ID!
body: String!
createdAt: String!
username: String!
}
type Query {
sayHi: String!
getPosts: [Post]
}
`;
const resolvers = {
Query: {
sayHi: () => "hello world"
async getPosts() {
try {
const posts = await Post.find();
return posts;
} catch (err) {
throw new Error(err);
}
}
}
};
......
const { model, Schema } = require("mongoose");
const postSchema = new Schema({
body: String,
username: String,
createdAt: String,
comments: [
{
body: String,
username: String,
createdAt: String
}
],
likes: [
{
username: String,
createdAt: String
}
],
user: {
type: Schema.Types.ObjectId,
ref: "users"
}
});
module.exports = model("Post", postSchema);
const { model, Schema } = require("mongoose");
const userSchema = new Schema({
username: String,
password: String,
email: String,
createdAt: String
});
module.exports = model("User", userSchema);
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!