Skip to content

Commit da3f8e3

Browse files
unicodeveloperpeggyrayzis
authored andcommitted
Add content for isBooked resolver function
1 parent 0c20169 commit da3f8e3

1 file changed

Lines changed: 52 additions & 10 deletions

File tree

docs/source/tutorial/resolvers.md

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,36 +65,78 @@ _src/resolvers.js_
6565
```js
6666
...
6767
Mutation: {
68-
bookTrip: async (_, { launchId }, { dataSources }) =>
69-
dataSources.userAPI.bookTrip({ launchId }),
70-
cancelTrip: async (_, { launchId }, { dataSources }) =>
71-
dataSources.userAPI.cancelTrip({ launchId }),
68+
bookTrip: async (_, { launchId }, { dataSources }) => {
69+
const result = await dataSources.userAPI.bookTrip({ launchId });
70+
if (!result)
71+
return {
72+
success: false,
73+
message: 'failed to book trip',
74+
};
75+
76+
const launch = await dataSources.launchAPI.getLaunchById({ launchId });
77+
return {
78+
success: true,
79+
message: 'trip booked',
80+
launch,
81+
};
82+
},
83+
cancelTrip: async (_, { launchId }, { dataSources }) => {
84+
const result = dataSources.userAPI.cancelTrip({ launchId });
85+
86+
if (!result)
87+
return {
88+
success: false,
89+
message: 'failed to cancel trip',
90+
};
91+
92+
const launch = await dataSources.launchAPI.getLaunchById({ launchId });
93+
return {
94+
success: true,
95+
message: 'trip cancelled',
96+
launch,
97+
};
98+
},
7299
login: async (_, { email }, { dataSources }) => {
73100
const user = await dataSources.userAPI.findOrCreateUser({ email });
74101
if (user) return new Buffer(email).toString('base64');
75102
return false;
76103
},
77-
}
104+
},
78105
```
79106

80107
As shown in the code above, there are three resolver functions, `bookTrip`, `cancelTrip`, and `login`.
81108

82-
The `bookTrip` function takes in a `launchId`, and makes a request to book a trip for that particular launch.
109+
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+
111+
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.
83112

84-
The `cancelTrip` function takes in a `launchId`, and makes a request via the `cancelTrip` method of the user data source to cancel a trip.
113+
For both functions, if the request fails, then an object containing a failed success status and message is returned to the client.
85114

86115
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.
87116

88117
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.
89118

90-
Now, let's get to the `User` resolver functions.
91-
92-
Copy the code below and paste it just after the `Mutation` resolver functions.
119+
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.
93120

94121
_src/resolvers.js_
95122

96123
```js
97124
...
125+
Launch: {
126+
isBooked: async (launch, _, { dataSources }) =>
127+
dataSources.userAPI.isBookedOnLaunch({ launchId: launch.id }),
128+
}
129+
```
130+
131+
The `isBooked` function makes a request to the `isBookedOnLaunch` method of the `UserAPI` datasource class with the id of a launch. This request confirms whether the launch has been booked by the logged-in user.
132+
133+
Now, let's implement the `User` resolver functions.
134+
135+
Copy the code below and paste it just after the `Launch` resolver function.
136+
137+
_src/resolvers.js_
138+
139+
```js
98140
...
99141
User: {
100142
trips: async (_, __, { dataSources }) => {

0 commit comments

Comments
 (0)