Skip to content

Instantly share code, notes, and snippets.

@DanishSiddiq
Last active May 11, 2020 20:50
Show Gist options
  • Save DanishSiddiq/8445a8666eb012f7d357fb41772fef9f to your computer and use it in GitHub Desktop.
Save DanishSiddiq/8445a8666eb012f7d357fb41772fef9f to your computer and use it in GitHub Desktop.
Defining CRUD operations using GraphQL and Express route
const { Router } = require('express');
const express_graphql = require('express-graphql');
const { buildSchema } = require('graphql');
const studentController = require('./student.ctrl');
// GraphQL schema
const schema = buildSchema(`
scalar Date
, input SearchInput {
_id: String,
firstName: String,
lastName: String,
registrationNumber: Int,
email: String,
}
input StudentInput {
firstName: String,
lastName: String,
registrationNumber: Int,
email: String,
}
type Student {
_id: ID,
firstName: String,
lastName: String,
registrationNumber: Int,
email: String,
createdAt: Date,
updatedAt: Date
}
type mutationResult
{
code: Int!,
msg: String!
}
type Query {
find(input: SearchInput!): [Student]
findOne(input: SearchInput!): Student
}
type Mutation {
create (input: StudentInput!): Student!
update (_id: String!, input: StudentInput!): mutationResult!
delete (_id: String!): mutationResult!
}
`);
// Root resolver
const root = {
find: studentController.find,
findOne: studentController.findOne,
create: studentController.createOne,
update: studentController.updateOne,
delete: studentController.deleteOne
};
// router
const router = Router();
router.use('/api/v1/student', express_graphql({
schema: schema,
rootValue: root,
graphiql: true
}));
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment