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: docs/source/tutorial/resolvers.md
+114-2Lines changed: 114 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,9 +7,121 @@ It's time to finally leverage all the data sources logic in our graph's resolver
7
7
8
8
<h2id="data-sources">Call your data sources in resolvers</h2>
9
9
10
-
In the previous section of this tutorial, two data source classes were created. We'll be able to access those data sources in our resolvers, however, we need
10
+
In the previous section of this tutorial, two data source classes were created and passed as options to the `ApolloServer` constructor. We'll access those data sources in our resolvers soon!
11
11
12
-
Create a `resolvers.js` file within the `src` directory.
12
+
Resolver functions accepts four arguments:
13
+
14
+
```js
15
+
fieldName(parent, args, context, info) {
16
+
// implementation
17
+
}
18
+
```
19
+
20
+
***parent**: This is an object that contains the result returned from the resolver on the parent field.
21
+
***args**: This is an object that contains the field arguments specified in a query.
22
+
***context**: This is an object shared by all resolvers in a particular query, and is used to contain per-request state, including authentication information, dataloader instances, and anything else that should be taken into account when resolving the query.
23
+
***info**: This argument has information about the execution state of the query. Mostly used in advanced cases.
24
+
25
+
Create a `resolvers.js` file in the `src` directory. Copy the code below and paste it in the file.
The code above shows the resolver functions for the `Query` type. Let's analyze it.
45
+
46
+
The `launches` field in the schema's Query type has two arguments, `pageSize`, and `after` which are necessary for pagination. This is why they appear as the second argument in the `launches` resolver function as shown in the code above. `{ dataSources }` appear as the third argument because Apollo Server puts them on the context for every request.
47
+
48
+
In the body of the `launches` async resolver function, a request is made to fetch all launches via the `getAllLaunches` method on the `LaunchAPI` data source.
49
+
50
+
The `launch` async function takes in an `id` as the second argument which represents a `launchId`. A request is made to fetch a launch by calling the `getLaunchById` method of the `launchAPI` data source class.
51
+
52
+
The `me` async function // Wait for the code to be stable.
53
+
54
+
Copy the code below for the `Mutation` type and paste it just after the `Query` resolver functions in the file.
0 commit comments