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
if (user) returnnewBuffer(email).toString('base64');
102
-
returnfalse;
103
-
},
104
99
},
105
100
```
106
101
107
-
As shown in the code above, there are three resolver functions, `bookTrip`, `cancelTrip`, and `login`.
102
+
As shown in the code above, there are two resolver functions, `bookTrip`, and `cancelTrip`.
108
103
109
104
The `bookTrip` function takes in a `launchId`, and makes a request to book a trip for that particular launch. If a trip is booked successfully, then the function retrieves the booked launch and returns an object containing a success status and message back to the client.
110
105
111
106
The `cancelTrip` function takes in a `launchId`, and makes a request via the `cancelTrip` method of the user data source to cancel a trip. If a trip is canceled successfully, then the function retrieves the launch that has been canceled and returns an object indicating a success status back to the client.
112
107
113
108
For both functions, if the request fails, then an object containing a failed success status and message is returned to the client.
114
109
115
-
The `login` function takes in an email, checks the user table in the database via the `findOrCreateUser` method to verify if the user exists or not. If the user exists or a new user is created, return a `base64` encoding of the user's email.
116
-
117
-
The `base64` encoding of the user's detail is for obscuring the data. The result is a form of unique string token which we use for authentication.
118
-
119
110
Now, we need to add a resolver function to take care of the booked status on a launch. Copy the code below and paste it just after the `Mutation` resolver functions.
120
111
121
112
_src/resolvers.js_
@@ -147,7 +138,7 @@ Mission: {
147
138
},
148
139
```
149
140
150
-
The `missionPatch` function checks wether a certain patch size, either `SMALL` OR `LARGE` was passed as an argument. Based on the argument value, the appropriate mission patch is then returned as part of the Launch to the client.
141
+
The `missionPatch` function checks wether a certain patch size, either `SMALL` OR `LARGE` was passed as an argument. Based on the argument's value, the appropriate mission patch is then returned as part of the launch to the client.
151
142
152
143
Now, let's implement the `User` resolver functions.
153
144
@@ -204,6 +195,27 @@ A response like this should be returned at the right side of the playground.
204
195
205
196

206
197
198
+
Let's write a launch query that accepts an argument. Copy the query below and paste it in the playground. Hit the Play button to get a response.
199
+
200
+
```js
201
+
query {
202
+
launch(id:60) {
203
+
id
204
+
year
205
+
rocket {
206
+
id
207
+
name
208
+
type
209
+
}
210
+
}
211
+
}
212
+
```
213
+
214
+
A response like the one shown below should be returned at the right side of the playground
215
+
216
+

217
+
218
+
207
219
<h2id="pagination">Pagination</h2>
208
220
209
221
The `launches` query returned a large data set of launches. This data set contains too much data to be fetched all at once.
@@ -252,49 +264,17 @@ type LaunchConnection {
252
264
253
265
The `launches` field takes in two parameters, `pageSize` and `after`. `pageSize` refers to the number of items to show at once while `after` refers to the cursor that keeps track of where the next set of data should be fetched from. We created a `LaunchConnection` type that returns a result that shows the list of launches with a `cursor` field and a `hasMore` field to indicate if there's more data to be fetched.
254
266
255
-
Copy the pagination helper code below and paste it in the `src/utils.js` file.
256
-
257
-
_src/utils.js_
258
-
259
-
```js
260
-
module.exports.paginateResults= ({
261
-
after: cursor,
262
-
pageSize =20,
263
-
results,
264
-
// can pass in a function to calculate an item's cursor
265
-
getCursor= () =>null,
266
-
}) => {
267
-
if (pageSize <1) return [];
268
-
if (!cursor) returnresults.slice(0, pageSize);
269
-
270
-
constcursorIndex=results.findIndex(item=> {
271
-
// if an item has a `cursor` on it, use that, otherwise try to generate one
272
-
let itemCursor =item.cursor?item.cursor:getCursor(item);
273
-
// if there's still not a cursor, return false by default
274
-
return itemCursor ? cursor === itemCursor :false;
275
-
});
276
-
277
-
return cursorIndex >=0
278
-
? cursorIndex ===results.length-1// don't let us overflow
Open up the `src/utils.js` file in the repo you cloned in the previous section and check out the `paginateResults` function.
288
268
289
-
The code above is a helper function for paginating data from the server.Now, let's update the necessary resolver functions to accomodate pagination.
269
+
The `paginateResults` function in the file is a helper function for paginating data from the server.Now, let's update the necessary resolver functions to accomodate pagination.
290
270
291
271
First, copy the code below and add it to the top of the `src/resolvers.js` file.
292
272
293
273
```js
294
274
const { paginateResults } =require('./utils');
295
275
```
296
276
297
-
We required the `paginateResults` function from the `src/utils.js` file. Now, update the `launches` resolver function in the `src/resolvers.js` file with the code below:
277
+
Now that we have required the `paginateResults` function from the `src/utils.js` file. Let's update the `launches` resolver function in the `src/resolvers.js` file with the code below:
298
278
299
279
_src/resolvers.js_
300
280
@@ -368,7 +348,7 @@ In the code above, the context function extracts the value of the `authorization
368
348
369
349
If the user exists, a context object containing the logged-in user is returned to the resolver functions.
370
350
371
-
How do we create the token passed to the `authorization` headers? Check out the Query `login` resolver function again.
351
+
How do we create the token passed to the `authorization` headers? Add a `login` resolver function to the `Mutation` type in the `resolvers.js` file.
0 commit comments