| 1 | const { RESTDataSource } = require('apollo-datasource-rest'); |
| 2 | |
| 3 | class LaunchAPI extends RESTDataSource { |
| 4 | constructor() { |
| 5 | super(); |
| 6 | this.baseURL = 'https://api.spacexdata.com/v2/'; |
| 7 | } |
| 8 | |
| 9 | // leaving this inside the class to make the class easier to test |
| 10 | launchReducer(launch) { |
| 11 | return { |
| 12 | id: launch.flight_number || 0, |
| 13 | cursor: `${launch.launch_date_unix}`, |
| 14 | site: launch.launch_site && launch.launch_site.site_name, |
| 15 | mission: { |
| 16 | name: launch.mission_name, |
| 17 | missionPatchSmall: launch.links.mission_patch_small, |
| 18 | missionPatchLarge: launch.links.mission_patch, |
| 19 | }, |
| 20 | rocket: { |
| 21 | id: launch.rocket.rocket_id, |
| 22 | name: launch.rocket.rocket_name, |
| 23 | type: launch.rocket.rocket_type, |
| 24 | }, |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | async getAllLaunches() { |
| 29 | const response = await this.get('launches'); |
| 30 | |
| 31 | // transform the raw launches to a more friendly |
| 32 | return Array.isArray(response) |
| 33 | ? response.map(launch => this.launchReducer(launch)) : []; |
| 34 | } |
| 35 | |
| 36 | async getLaunchById({ launchId }) { |
| 37 | const res = await this.get('launches', { flight_number: launchId }); |
| 38 | return this.launchReducer(res[0]); |
| 39 | } |
| 40 | |
| 41 | async getLaunchesByIds({ launchIds }) { |
| 42 | return Promise.all( |
| 43 | launchIds.map(launchId => this.getLaunchById({ launchId })), |
| 44 | ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | module.exports = LaunchAPI; |
nothing calls this directly
no outgoing calls
no test coverage detected