Skip to content

Commit a672db3

Browse files
authored
Merge pull request thombergs#273 from ajibade3210/nodeGraphql
Node graphql
2 parents 5a9dd70 + 27b63f4 commit a672db3

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### NBuild Awesome CRUD APIs Using Apollo Server(Graphql), MongoDB and Node.Js Code Example
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const { ApolloServer } = require("@apollo/server");
2+
const { startStandaloneServer } = require("@apollo/server/standalone");
3+
const mongoose = require("mongoose");
4+
const { resolvers } = require("./resolvers.js");
5+
const { typeDefs } = require("./models/typeDefs.js");
6+
7+
const MONGO_URI = "mongodb://localhost:27017/student-register";
8+
9+
// Database connection
10+
mongoose
11+
.connect(MONGO_URI, {
12+
useNewUrlParser: true,
13+
useUnifiedTopology: true,
14+
})
15+
.then(() => {
16+
console.log(`Db Connected`);
17+
})
18+
.catch(err => {
19+
console.log(err.message);
20+
});
21+
22+
const server = new ApolloServer({ typeDefs, resolvers });
23+
24+
startStandaloneServer(server, {
25+
listen: { port: 4000 },
26+
}).then(({ url }) => {
27+
console.log(`Server ready at ${url}`);
28+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const mongoose = require("mongoose");
2+
3+
const Student = mongoose.model("Student", {
4+
firstName: String,
5+
lastName: String,
6+
age: Number,
7+
});
8+
9+
module.exports = { Student };
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const gql = require("graphql-tag");
2+
3+
const typeDefs = gql`
4+
type Query {
5+
hello: String
6+
welcome(name: String): String
7+
students: [Student] #return array of students
8+
student(id: ID): Student #return student by id
9+
}
10+
type Student {
11+
id: ID
12+
firstName: String
13+
lastName: String
14+
age: Int
15+
}
16+
type Mutation {
17+
create(firstName: String, lastName: String, age: Int): Student
18+
update(id: ID, firstName: String, lastName: String, age: Int): Student
19+
delete(id: ID): Student
20+
}
21+
`;
22+
23+
module.exports = { typeDefs };
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "graphql",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node index.js",
9+
"dev": "nodemon index.js"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC",
14+
"dependencies": {
15+
"@apollo/server": "^4.4.1",
16+
"graphql": "^16.6.0",
17+
"graphql-tag": "^2.12.6",
18+
"mongoose": "^7.0.1"
19+
}
20+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { Student } = require("./models/Student.js");
2+
3+
// GraphQL Resolvers
4+
const resolvers = {
5+
Query: {
6+
hello: () => "Hello from Reflectoring Blog",
7+
welcome: (parent, args) => `Hello ${args.name}`,
8+
students: async () => await Student.find({}), // return array of students
9+
student: async (parent, args) => await Student.findById(args.id), // return student by id
10+
},
11+
Mutation: {
12+
create: async (parent, args) => {
13+
const { firstName, lastName, age } = args;
14+
const newStudent = new Student({
15+
firstName,
16+
lastName,
17+
age,
18+
});
19+
await newStudent.save();
20+
return newStudent;
21+
},
22+
update: async (parent, args) => {
23+
const { id } = args;
24+
const updatedStudent = await Student.findByIdAndUpdate(id, args);
25+
if (!updatedStudent) {
26+
throw new Error(`Student with ID ${id} not found`);
27+
}
28+
return updatedStudent;
29+
},
30+
delete: async (parent, args) => {
31+
const { id } = args;
32+
const deletedStudent = await Student.findByIdAndDelete(id);
33+
if (!deletedStudent) {
34+
throw new Error(`Student with ID ${id} not found`);
35+
}
36+
return deletedStudent;
37+
},
38+
},
39+
};
40+
41+
module.exports = { resolvers };

0 commit comments

Comments
 (0)