Merge pull request #273 from ajibade3210/nodeGraphql

Node graphql
This commit is contained in:
Artur Kuksin
2023-03-22 16:55:46 +01:00
committed by GitHub
6 changed files with 122 additions and 0 deletions

View File

@@ -0,0 +1 @@
### NBuild Awesome CRUD APIs Using Apollo Server(Graphql), MongoDB and Node.Js Code Example

View File

@@ -0,0 +1,28 @@
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}`);
});

View File

@@ -0,0 +1,9 @@
const mongoose = require("mongoose");
const Student = mongoose.model("Student", {
firstName: String,
lastName: String,
age: Number,
});
module.exports = { Student };

View File

@@ -0,0 +1,23 @@
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 };

View File

@@ -0,0 +1,20 @@
{
"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"
}
}

View File

@@ -0,0 +1,41 @@
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 };