Skip to content

Commit 8285edb

Browse files
committed
Add apollo server
1 parent 5d30e66 commit 8285edb

File tree

6 files changed

+135
-21
lines changed

6 files changed

+135
-21
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { RESTDataSource } = require('apollo-datasource-rest');
2+
const fetch = require("node-fetch");
3+
const { stations } = require('./stations.json');
4+
5+
class StationAPI extends RESTDataSource {
6+
async getAllStations() {
7+
return stations && stations.length ? stations.map(l => this.stationReducer(l)) : []
8+
}
9+
10+
stationReducer(station) {
11+
return {
12+
id: station.id,
13+
lat: station.lat,
14+
lng: station.lng,
15+
text: station.text,
16+
isFocussed: station.isFocussed,
17+
isSelected: station.isSelected
18+
}
19+
}
20+
21+
async getStationById({ stationId }) {
22+
const res = await this.get('stations', { id: stationId })
23+
return this.stationReducer(res[0])
24+
}
25+
26+
getStationsByIds({ stationIds }) {
27+
return Promise.all(
28+
stationIds.map(stationId => this.getStationById({ stationId })),
29+
)
30+
}
31+
}
32+
33+
module.exports = StationAPI
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"stations": [
3+
{ "id": "1", "lat": 52.380528, "lng": 4.900263, "text": "station 1", "isFocussed": true, "isSelected": true },
4+
{ "id": "2", "lat": 52.378851, "lng": 4.901465, "text": "station 2", "isFocussed": false, "isSelected": false },
5+
{ "id": "3", "lat": 52.374502, "lng": 4.889878, "text": "station 3", "isFocussed": false, "isSelected": false },
6+
{ "id": "4", "lat": 52.373244, "lng": 4.890908, "text": "station 4", "isFocussed": false, "isSelected": false },
7+
{ "id": "5", "lat": 52.368109, "lng": 4.889105, "text": "station 5", "isFocussed": false, "isSelected": false },
8+
{ "id": "6", "lat": 52.369000, "lng": 4.905671, "text": "station 6", "isFocussed": false, "isSelected": false }
9+
]
10+
}

packages/server/src/index.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1-
/* eslint no-console: 0 */
2-
import 'dotenv/config'
3-
import cors from 'cors'
4-
import bodyParser from 'body-parser'
5-
import express from 'express'
1+
const { ApolloServer } = require('apollo-server');
2+
const typeDefs = require('./schema');
3+
const StationAPI = require('./datasources/station');
4+
const resolvers = require('./resolvers');
65

7-
const app = express()
6+
const server = new ApolloServer({
7+
typeDefs,
8+
resolvers,
9+
dataSources: () => ({
10+
stationAPI: new StationAPI(),
11+
})
12+
});
813

9-
app.use(cors())
10-
11-
app.use(bodyParser.urlencoded({ extended: true }))
12-
13-
app.use(bodyParser.json())
14-
15-
app.get('/', (req, res) => {
16-
res.send({ 'test': '123' })
17-
})
18-
19-
const port = process.env.PORT || 3000
20-
21-
app.listen(port, () =>
22-
console.log(`Streams Server listening on port ${port}!`),
23-
)
14+
server.listen().then(({ url }) => {
15+
console.log(`Server ready at ${url}`);
16+
});

packages/server/src/resolvers.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { paginateResults } = require('./utils');
2+
3+
module.exports = {
4+
Query: {
5+
stations: async (_, { pageSize = 20, after }, { dataSources }) => {
6+
const allStations = await dataSources.stationAPI.getAllStations()
7+
allStations.reverse()
8+
9+
const stations = paginateResults({
10+
after,
11+
pageSize,
12+
results: allStations,
13+
})
14+
15+
return {
16+
stations,
17+
cursor: stations.length ? stations[stations.length - 1].cursor : null,
18+
hasMore: stations.length
19+
? stations[stations.length - 1].cursor !==
20+
allStations[allStations.length - 1].cursor
21+
: false,
22+
}
23+
}
24+
}
25+
}

packages/server/src/schema.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { gql } = require('apollo-server');
2+
3+
const typeDefs = gql`
4+
type Query {
5+
stations(
6+
pageSize: Int
7+
after: String
8+
): StationConnection!
9+
station(id: String!): Station
10+
}
11+
12+
type StationConnection {
13+
cursor: String!
14+
hasMore: Boolean!
15+
stations: [Station]!
16+
}
17+
18+
type Station {
19+
id: String!
20+
lat: Float!
21+
lng: Float!
22+
text: String!
23+
isFocussed: Boolean!
24+
isSelected: Boolean!
25+
}
26+
`
27+
module.exports = typeDefs

packages/server/src/utils.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports.paginateResults = ({
2+
after: cursor,
3+
pageSize = 20,
4+
results,
5+
getCursor = () => null,
6+
}) => {
7+
if (pageSize < 1) return []
8+
9+
if (!cursor) return results.slice(0, pageSize)
10+
const cursorIndex = results.findIndex(item => {
11+
let itemCursor = item.cursor ? item.cursor : getCursor(item)
12+
13+
return itemCursor ? cursor === itemCursor : false
14+
})
15+
16+
return cursorIndex >= 0
17+
? cursorIndex === results.length - 1
18+
? []
19+
: results.slice(
20+
cursorIndex + 1,
21+
Math.min(results.length, cursorIndex + 1 + pageSize),
22+
)
23+
: results.slice(0, pageSize)
24+
25+
results.slice(cursorIndex >= 0 ? cursorIndex + 1 : 0, cursorIndex >= 0)
26+
}

0 commit comments

Comments
 (0)