Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/content/technotes/TN0047-error-status-code-200.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
title: Error! Status Code 200?
subtitle: Granular error handling in your API
description: Learn GraphQL uniquly handles errors and how that compares to traditional REST APIs.
Comment thread
smyrick marked this conversation as resolved.
Outdated
published: 2024-02-09
id: TN0047
tags: [best-practices, error-handling]
---

In the realm of API development, HTTP status codes serve as crucial indicators of request success or failure.
However, when it comes to GraphQL, the traditional approach of using REST/HTTP error codes becomes inadequate.
GraphQL introduces a paradigm shift in how APIs are designed and operated, rendering conventional status codes insufficient for capturing the nuances of GraphQL operations, especially in the context of parallel operations.

>
> TLDR; The GraphQL 200 status codes signify the successful processing of a request at the Apollo Router level, but they do not encapsulate the outcome of individual operations or the presence of errors within a GraphQL response.
Comment thread
burn2delete marked this conversation as resolved.
Outdated
> Attempting to shoehorn HTTP error codes into the GraphQL paradigm overlooks the unique characteristics and capabilities of GraphQL, such as granular error handling, parallel execution, and asynchronous patterns.
Comment thread
smyrick marked this conversation as resolved.
Outdated
>
> You can learn more about status codes in GraphQL by reading the [GraphQL-over-HTTP](https://graphql.github.io/graphql-over-http/draft/) specification.
Comment thread
smyrick marked this conversation as resolved.
Outdated
>

## Nature of GraphQL Operations
Comment thread
smyrick marked this conversation as resolved.
Outdated

In a typical REST API, each endpoint corresponds to a specific resource or action, making it straightforward to map HTTP status codes to the outcome of each request.
However, GraphQL operates differently. Instead of multiple endpoints, GraphQL has a single endpoint that serves as an entry point for executing queries, mutations, and subscriptions.
Comment thread
burn2delete marked this conversation as resolved.
Outdated
This unified approach allows clients to request precisely the data they need in a single round trip to the server.

## Granular Error Handling

GraphQL offers granular error handling through its response structure.
In a GraphQL response, along with the data requested by the client, the server can include an array of errors, each containing detailed information about what went wrong.
Comment thread
burn2delete marked this conversation as resolved.
Outdated
This level of granularity enables clients to understand and react to errors more effectively than traditional HTTP status codes would allow.

## Parallel Execution and Partial Results
Comment thread
smyrick marked this conversation as resolved.
Outdated

One of the most powerful features of GraphQL is its ability to execute multiple operations in parallel.
This means that even if one part of a GraphQL operation fails, other parts may still succeed.
Comment thread
smyrick marked this conversation as resolved.
Outdated
In contrast, REST APIs typically follow a serial execution model, where one failed request can halt the entire process.

Therefore, using HTTP status codes to convey the outcome of parallel GraphQL operations would be impractical and misleading.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still need a section calling out when non-200s are used. The GraphQL over http spec allows it and so we don't want some one to take away from the this note that you ALWAYS have to return 200

## Significance of 200 Status Codes

In GraphQL, the presence of a 200 status code indicates that the request was successful from the Apollo Router's perspective.
This does not necessarily mean that the client received all the data it requested without any errors.
Comment thread
smyrick marked this conversation as resolved.
Outdated
Instead, it signifies that the GraphQL operation as a whole was processed without encountering any issues, regardless of the outcome of individual field resolvers.
3 changes: 2 additions & 1 deletion src/content/technotes/_redirects
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@
/TN0043 /docs/technotes/TN0043-using-graphql-for-abstraction
/TN0044 /docs/technotes/TN0044-graphql-and-rest-together
/TN0045 /docs/technotes/TN0045-router_resource_estimator
/TN0046 /docs/technotes/TN0046-managing-graph-environments-using-variants
/TN0046 /docs/technotes/TN0046-managing-graph-environments-using-variants
/TN0047 /docs/technotes/TN0047-error-status-code-200