DEV Community

Avinash Maurya
Avinash Maurya

Posted on

CRUD in GraphQL MongoDB

CRUD(Create, Read, Update, Delete) operations in GraphQL, MongoDB, and PostgreSQL (using pg-promise) based on the typical SQL employees table.

GraphQL:

  1. Create (Mutation):
mutation {
  createEmployee(input: { name: "John Doe", department: "IT", hireDate: "2022-03-01" }) {
    id
    name
    department
    hireDate
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. Read (Query):
query {
  employees {
    id
    name
    department
    hireDate
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. Update (Mutation):
mutation {
  updateEmployee(id: "123", input: { name: "Updated Name" }) {
    id
    name
    department
    hireDate
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. Delete (Mutation):
mutation {
  deleteEmployee(id: "123") {
    id
    name
    department
    hireDate
  }
}
Enter fullscreen mode Exit fullscreen mode

MongoDB:

// Assuming you have a MongoDB connection and a collection named 'employees'

// Create (Insert)
db.employees.insertOne({
  name: "John Doe",
  department: "IT",
  hireDate: new Date("2022-03-01")
});

// Read (Find)
const allEmployees = db.employees.find().toArray();

// Update (UpdateOne)
db.employees.updateOne(
  { _id: ObjectId("employee_id_here") },
  { $set: { name: "Updated Name" } }
);

// Delete (DeleteOne)
db.employees.deleteOne({ _id: ObjectId("employee_id_here") });
Enter fullscreen mode Exit fullscreen mode

PostgreSQL (pg-promise):

// Assuming you have a pg-promise connection and a table named 'employees'

// Create (Insert)
const newEmployee = await db.one(
  'INSERT INTO employees(name, department, hire_date) VALUES($1, $2, $3) RETURNING *',
  ["John Doe", "IT", new Date("2022-03-01")]
);

// Read (Select)
const allEmployees = await db.any('SELECT * FROM employees');

// Update (Update)
const updatedEmployee = await db.one(
  'UPDATE employees SET name = $1 WHERE id = $2 RETURNING *',
  ["Updated Name", 123]
);

// Delete (Delete)
const deletedEmployee = await db.one(
  'DELETE FROM employees WHERE id = $1 RETURNING *',
  [123]
);
Enter fullscreen mode Exit fullscreen mode

These examples demonstrate basic CRUD operations in GraphQL, MongoDB, and PostgreSQL. Adapt the queries and mutations based on your actual GraphQL schema and database schema. Additionally, ensure you have proper error handling and input validation in your actual implementation.

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay