DEV Community

Discussion on: You're Not Using HTTP Status Codes Right

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

I use HTTP 200 everywhere because Facebook says it's the best practice now #graphql

Collapse
 
luiz0x29a profile image
Real AI

eeew

Collapse
 
dendihandian profile image
Dendi Handian

Source please?

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard • Edited

If you are doing REST, you can ignore my snarky comment and read the fine article instead.

For GraphQL specifically, Facebook took the somewhat controversial decision to throw away all HTTP codes and verbs and server-side caching mechanisms. Everything is a POST { "query": "graphql query", variables: { json } } to example.com/graphql and returns HTTP 200 OK. Exceptions and failures are treated via another mechanism. Caching... who needs caching?

See for example 200 OK! Error Handling in GraphQL and serving GraphQL over HTTP

Thread Thread
 
pragativerma18 profile image
Pragati Verma

Thanks for sharing this @jmfayard, I have only worked with REST APIs, so this is mostly about it, but I am glad that now the readers can understand the context for GraphQL from your comment as well.

Thread Thread
 
joelbonetr profile image
JoelBonetR πŸ₯‡

@jmfayard isn't a world without errors the one we want? πŸ˜‚
At the end is a design decision and you can use it or not. Apollo sure has some errors available. Got your point anyway 😁

Thread Thread
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard • Edited

I think the way graphql handle errors is surprising at first but fine in the end.
On the other hand the decision to just disregard the built-in HTTP server side caching is IMHO very questionable.
Depending on your use case, that may be a reason to prefer to stick to REST.

Thread Thread
 
joelbonetr profile image
JoelBonetR πŸ₯‡
On the other hand the decision to just disregard the built-in HTTP server side caching is IMHO very questionable.

In an endpoint-based API, clients can use HTTP caching to easily avoid refetching resources, and for identifying when two resources are the same.
The URL in these APIs is a globally unique identifier that the client can leverage to build a cache.
In GraphQL, though, there's no URL-like primitive that provides this globally unique identifier for a given object. It's hence a best practice for the API to expose such an identifier for clients to use.
source

You can extend that to specific fields easily instead caching the whole response, see example