-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatasource.js
36 lines (32 loc) · 1.29 KB
/
datasource.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//to make the GraphQL wrapper of the REST API, we need to work with the RESTDataSource class
const { RESTDataSource } = require('apollo-datasource-rest')
class ITunesSearchAPI extends RESTDataSource {
constructor() {
super()
//this is the base url for our API call, if you had more than one async query
//below this base would be the point where the queries diverge
this.baseURL = 'https://itunes.apple.com/search'
}
//this is our main fetch call for our iTunes query
async getITunesSearchResult({ term }) {
//use template literals so you can change the search term with each query -
//our client side application will provide the search term
const response = await this.get(`?term=${term}`)
console.log("Raw repsonse: " + response)
return this.iTunesSearchResultReducer(JSON.parse(response), term)
}
// our async call will pass data to this reducer, which will return the data
//mapped to our GraphQL schema
iTunesSearchResultReducer(response, term) {
console.log("Result count: " + response.resultCount)
return response.results.map(res => ({
id: "123",
kind: res.kind,
trackId: res.trackId,
trackName: res.trackName,
artistId: res.artistId,
artistName: res.artistName
}));
}
}
module.exports = ITunesSearchAPI