Skip to content

Commit 5589c9d

Browse files
unicodeveloperpeggyrayzis
authored andcommitted
Add a launch query
1 parent bb6ebca commit 5589c9d

File tree

2 files changed

+27
-47
lines changed

2 files changed

+27
-47
lines changed

docs/source/images/alaunch.png

207 KB
Loading

docs/source/tutorial/resolvers.md

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,17 @@ Mutation: {
9696
launch,
9797
};
9898
},
99-
login: async (_, { email }, { dataSources }) => {
100-
const user = await dataSources.userAPI.findOrCreateUser({ email });
101-
if (user) return new Buffer(email).toString('base64');
102-
return false;
103-
},
10499
},
105100
```
106101

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`.
108103

109104
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.
110105

111106
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.
112107

113108
For both functions, if the request fails, then an object containing a failed success status and message is returned to the client.
114109

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-
119110
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.
120111

121112
_src/resolvers.js_
@@ -147,7 +138,7 @@ Mission: {
147138
},
148139
```
149140

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

152143
Now, let's implement the `User` resolver functions.
153144

@@ -204,6 +195,27 @@ A response like this should be returned at the right side of the playground.
204195

205196
![All Launches](../images/launches.png)
206197

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+
![A Launch](../images/alaunch.png)
217+
218+
207219
<h2 id="pagination">Pagination</h2>
208220

209221
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 {
252264

253265
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.
254266

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) return results.slice(0, pageSize);
269-
270-
const cursorIndex = 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
279-
? []
280-
: results.slice(
281-
cursorIndex + 1,
282-
Math.min(results.length, cursorIndex + 1 + pageSize),
283-
)
284-
: results.slice(0, pageSize);
285-
results.slice(cursorIndex >= 0 ? cursorIndex + 1 : 0, cursorIndex >= 0);
286-
};
287-
```
267+
Open up the `src/utils.js` file in the repo you cloned in the previous section and check out the `paginateResults` function.
288268

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

291271
First, copy the code below and add it to the top of the `src/resolvers.js` file.
292272

293273
```js
294274
const { paginateResults } = require('./utils');
295275
```
296276

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

299279
_src/resolvers.js_
300280

@@ -368,7 +348,7 @@ In the code above, the context function extracts the value of the `authorization
368348
369349
If the user exists, a context object containing the logged-in user is returned to the resolver functions.
370350
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.
372352
373353
_src/resolvers.js_
374354

0 commit comments

Comments
 (0)