Skip to content

Commit 16987d6

Browse files
author
kunapot
committed
move loader to folder loaders
1 parent d3cdd5c commit 16987d6

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

db.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
"userId": "rkAixaNGG",
3434
"id": "B1vOzTEfz",
3535
"credit": "rkAixaNGG"
36+
},
37+
{
38+
"url": "https://images.unsplash.com/photo-1497294815431-9365093b7331?auto=format&fit=crop&w=750&q=80&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D",
39+
"userId": "rkAixaNGG",
40+
"id": "B1vOzTEff",
41+
"credit": "rkAixaNGG"
3642
}
3743
],
3844
"posts": [

server/loaders/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const DataLoader = require('dataloader')
2+
const { fetchUsersById } = require('../apis/user')
3+
4+
const userLoader = new DataLoader(ids => Promise.all(ids.map(fetchUsersById)))
5+
6+
const loaders = {
7+
userLoader,
8+
}
9+
10+
module.exports = loaders

server/server.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ const express = require('express')
22
const bodyParser = require('body-parser')
33
const { graphqlExpress, graphiqlExpress } = require('apollo-server-express')
44
const cors = require('cors')
5-
const DataLoader = require('dataloader')
65
const schema = require('./schemas/Root')
7-
const { fetchUsersById } = require('./apis/user')
6+
const loaders = require('./loaders')
87

98
const app = express()
109
app.use(cors())
1110

12-
const userLoader = new DataLoader(ids => Promise.all(ids.map(fetchUsersById)))
13-
const loaders = {
14-
userLoader,
15-
}
16-
1711
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema, context: { loaders } }))
1812
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }))
1913
app.listen(4000, () => console.log('Now browse to localhost:4000/graphiql'))

0 commit comments

Comments
 (0)