0

Is there a reason why the request to GraphQL endpoints through Javascript libraries like fetch or axios is passed as a POST request?

Technically we are just getting data from the server, so it must be a GET request. But in all the examples for axios and fetch, I see that they are passed as post requests.

1 Answers1

0

Technically we are just getting data from the server, so it must be a GET request.

Well that's only the case when you run a query, not when you run a mutation.

But more importantly, you're not just getting data from the server, you're also sending data to the server: the query document and optionally some variables and an operation name. Since GET requests ought not to have a body, POST was chosen.

That said, this is just a convention adopted by most endpoints, the GraphQL spec does not actually require it but allows any transport. You could also send the query document in a header, you could send it as part of the URL, you could send only the name of a predefined query to the server, then you could easily use GET requests - but beware of size limits and impact of caching.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375