0
0
GraphqlHow-ToBeginner · 4 min read

How to Implement Soft Delete in GraphQL: Simple Guide

To implement soft delete in GraphQL, add a boolean field like isDeleted to your data model and update your queries to filter out records where isDeleted is true. Instead of deleting records, update this field to true to mark them as deleted while keeping data intact.
📐

Syntax

Soft delete requires adding a flag field to your data type, typically a boolean like isDeleted. Your mutation to delete an item updates this flag instead of removing the record. Queries must filter out items where isDeleted is true to hide deleted data.

  • isDeleted: Boolean field indicating if the record is deleted.
  • softDeleteItem(id: ID!): Mutation to mark an item as deleted.
  • Query filters to exclude deleted items.
graphql
type Item {
  id: ID!
  name: String!
  isDeleted: Boolean!
}

type Mutation {
  softDeleteItem(id: ID!): Item
}

type Query {
  items: [Item!]!
}
💻

Example

This example shows a simple GraphQL schema and resolver logic for soft deleting an item by setting isDeleted to true. Queries only return items where isDeleted is false.

javascript
const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Item {
    id: ID!
    name: String!
    isDeleted: Boolean!
  }

  type Query {
    items: [Item!]!
  }

  type Mutation {
    softDeleteItem(id: ID!): Item
  }
`;

const items = [
  { id: '1', name: 'Item 1', isDeleted: false },
  { id: '2', name: 'Item 2', isDeleted: false }
];

const resolvers = {
  Query: {
    items: () => items.filter(item => !item.isDeleted),
  },
  Mutation: {
    softDeleteItem: (_, { id }) => {
      const item = items.find(i => i.id === id);
      if (!item) return null;
      item.isDeleted = true;
      return item;
    },
  },
};

const server = new ApolloServer({ typeDefs, resolvers });

server.listen().then(({ url }) => {
  console.log(`Server ready at ${url}`);
});
Output
Server ready at http://localhost:4000/
⚠️

Common Pitfalls

Common mistakes when implementing soft delete include:

  • Not filtering out deleted records in queries, causing deleted data to still appear.
  • Using hard delete mutations that remove data instead of marking it deleted.
  • Forgetting to update related data or cascading soft deletes if needed.
  • Not indexing the isDeleted field in databases, which can slow queries.

Always ensure your queries explicitly exclude soft deleted items and mutations only update the flag.

graphql
/* Wrong: Hard delete mutation removes item */
mutation {
  deleteItem(id: "1") {
    id
  }
}

/* Right: Soft delete mutation updates isDeleted flag */
mutation {
  softDeleteItem(id: "1") {
    id
    isDeleted
  }
}
📊

Quick Reference

StepDescription
Add isDeleted fieldAdd a boolean field to your data model to mark deletion status.
Update delete mutationChange delete mutation to set isDeleted to true instead of removing data.
Filter queriesModify queries to exclude records where isDeleted is true.
Handle related dataConsider cascading soft deletes or handling relations carefully.
Index isDeletedAdd database index on isDeleted for query performance.

Key Takeaways

Soft delete means marking data as deleted with a flag instead of removing it.
Always filter out soft deleted records in your GraphQL queries.
Update delete mutations to set the deletion flag, not remove data.
Remember to handle related data and indexing for performance.
Soft delete helps keep data history and allows recovery.