Skip to content

Commit a6f4e6c

Browse files
author
Sashko Stubailo
committed
Proposed edits to HTTP section
1 parent 0e10ab1 commit a6f4e6c

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

site/docs/BestPractice-ServingOverHTTP.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ next: /learn/authorization/
99
HTTP is the most common choice for client-server protocol when using GraphQL because of its ubiquity. Here are some guidelines for setting up a GraphQL server to operate over HTTP.
1010

1111
### Web Request Pipeline
12-
Most modern web frameworks use a pipeline model where requests are passed through a stack of middleware (AKA filters/plugins). As the request flows through the pipeline, it can be inspected, transformed, modified, or terminated with a response. GraphQL should be placed after all authentication middleware.
12+
Most modern web frameworks use a pipeline model where requests are passed through a stack of middleware (AKA filters/plugins). As the request flows through the pipeline, it can be inspected, transformed, modified, or terminated with a response. GraphQL should be placed after all authentication middleware, so that you have access to the same session and user information you would in your HTTP endpoint handlers.
1313

1414
### URIs, Routes
15-
HTTP is commonly associated with REST, which uses "resources" as its core concept. In contrast, GraphQL's conceptual model is an entity graph. As a result, entities in GraphQL are not identified by URLs. Instead, a GraphQL server operates on a single URL/endpoint and all GraphQL requests for a given graph/domain should be directed at this endpoint.
15+
HTTP is commonly associated with REST, which uses "resources" as its core concept. In contrast, GraphQL's conceptual model is an entity graph. As a result, entities in GraphQL are not identified by URLs. Instead, a GraphQL server operates on a single URL/endpoint, usually `/graphql`, and all GraphQL requests for a given service should be directed at this endpoint.
1616

1717
### HTTP Methods, Headers, and Body
1818
Your GraphQL HTTP server should handle the HTTP GET and POST methods.
1919

20+
#### GET request
21+
2022
When receiving an HTTP GET request, the GraphQL query should be specified in the "query" query string. For example, if we wanted to execute the following GraphQL query:
2123

2224
```graphql
@@ -33,7 +35,20 @@ This request could be sent via an HTTP GET like so:
3335
http://myapi/graphql?query={me{name}}
3436
```
3537

36-
When receiving an HTTP POST request, we recommend supporting these two cases:
38+
Query variables can be sent in an additional query parameter called `variables`.
39+
40+
#### POST request
41+
42+
A standard GraphQL POST request should use the `application/json` content type, and include a JSON-encoded body of the following form:
43+
44+
```js
45+
{
46+
"query": "...",
47+
"variables": { variable1: value, ... }
48+
}
49+
```
50+
51+
In addition to the above, we recommend supporting two additional cases:
3752

3853
* If the "query" query string parameter is present (as in the GET example above), it should be parsed and handled in the same way as the HTTP GET case.
3954
* If the "application/graphql" Content-Type header is present, treat the HTTP POST body contents as the GraphQL query string.

0 commit comments

Comments
 (0)