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/learn/BestPractice-ServingOverHTTP.md
+10-9Lines changed: 10 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -3,20 +3,21 @@ title: Serving over HTTP
3
3
layout: ../_core/DocsLayout
4
4
category: Best Practices
5
5
permalink: /learn/serving-over-http/
6
+
next: /learn/authorization/
6
7
---
7
8
8
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.
9
10
10
-
###Web Request Pipeline
11
+
## Web Request Pipeline
11
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.
12
13
13
-
###URIs, Routes
14
+
## URIs, Routes
14
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.
15
16
16
-
###HTTP Methods, Headers, and Body
17
+
## HTTP Methods, Headers, and Body
17
18
Your GraphQL HTTP server should handle the HTTP GET and POST methods.
18
19
19
-
####GET request
20
+
### GET request
20
21
21
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:
Query variables can be sent as a JSON-encoded string in an additional query parameter called `variables`. If the query contains several named operations, an `operationName` query parameter can be used to control which one should be executed.
38
39
39
-
####POST request
40
+
### POST request
40
41
41
42
A standard GraphQL POST request should use the `application/json` content type, and include a JSON-encoded body of the following form:
42
43
43
44
```js
44
45
{
45
46
"query":"...",
46
47
"operationName":"...",
47
-
"variables": { variable1: value, ... }
48
+
"variables": { variable1: value, ... }
48
49
}
49
50
```
50
51
@@ -57,7 +58,7 @@ In addition to the above, we recommend supporting two additional cases:
57
58
58
59
If you're using express-graphql, you already get these behaviors for free.
59
60
60
-
###Response
61
+
## Response
61
62
62
63
Regardless of the method by which the query and variables were sent, the response should be returned in the body of the request in JSON format. As mentioned in the spec, a query might result in some data and some errors, and those should be returned in a JSON object of the form:
63
64
@@ -70,7 +71,7 @@ Regardless of the method by which the query and variables were sent, the respons
70
71
71
72
If there were no errors returned, the `"errors"` field should not be present on the response. If no data is returned, [according to the GraphQL spec](http://facebook.github.io/graphql/#sec-Data), the `"data"` field should only be included if the error occurred during execution.
72
73
73
-
###GraphiQL
74
+
## GraphiQL
74
75
GraphiQL is useful during testing and development but should be disabled in production by default. If you are using express-graphql, you can toggle it based on the NODE_ENV environment variable:
If you are using NodeJS, we recommend using either [express-graphql](https://github.com/graphql/express-graphql) or [apollo-server](https://github.com/apollostack/apollo-server).
Copy file name to clipboardExpand all lines: site/learn/BestPractice-ThinkingInGraphs.md
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -3,15 +3,15 @@ title: Thinking in Graphs
3
3
layout: ../_core/DocsLayout
4
4
category: Best Practices
5
5
permalink: /learn/thinking-in-graphs/
6
-
next: /learn/authorization/
6
+
next: /learn/serving-over-http/
7
7
---
8
8
9
-
###It's Graphs All the Way Down [\*](https://en.wikipedia.org/wiki/Turtles_all_the_way_down)
9
+
## It's Graphs All the Way Down [\*](https://en.wikipedia.org/wiki/Turtles_all_the_way_down)
10
10
> With GraphQL, you model your business domain as a graph
11
11
12
12
Graphs are powerful tools for modeling many real-world phenomena because they resemble our natural mental models and verbal descriptions of the underlying process. With GraphQL, you model your business domain as a graph by defining a schema; within your schema, you define different types of nodes and how they connect/relate to one another. On the client, this creates a pattern similar to Object-Oriented Programming: types that reference other types. On the server, since GraphQL only defines the interface, you have the freedom to use it with any backend (new or legacy!).
13
13
14
-
###Shared Language
14
+
## Shared Language
15
15
> Naming things is a hard but important part of building intuitive APIs
16
16
17
17
Think of your GraphQL schema as an expressive shared language for your team and your users. To build a good schema, examine the everyday language you use to describe your business. For example, let's try to describe an email app in plain english:
@@ -50,7 +50,7 @@ fragment previewInfo on Email {
50
50
}
51
51
```
52
52
53
-
###Business Logic Layer
53
+
## Business Logic Layer
54
54
> Your business logic layer should act as the single source of truth for enforcing business domain rules
55
55
56
56
Where should you define the actual business logic? Where should you perform validation and authorization checks? The answer: inside a dedicated business logic layer. Your business logic layer should act as the single source of truth for enforcing business domain rules.
@@ -66,7 +66,7 @@ Sometimes, you will find yourself working with legacy data sources that do not p
66
66
67
67
Build your GraphQL schema to express "what" rather than "how". Then you can improve your implementation details without breaking the interface with older clients.
68
68
69
-
###One Step at a time
69
+
## One Step at a time
70
70
> Get validation and feedback more frequently
71
71
72
72
Don't try to model your entire business domain in one sitting. Rather, build only the part of the schema that you need for one scenario at a time. By gradually expanding the schema, you will get validation and feedback more frequently to steer you toward building the right solution.
GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data. GraphQL isn't tied to any specific database or storage engine and is instead backed by your existing code and data.
10
+
11
+
A GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type. For example, a GraphQL service that tells us who the logged in user is (`me`) as well as that User's name might look something like this:
12
+
13
+
```graphql
14
+
typeQuery {
15
+
me: User
16
+
}
17
+
18
+
typeUser {
19
+
id: ID
20
+
name: String
21
+
}
22
+
```
23
+
24
+
Alongwithfunctionsforeachfieldoneachtype:
25
+
26
+
```js
27
+
functionQuery_me(request) {
28
+
returnrequest.auth.user;
29
+
}
30
+
31
+
functionUser_name(user) {
32
+
returnuser.getName();
33
+
}
34
+
```
35
+
36
+
Once a GraphQL service is running (typically at a URL on a web service), it can be sent GraphQL queries to validate and execute. A received query is first checked to ensure it only refers to the types and fields defined, then runs the provided functions to produce a result.
37
+
38
+
For example the query:
39
+
40
+
```graphql
41
+
{
42
+
me {
43
+
name
44
+
}
45
+
}
46
+
```
47
+
48
+
Could produce the JSON result:
49
+
50
+
```json
51
+
{
52
+
"me": {
53
+
"name": "Luke Skywalker"
54
+
}
55
+
}
56
+
```
57
+
58
+
Learn more about GraphQL: the query language, type system, how the GraphQL service works, and best practices for using GraphQL:
59
+
60
+
## Core Concepts
61
+
62
+
Start here to get a solid understanding of GraphQL and how it works.
63
+
64
+
-[Query Language](/learn/queries/)
65
+
-[Type System](/learn/schema/)
66
+
- Validation
67
+
- Execution
68
+
- Introspection
69
+
70
+
## Best Practices
71
+
72
+
Ready to start using GraphQL? Follow these guidelines to get a better understanding of how to approach common topics.
0 commit comments