Commit 44e25903 by qiuzhi99

register

1 parent 8fa8779d
const postsResolvers = require("./posts");
const usersResolvers = require("./users");
module.exports = {
Query: {
...postsResolvers.Query
}
};
const Post = require("../../models/Post");
module.exports = {
Query: {
async getPosts() {
try {
const posts = await Post.find();
return posts;
} catch (err) {
throw new Error(err);
}
}
}
};
const { gql } = require("apollo-server");
module.exports = gql`
type Post {
id: ID!
body: String!
createdAt: String!
username: String!
}
type Query {
getPosts: [Post]
}
type User {
id: ID!
email: String!
username: String!
createdAt: String!
token: String!
}
input RegisterInput {
username: String!
email: String!
password: String!
confirmPassword: String!
}
type Mutation {
register(registerInput: RegisterInput): User!
}
`;
const { ApolloServer } = require("apollo-server"); const { ApolloServer } = require("apollo-server");
const gql = require("graphql-tag");
const mongoose = require("mongoose"); const mongoose = require("mongoose");
const Post = require("./models/Post"); const Post = require("./models/Post");
const typeDefs = gql` const typeDefs = require("./graphql/typeDefs");
type Post { const resolvers = require("./graphql/resolvers");
id: ID!
body: String!
createdAt: String!
username: String!
}
type Query {
getPosts: [Post]
}
`;
const resolvers = {
Query: {
async getPosts() {
try {
const posts = await Post.find();
return posts;
} catch (err) {
throw new Error(err);
}
}
}
};
const server = new ApolloServer({ const server = new ApolloServer({
typeDefs, typeDefs,
......
...@@ -3,9 +3,15 @@ ...@@ -3,9 +3,15 @@
"version": "1.0.0", "version": "1.0.0",
"main": "index.js", "main": "index.js",
"license": "MIT", "license": "MIT",
"scripts": {
"start": "nodemon index"
},
"dependencies": { "dependencies": {
"apollo-server": "^2.9.3", "apollo-server": "^2.9.3",
"graphql": "^14.5.4", "graphql": "^14.5.4",
"mongoose": "^5.6.13" "mongoose": "^5.6.13"
},
"devDependencies": {
"nodemon": "^1.19.2"
} }
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!