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: src/pages/learn/execution.mdx
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,7 @@ If a field produces a scalar value like a string or number, then the execution c
53
53
54
54
## Root fields and resolvers
55
55
56
-
At the top level of every GraphQL server is an Object type that represents the possible entry points into the GraphQL API, it's often called the `query` root operation type or the `Query` type. If the API supports mutations to write data and subscriptions to fetch real-time data as well, then it will have `Mutation` and `Subscription` types that expose fields to perform these kinds of operations too. You can learn more about these types on the [Schema and Types page](/learn/schema/#the-query-mutation-and-subscription-types).
56
+
At the top level of every GraphQL server is an Object type that represents the possible entry points into the GraphQL API, it's often called "the `query` root operation type" or the `Query` type. If the API supports mutations to write data and subscriptions to fetch real-time data as well, then it will have `Mutation` and `Subscription` types that expose fields to perform these kinds of operations too. You can learn more about these types on the [Schema and Types page](/learn/schema/#the-query-mutation-and-subscription-types).
57
57
58
58
In this example, our `Query` type provides a field called `human` which accepts the argument `id`. The resolver function for this field likely accesses a database and then constructs and returns a `Human` type:
59
59
@@ -65,12 +65,12 @@ function Query_human(obj, args, context, info) {
65
65
}
66
66
```
67
67
68
-
This example is written in JavaScript, however, GraphQL servers can be built in [many different languages](/code/). The resolver function receives four arguments:
68
+
This example is written in JavaScript, however GraphQL servers can be built in [many different languages](/code/). In the reference implementation, a resolver function receives four arguments:
69
69
70
70
-`obj`: The previous object (for a field on the root `Query` type, this argument is often not used).
71
71
-`args`: The arguments provided to the field in the GraphQL operation.
72
72
-`context`: A value provided to every resolver that may hold important contextual information like the currently logged in user, or access to a database.
73
-
-`info`: A value holding field-specific information relevant to the current operation as well as the schema details. Refer to [type GraphQLResolveInfo for more details](/graphql-js/type/#graphqlobjecttype).
73
+
-`info`: generally only used in advanced use-cases, this is a value holding field-specific information relevant to the current operation as well as the schema details; refer to [type GraphQLResolveInfo](/graphql-js/type/#graphqlobjecttype) for more details.
74
74
75
75
<Callouttype="info">
76
76
Note that while a query operation could technically write data to the underlying data system during its execution, mutation operations are conventionally used for requests that produce side effects during their execution. And because mutation operations produce side effects, GraphQL implementations can be expected to execute these fields serially.
@@ -88,7 +88,7 @@ function Query_human(obj, args, context, info) {
88
88
}
89
89
```
90
90
91
-
The `context` is used to provide access to a database. Specifically, the `id` argument in the GraphQL query is used to fetch data for a user. Since loading from a database is an asynchronous operation, this returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). In JavaScript, Promises are used to work with asynchronous values, but the same concept exists in many languages, often called _Futures_, _Tasks_, or _Deferred_. When the database returns the data, we can construct and return a new `Human` object.
91
+
The `id` argument in the GraphQL query specifies the user whose data is requested, while `context` provides access to retrieve this data from a database. Since loading from a database is an asynchronous operation, this returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). In JavaScript, Promises are used to work with asynchronous values, but the same concept exists in many languages, often called _Futures_, _Tasks_, or _Deferred_. When the database returns the data, we can construct and return a new `Human` object.
92
92
93
93
Notice that while the resolver function needs to be aware of Promises, the GraphQL query does not. It simply expects the `human` field to return something that can be further resolved to a scalar `name` value. During execution, GraphQL will wait for Promises, Futures, and Tasks to be completed before continuing and will do so with optimal concurrency.
94
94
@@ -102,11 +102,11 @@ function Human_name(obj, args, context, info) {
102
102
}
103
103
```
104
104
105
-
A GraphQL server is powered by a type system that is used to determine what to do next. Even before the `human` field returns anything, GraphQL knows that the next step will be to resolve fields on the `Human` type since the type system tells it that the `human` field will return this output type.
105
+
A GraphQL server is powered by a type system that is used to determine what to do next. Even before the `human` field returns anything, GraphQL knows the next step will be to resolve fields on the `Human` type since the type system tells it that the `human` field will return this output type.
106
106
107
107
Resolving the name in this case is straightforward. The name resolver function is called and the `obj` argument is the new `Human` object returned from the previous field. In this case, we expect this object to have a `name` property, which we can read and return directly.
108
108
109
-
Many GraphQL libraries will let you omit resolvers this simple and will assume that if a resolver isn't provided for a field, a property of the same name should be read and returned.
109
+
Many GraphQL libraries let you omit resolvers this simple, assuming that if a resolver isn't provided for a field, a property of the same name should be read and returned.
110
110
111
111
## Scalar coercion
112
112
@@ -168,7 +168,7 @@ As we can see, each field in the nested selection sets resolves to a scalar leaf
168
168
To recap what we've learned about execution:
169
169
170
170
- Each field in a GraphQL type system will have a corresponding resolver function that provides data for the field from an existing data source
171
-
- Execution begins at the top-level `query`, `mutation`, and `subscription` fields
171
+
- Execution begins at the top-level `Query`, `Mutation`, or `Subscription` fields
172
172
- Resolvers may execute asynchronously
173
173
- Scalar coercion converts values into the types expected by the schema
174
174
- When a field on an Object type returns a List type of other objects, additional data may need to be fetched from the underlying data source to transform any foreign key-like references (such as IDs) into the related objects
Copy file name to clipboardExpand all lines: src/pages/learn/response.mdx
+9-8Lines changed: 9 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,10 +27,10 @@ The inclusion of the `data` key isn't an arbitrary decision made by the underlyi
27
27
28
28
One thing that the GraphQL specification doesn't require is a specific serialization format for the response. That said, responses are typically formatted as JSON, as in the example above.
29
29
30
-
Additionally, the GraphQL specification doesn't require the use of a particular transport protocol for requests either, although it is [common for HTTP to be used](/learn/serving-over-http/)with `POST` or `GET` methods for stateless query and mutations operations. Long-lived, stateful subscription operations are often supported by WebSockets or server-sent events instead.
30
+
Additionally, the GraphQL specification doesn't require the use of a particular transport protocol for requests either, although it is [common for HTTP to be used](/learn/serving-over-http/) for stateless query and mutations operations. Long-lived, stateful subscription operations are often supported by WebSockets or server-sent events instead.
31
31
32
32
<Callouttype="info">
33
-
There is [a draft GraphQL over HTTP specification](https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md) available with further guidelines for using HTTP with a GraphQL clients and servers.
33
+
There is [a draft GraphQL over HTTP specification](https://graphql.github.io/graphql-over-http/draft/) available with further guidelines for using HTTP with GraphQL clients and servers.
34
34
</Callout>
35
35
36
36
## Errors
@@ -51,7 +51,7 @@ operation {
51
51
}
52
52
```
53
53
54
-
The `message` key inside the `errors` object provides some helpful information for the developer to understand what kind of error was raised, and the `locations` key indicates where the error occurred in the document.
54
+
The `message` key inside the `errors` object provides some helpful information for the developer to understand what kind of error was raised, and the `locations` key, if present, indicates where the error occurred in the document.
55
55
56
56
Sometimes, a GraphQL request may be syntactically correct, but when the server parses and [validates](/learn/validation/) the document against the schema, it finds an issue with part of the operation and raises a _validation error_.
57
57
@@ -81,9 +81,9 @@ In the previous examples, we can see that when a request error occurs the `data`
81
81
82
82
### Field errors
83
83
84
-
Field errors are raised if something unexpected happens when a resolver function is called. For example, a resolver may provide a null value for a field that has a Non-Null output type or there may be some other internal server error during execution.
84
+
Field errors are raised if something unexpected happens during execution. For example, a resolver may raise an error directly, or may return invalid data such as a null value for a field that has a Non-Null output type.
85
85
86
-
In these cases, the `data` key will be included in the result with the `errors` key because GraphQL will try to continue execution beyond the problematic field and potentially return a partial result.
86
+
In these cases, GraphQL will attempt to continue executing the other fields and return a partial response, with the `data` key appearing alongside the `errors` key.
87
87
88
88
Let's look at an example:
89
89
@@ -103,18 +103,19 @@ As with network calls to any type of API, network errors that are not specific t
103
103
104
104
## Extensions
105
105
106
-
The final top-level key allowed by the GraphQL specification in a response is the `extentions` key. This key is reserved for GraphQL implementations to provide additional information about the response and there are no restrictions on what it may contain.
106
+
The final top-level key allowed by the GraphQL specification in a response is the `extentions` key. This key is reserved for GraphQL implementations to provide additional information about the response and though it must be an object if present, there are no other restrictions on what it may contain.
107
107
108
-
For example, some GraphQL servers may include telemetry data or information about rate limit consumption under this key. Note that what data are available in `extensions` and whether this data is available in production or development environments will depend entirely on the specific GraphQL implementation.
108
+
For example, some GraphQL servers may include telemetry data or information about rate limit consumption under this key. Note that what data is available in `extensions` and whether this data is available in production or development environments will depend entirely on the specific GraphQL implementation.
109
109
110
110
## Next steps
111
111
112
112
To recap what we've learned about GraphQL response formats:
113
113
114
114
- The GraphQL specification allows three top-level keys in a response: `data`, `errors`, and `extensions`
115
+
- At least one of `data` or `errors` will be present on the response (when it contains both it is a "partial response")
115
116
- The `data` key contains the result of the executed operation
116
117
- Information about raised errors is included in the `errors` key of the response
117
-
- Request errors (such as syntax or validation errors) are raised before execution begins so there won't be any data included in the response
118
+
- Request errors (such as syntax or validation errors) are raised before execution begins so `data` won't be included in the response
118
119
- When a field error occurs during execution, there will be a description of the issue in the `errors` key and there may be partial data included with the `data` key
119
120
- GraphQL implementations may include additional arbitrary information about the response in the `extensions` key
0 commit comments