You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: site/docs/BestPractice-ServingOverHTTP.md
+18-3Lines changed: 18 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,14 +9,16 @@ next: /learn/authorization/
9
9
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.
10
10
11
11
### 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.
13
13
14
14
### 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/endpointand 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.
16
16
17
17
### HTTP Methods, Headers, and Body
18
18
Your GraphQL HTTP server should handle the HTTP GET and POST methods.
19
19
20
+
#### GET request
21
+
20
22
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:
21
23
22
24
```graphql
@@ -33,7 +35,20 @@ This request could be sent via an HTTP GET like so:
33
35
http://myapi/graphql?query={me{name}}
34
36
```
35
37
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:
37
52
38
53
* 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.
39
54
* If the "application/graphql" Content-Type header is present, treat the HTTP POST body contents as the GraphQL query string.
0 commit comments