-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
72 lines (65 loc) · 1.69 KB
/
server.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
61
62
63
64
65
66
67
68
69
70
71
72
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
// Import data
const playerData = require('./data/players');
const teamsData = require('./data/teams');
const schema = buildSchema(`
type Query {
players: [Player],
player(name: String): Player,
teams: [Team],
team(name: String): Team,
},
type Player {
id: Int!,
name: String,
team: Int,
nationality: String,
}
type Team {
id: Int,
name: String,
league: String,
country: String,
players: [Player],
}
`)
// Helper functions for resolvers
const getTeamPlayers = (id) => {
return playerData.filter(player => player.team === id);
}
const getTeam = (name) => {
return teamsData.find(team => team.name === name);
}
const root = {
player: arg => playerData.find(player => player.name === arg.name),
players: () => playerData,
teams: () => {
// Return team with players
return teamsData.map(team => ({
...team,
players: getTeamPlayers(team.id),
}));
},
team: arg => {
// Find team based on argument
const { name } = arg;
const team = getTeam(name);
return {
...team,
players: getTeamPlayers(team.id),
}
},
createPlayer: (arg) => {
console.log(arg);
}
};
const app = express();
// Initalize app, graphiql is set to true to get interface on frontend
app.listen(4000, () => console.log("App running on 4000"));
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}))