Skip to content

Commit cabc093

Browse files
committed
initial commit
0 parents  commit cabc093

File tree

8 files changed

+3940
-0
lines changed

8 files changed

+3940
-0
lines changed

.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-env"]
3+
}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

package.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "graphql-mongo-server",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "nodemon --exec babel-node src/index.js"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"devDependencies": {
13+
"@babel/cli": "^7.4.3",
14+
"@babel/core": "^7.4.3",
15+
"@babel/node": "^7.2.2",
16+
"@babel/preset-env": "^7.4.3",
17+
"nodemon": "^1.18.11"
18+
},
19+
"dependencies": {
20+
"apollo-server-express": "^2.4.8",
21+
"express": "^4.16.4",
22+
"graphql": "^14.2.1",
23+
"mongoose": "^5.5.1"
24+
}
25+
}

src/index.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ApolloServer, gql } from "apollo-server-express";
2+
import express from "express";
3+
import mongoose from "mongoose";
4+
import { resolvers } from "./resolvers";
5+
import { typeDefs } from "./typeDefs";
6+
7+
const startServer = async () => {
8+
const app = express();
9+
10+
const server = new ApolloServer({
11+
typeDefs,
12+
resolvers
13+
});
14+
15+
server.applyMiddleware({ app });
16+
17+
await mongoose.connect("mongodb://localhost:27017/test3", {
18+
useNewUrlParser: true
19+
});
20+
21+
app.listen({ port: 4000 }, () =>
22+
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
23+
);
24+
};
25+
26+
startServer();

src/models/Cat.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import mongoose from "mongoose";
2+
3+
export const Cat = mongoose.model("Cat", { name: String });

src/resolvers.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Cat } from "./models/Cat";
2+
3+
export const resolvers = {
4+
Query: {
5+
hello: () => "hi",
6+
cats: () => Cat.find()
7+
},
8+
Mutation: {
9+
createCat: async (_, { name }) => {
10+
const kitty = new Cat({ name });
11+
await kitty.save();
12+
return kitty;
13+
}
14+
}
15+
};

src/typeDefs.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { gql } from "apollo-server-express";
2+
3+
export const typeDefs = gql`
4+
type Query {
5+
hello: String!
6+
cats: [Cat!]!
7+
}
8+
9+
type Cat {
10+
id: ID!
11+
name: String!
12+
}
13+
14+
type Mutation {
15+
createCat(name: String!): Cat!
16+
}
17+
`;

0 commit comments

Comments
 (0)