-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (54 loc) · 1.38 KB
/
index.js
File metadata and controls
61 lines (54 loc) · 1.38 KB
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
61
const express = require('express');
const graphqlHTTP = require('express-graphql');
const { buildSchema } = require('graphql');
const db = require('./db')
const DataLoader = require('dataloader');
const userLoader = new DataLoader(async (keys) => {
const rows = await db.batchGetUsers(keys);
return rows.map(r => new User(r));
});
const followersLoader = new DataLoader(async (keys) => {
const rows = await db.batchGetFollowers(keys);
return rows.map(r => r.map(c =>new User(c)));
});
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type User {
firstName: String!
lastName: String!
followers: [User]
}
type Query {
users: [User]
}
`);
// This class implements the RandomDie GraphQL type
class User {
constructor(data) {
this.id = data.id;
this.firstName = data.firstName;
this.lastName = data.lastName;
}
async followers(a, b, c) {
return followersLoader.load(this.id);
return followers;
}
}
// The root provides the top-level API endpoints
var root = {
users: async function () {
const rows = await db.getAllUsers();
let result = rows.map(data => new User(data));
return result;
}
}
var app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
db.initialize().then(() => {
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');
});