Skip to content

Commit a5a5289

Browse files
mandiwisebenjie
andauthored
Apply suggestions from code review
Co-authored-by: Benjie <[email protected]>
1 parent b1215ce commit a5a5289

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/pages/learn/execution.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ If a field produces a scalar value like a string or number, then the execution c
5353

5454
## Root fields and resolvers
5555

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).
5757

5858
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:
5959

@@ -65,12 +65,12 @@ function Query_human(obj, args, context, info) {
6565
}
6666
```
6767

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:
6969

7070
- `obj`: The previous object (for a field on the root `Query` type, this argument is often not used).
7171
- `args`: The arguments provided to the field in the GraphQL operation.
7272
- `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.
7474

7575
<Callout type="info">
7676
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) {
8888
}
8989
```
9090

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.
9292

9393
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.
9494

@@ -102,11 +102,11 @@ function Human_name(obj, args, context, info) {
102102
}
103103
```
104104

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.
106106

107107
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.
108108

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.
110110

111111
## Scalar coercion
112112

@@ -168,7 +168,7 @@ As we can see, each field in the nested selection sets resolves to a scalar leaf
168168
To recap what we've learned about execution:
169169

170170
- 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
172172
- Resolvers may execute asynchronously
173173
- Scalar coercion converts values into the types expected by the schema
174174
- 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

src/pages/learn/response.mdx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ The inclusion of the `data` key isn't an arbitrary decision made by the underlyi
2727

2828
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.
2929

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.
3131

3232
<Callout type="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.
3434
</Callout>
3535

3636
## Errors
@@ -51,7 +51,7 @@ operation {
5151
}
5252
```
5353

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.
5555

5656
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_.
5757

@@ -81,9 +81,9 @@ In the previous examples, we can see that when a request error occurs the `data`
8181

8282
### Field errors
8383

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.
8585

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.
8787

8888
Let's look at an example:
8989

@@ -103,18 +103,19 @@ As with network calls to any type of API, network errors that are not specific t
103103

104104
## Extensions
105105

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.
107107

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.
109109

110110
## Next steps
111111

112112
To recap what we've learned about GraphQL response formats:
113113

114114
- 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")
115116
- The `data` key contains the result of the executed operation
116117
- 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
118119
- 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
119120
- GraphQL implementations may include additional arbitrary information about the response in the `extensions` key
120121

0 commit comments

Comments
 (0)