Compare commits
1 Commits
master
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
149874b9d4 |
@@ -1 +0,0 @@
|
||||
### NBuild Awesome CRUD APIs Using Apollo Server(Graphql), MongoDB and Node.Js Code Example
|
||||
@@ -1,28 +0,0 @@
|
||||
const { ApolloServer } = require("@apollo/server");
|
||||
const { startStandaloneServer } = require("@apollo/server/standalone");
|
||||
const mongoose = require("mongoose");
|
||||
const { resolvers } = require("./resolvers.js");
|
||||
const { typeDefs } = require("./models/typeDefs.js");
|
||||
|
||||
const MONGO_URI = "mongodb://localhost:27017/student-register";
|
||||
|
||||
// Database connection
|
||||
mongoose
|
||||
.connect(MONGO_URI, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
})
|
||||
.then(() => {
|
||||
console.log(`Db Connected`);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err.message);
|
||||
});
|
||||
|
||||
const server = new ApolloServer({ typeDefs, resolvers });
|
||||
|
||||
startStandaloneServer(server, {
|
||||
listen: { port: 4000 },
|
||||
}).then(({ url }) => {
|
||||
console.log(`Server ready at ${url}`);
|
||||
});
|
||||
@@ -1,9 +0,0 @@
|
||||
const mongoose = require("mongoose");
|
||||
|
||||
const Student = mongoose.model("Student", {
|
||||
firstName: String,
|
||||
lastName: String,
|
||||
age: Number,
|
||||
});
|
||||
|
||||
module.exports = { Student };
|
||||
@@ -1,23 +0,0 @@
|
||||
const gql = require("graphql-tag");
|
||||
|
||||
const typeDefs = gql`
|
||||
type Query {
|
||||
hello: String
|
||||
welcome(name: String): String
|
||||
students: [Student] #return array of students
|
||||
student(id: ID): Student #return student by id
|
||||
}
|
||||
type Student {
|
||||
id: ID
|
||||
firstName: String
|
||||
lastName: String
|
||||
age: Int
|
||||
}
|
||||
type Mutation {
|
||||
create(firstName: String, lastName: String, age: Int): Student
|
||||
update(id: ID, firstName: String, lastName: String, age: Int): Student
|
||||
delete(id: ID): Student
|
||||
}
|
||||
`;
|
||||
|
||||
module.exports = { typeDefs };
|
||||
@@ -1,20 +0,0 @@
|
||||
{
|
||||
"name": "graphql",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node index.js",
|
||||
"dev": "nodemon index.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@apollo/server": "^4.4.1",
|
||||
"graphql": "^16.6.0",
|
||||
"graphql-tag": "^2.12.6",
|
||||
"mongoose": "^7.0.1"
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
const { Student } = require("./models/Student.js");
|
||||
|
||||
// GraphQL Resolvers
|
||||
const resolvers = {
|
||||
Query: {
|
||||
hello: () => "Hello from Reflectoring Blog",
|
||||
welcome: (parent, args) => `Hello ${args.name}`,
|
||||
students: async () => await Student.find({}), // return array of students
|
||||
student: async (parent, args) => await Student.findById(args.id), // return student by id
|
||||
},
|
||||
Mutation: {
|
||||
create: async (parent, args) => {
|
||||
const { firstName, lastName, age } = args;
|
||||
const newStudent = new Student({
|
||||
firstName,
|
||||
lastName,
|
||||
age,
|
||||
});
|
||||
await newStudent.save();
|
||||
return newStudent;
|
||||
},
|
||||
update: async (parent, args) => {
|
||||
const { id } = args;
|
||||
const updatedStudent = await Student.findByIdAndUpdate(id, args);
|
||||
if (!updatedStudent) {
|
||||
throw new Error(`Student with ID ${id} not found`);
|
||||
}
|
||||
return updatedStudent;
|
||||
},
|
||||
delete: async (parent, args) => {
|
||||
const { id } = args;
|
||||
const deletedStudent = await Student.findByIdAndDelete(id);
|
||||
if (!deletedStudent) {
|
||||
throw new Error(`Student with ID ${id} not found`);
|
||||
}
|
||||
return deletedStudent;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = { resolvers };
|
||||
@@ -11945,9 +11945,9 @@
|
||||
}
|
||||
},
|
||||
"minimist": {
|
||||
"version": "1.2.5",
|
||||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
|
||||
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==",
|
||||
"version": "1.2.8",
|
||||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz",
|
||||
"integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
|
||||
"dev": true
|
||||
},
|
||||
"minipass": {
|
||||
|
||||
Reference in New Issue
Block a user