This repository was archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
[TN] Error Status Code 200 #763
Open
burn2delete
wants to merge
12
commits into
main
Choose a base branch
from
technote-status-codes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c4f8ba0
Create TN0047-error-status-code-200.mdx
burn2delete 6980486
Update _redirects
burn2delete cc4b570
wip
burn2delete 347ab01
Update TN0047-error-status-code-200.mdx
burn2delete 66d6440
Update src/content/technotes/TN0047-error-status-code-200.mdx
burn2delete fa733e6
Update src/content/technotes/TN0047-error-status-code-200.mdx
burn2delete eb99022
Update src/content/technotes/TN0047-error-status-code-200.mdx
burn2delete e1a4309
Update TN0047-error-status-code-200.mdx
burn2delete e79ab22
Merge branch 'main' into technote-status-codes
burn2delete b808e4a
Merge branch 'main' into technote-status-codes
burn2delete a9d3af8
Merge branch 'main' into technote-status-codes
burn2delete 153ee1f
Apply suggestions from code review
smyrick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
title: Error! Status Code 200? | ||
subtitle: Granular error handling in your API | ||
description: Learn how GraphQL uniquely handles errors and how that compares to traditional REST APIs. | ||
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;** In GraphQL, the presence of a 200 status code indicates that the request was successful from the Apollo Router's perspective, but it doesn't necessarily mean that the client received all the data it requested without any errors. | ||
|
||
The GraphQL 200 status codes don't encapsulate the outcome of individual fields or the presence of errors within the GraphQL response. Rather, they signify that the client has sent a request, the operation is valid GraphQL against the schema, and the GraphQL server was able to return a spec-compliant GraphQL response. | ||
> | ||
> Shoehorning 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. | ||
> | ||
> To learn more about GraphQL status codes, see the [GraphQL-over-HTTP](https://graphql.github.io/graphql-over-http/draft/) specification. | ||
> | ||
|
||
## Nature of GraphQL operations | ||
|
||
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 many different queries, mutations, and subscriptions. You can even execute more then one query in a single request. | ||
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. | ||
A GraphQL response contains two key fields: | ||
- The `data` field represents all the information the server could respond with that matches the clients request. | ||
- The `errors` field is an array containing detailed information about what may have gone wrong when making the request. | ||
|
||
A server returns a response in this shape, even if it only contains the `errors` field. | ||
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 | ||
|
||
One of the most powerful features of GraphQL is its ability to execute multiple operations in parallel: if one part of a GraphQL operation fails, other parts may still succeed. | ||
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. | ||
|
||
## Summary | ||
|
||
In GraphQL, the presence of a 200 status code indicates that the request was successful from the Apollo Router's perspective. | ||
This doesn't necessarily mean that the client received all the data it requested without any errors. | ||
Instead, it signifies that the GraphQL operation as a whole was processed without encountering any issues, regardless of the outcome of individual field resolvers. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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