Back to results for “GraphQL”
Node.js › GraphQL

What is the difference between a GraphQL Query, Mutation, and Subscription operation type?

Intermediate

Answer

A Query fetches data without modifying anything, analogous to a REST GET request. A Mutation performs a write operation that changes data (creating, updating, or deleting a resource), analogous to a REST POST/PUT/DELETE. A Subscription establishes a persistent connection (typically over WebSockets) that pushes real-time updates to the client whenever a specified event occurs on the server, with no direct REST equivalent, since REST has no standard mechanism for server-initiated updates.
type Query { posts: [Post!]! }
type Mutation { createPost(title: String!, body: String!): Post! }
type Subscription { postCreated: Post! }

# Client subscribing to real-time updates
subscription { postCreated { id title } }
Real-world example A live-blogging application uses a Query to load initial posts on page load, a Mutation for the author to publish a new post, and a Subscription that pushes each newly published post to all currently connected readers' browsers in real time without them needing to refresh.

Common follow-ups: What transport mechanism does a GraphQL Subscription typically use under the hood to push real-time data to clients?;How would you handle authorization differently for a Mutation compared to a read-only Query?