Compare commits

..

3 Commits

Author SHA1 Message Date
Artur Kuksin
a672db346c Merge pull request #273 from ajibade3210/nodeGraphql
Node graphql
2023-03-22 16:55:46 +01:00
olaoluwa ajibade
27b63f4305 node graphql code example 2023-03-13 05:39:38 +01:00
olaoluwa ajibade
b18444293b node graphql code example 2023-03-13 05:36:45 +01:00
7 changed files with 128 additions and 6 deletions

View File

@@ -6167,9 +6167,9 @@
"integrity": "sha1-s55/HabrCnW6nBcySzR1PEfgZU0="
},
"node_modules/dns-packet": {
"version": "5.4.0",
"resolved": "https://registry.npmjs.org/dns-packet/-/dns-packet-5.4.0.tgz",
"integrity": "sha512-EgqGeaBB8hLiHLZtp/IbaDQTL8pZ0+IvwzSHA6d7VyMDM+B9hgddEMa9xjK5oYnw0ci0JQ6g2XCD7/f6cafU6g==",
"version": "5.3.1",
"resolved": "https://registry.npmjs.org/dns-packet/-/dns-packet-5.3.1.tgz",
"integrity": "sha512-spBwIj0TK0Ey3666GwIdWVfUpLyubpU53BTCu8iPn4r4oXd9O14Hjg3EHw3ts2oed77/SeckunUYCyRlSngqHw==",
"dependencies": {
"@leichtgewicht/ip-codec": "^2.0.1"
},
@@ -20660,9 +20660,9 @@
"integrity": "sha1-s55/HabrCnW6nBcySzR1PEfgZU0="
},
"dns-packet": {
"version": "5.4.0",
"resolved": "https://registry.npmjs.org/dns-packet/-/dns-packet-5.4.0.tgz",
"integrity": "sha512-EgqGeaBB8hLiHLZtp/IbaDQTL8pZ0+IvwzSHA6d7VyMDM+B9hgddEMa9xjK5oYnw0ci0JQ6g2XCD7/f6cafU6g==",
"version": "5.3.1",
"resolved": "https://registry.npmjs.org/dns-packet/-/dns-packet-5.3.1.tgz",
"integrity": "sha512-spBwIj0TK0Ey3666GwIdWVfUpLyubpU53BTCu8iPn4r4oXd9O14Hjg3EHw3ts2oed77/SeckunUYCyRlSngqHw==",
"requires": {
"@leichtgewicht/ip-codec": "^2.0.1"
}

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 };