-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApolloServer.js
60 lines (49 loc) · 1.18 KB
/
ApolloServer.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const { ApolloServer, gql } = require('apollo-server');
//const BoredAPI = require(".//datasource");
const typeDefs = gql`
type Event {
activity: String!
accessibility: Float!
type: String!
participants: Int!
price: Float!
key: Int!
}
type Query {
event(price: Float!): Event
events: [Event]
}
`;
const resolvers = {
Query: {
event: (root, { price }, { dataSources }) =>
dataSources.BoredAPI.getEventP(price),
events: (root, args, { dataSources }) => dataSources.BoredAPI.getRandomEvent(),
}
};
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({
BoredAPI: new BoredAPI(),
}),
});
server.listen().then(({ url }) => {
console.log("Server ready at ${url}");
});
const {RESTDataSource} = require('apollo-datasource-rest');
class BoredAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = 'https://www.boredapi.com/api/';
}
async getEvent() {
return this.get('activity');
}
async getEventP(price) {
const result = await this.get('activity', {
price
});
return result;
}
};