Skip to content

Commit 3785546

Browse files
unicodeveloperpeggyrayzis
authored andcommitted
Add boilerplate content till Jake's code is good to go
1 parent 2349643 commit 3785546

1 file changed

Lines changed: 114 additions & 2 deletions

File tree

docs/source/tutorial/resolvers.md

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,121 @@ It's time to finally leverage all the data sources logic in our graph's resolver
77

88
<h2 id="data-sources">Call your data sources in resolvers</h2>
99

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!
1111

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.
26+
27+
```js
28+
module.exports = {
29+
Query: {
30+
launches: async (root, { pageSize = 20, after }, { dataSources }) => {
31+
return dataSources.launchAPI.getAllLaunches();
32+
},
33+
launch: (root, { id }, { dataSources }) => {
34+
return dataSources.launchAPI.getLaunchById({ launchId: id });
35+
},
36+
me: async (_, __, { dataSources, user: { email } }) => {
37+
if (!email) return null;
38+
return dataSources.userAPI.findOrCreateUser({ email });
39+
},
40+
},
41+
};
42+
```
43+
44+
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.
55+
56+
```js
57+
module.exports = {
58+
Query: {
59+
...
60+
},
61+
Mutation: {
62+
bookTrip: async (root, { launchId }, { dataSources, user }) => {
63+
return dataSources.userAPI.bookTrip({
64+
userId: user.id,
65+
launchId: launchId,
66+
});
67+
},
68+
cancelTrip: async (root, { launchId }, { dataSources, user }) => {
69+
return dataSources.userAPI.cancelTrip({
70+
launchId: launchId,
71+
userId: user.id,
72+
});
73+
},
74+
login: async (root, { email }, { dataSources }) => {
75+
const user = await dataSources.userAPI.findOrCreateUser({ email });
76+
if (user) return new Buffer(email).toString('base64');
77+
return false;
78+
},
79+
}
80+
}
81+
```
82+
83+
// Explanation for Mutation resolver function here. Wait till we are sure of what we are doing in the context logic
84+
85+
Copy the code below for the `Launch` type and paste it just after the `Mutation` resolver functions.
86+
87+
```js
88+
module.exports = {
89+
...
90+
...
91+
Launch: {
92+
passengers: async (launch, _, { dataSources }) => {
93+
const res = await dataSources.userAPI.getUsersByLaunch({
94+
launchId: launch.id,
95+
});
96+
return res && res.length ? res : [];
97+
},
98+
},
99+
}
100+
```
101+
102+
// Explanation for Launch resolver function here. Wait till we are sure of what we are doing in the context logic
103+
104+
Copy the code below for the `User` type and paste it just after the `Launch` resolver function.
105+
106+
```js
107+
module.exports = {
108+
...
109+
...
110+
User: {
111+
trips: async (_, __, { dataSources, user: { id } }) => {
112+
// get ids of launches by user
113+
const launchIds = await dataSources.userAPI.getLaunchIdsByUser({
114+
userId: id,
115+
});
116+
117+
if (!launchIds.length) return [];
118+
119+
// look up those launches by their ids
120+
return (dataSources.launchAPI.getLaunchesByIds({ launchIds }) || []);
121+
},
122+
},
123+
}
124+
```
13125

14126
<h2 id="write-query">Write a query in the playground</h2>
15127

0 commit comments

Comments
 (0)