-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
188 lines (165 loc) · 4.75 KB
/
server.js
File metadata and controls
188 lines (165 loc) · 4.75 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
require('dotenv').config();
const express = require('express');
const app = express();
const expressGraphQL = require('express-graphql').graphqlHTTP;
const PORT = process.env.PORT || 5000;
const dbConnection = process.env.DB_URL;
const axios = require('axios');
const pg = require('pg');
// GraphQL Component Imports. //
const {
GraphQLSchema,
GraphQLObjectType,
graphql,
getDescription,
GraphQLString,
GraphQLInt,
GraphQLBoolean,
GraphQLList
} = require('graphql');
//Returns an Array of Song Objects
const getSongs = async (args) => {
let songs;
const client = new pg.Client(dbConnection);
await client.connect()
.catch((err) => console.log('Error connecting to DB', err));
let query = 'Select * from songs';
const values = []
if(args.name){
query += ` where lower(name) like lower($1)`
values.push(`%${args.name}%`)
}
if(args.artist){
if(values.length > 0){
query += ` or lower(artist) like lower($2)`
values.push(`%${args.artist}%`)
}
else{
query += ` where lower(artist) like lower($1)`
values.push(`%${args.artist}%`)
}
}
await client.query(query, values)
.then((res) => { songs = res.rows; })
.then(() => client.end())
.catch((err) => console.log('Error with query', err));
return songs;
};
//Returns an Array of Level Objects
const getLevels = async (args) => {
let levels;
const client = new pg.Client(dbConnection);
await client.connect()
.catch((err) => console.log('Error connecting to DB', err));
const query = 'Select * from levels';
await client.query(query)
.then((res) => { levels = res.rows; })
.then(() => client.end())
.catch((err) =>{ console.log('Error with query', err);});
return levels;
};
// Defining Song Type //
const songType = new GraphQLObjectType({
name: 'Song',
description: 'This represents a DJ Max Respect V song',
fields: () => ({
id: { type: GraphQLInt },
name: { type: GraphQLString },
artist: { type: GraphQLString },
dlc: { type: GraphQLBoolean },
levels: {
type: GraphQLList(levelType),
resolve: async (song, args) => {
const levels = await getLevels(args);
return levels.filter(level => level.song_id === song.id) || [];
}
}
})
});
const levelType = new GraphQLObjectType({
name: 'Level',
description: 'A representation of a playthrough of a song on DJ Max Respect V. Includes: The song chosen, the button style, the difficulty mode, the amount of difficulty stars',
fields: () => ({
id: { type: GraphQLInt },
button_mode: { type: GraphQLString },
song_id: { type: GraphQLInt },
song: {
type: songType,
resolve: async (level, args) => {
const songs = await getSongs(args);
return songs.find(song => song.id === level.song_id);
}
},
difficulty: { type: GraphQLString },
star_count: { type: GraphQLInt }
})
});
// Defining RootQuery //
const rootQuery = new GraphQLObjectType({
name: 'Query',
description: 'Root Query',
fields: () => ({
// Return all songs
songs: {
type: new GraphQLList(songType),
description: 'DJ Max Respect Songs',
args: {name:{type: GraphQLString}, artist: {type: GraphQLString}},
resolve: async (parent, args) => {
const songs = getSongs(args);
return songs;
}
},
// Return all levels
levels: {
type: GraphQLList(levelType),
description: 'DJ Max Levels',
resolve: async () => {
const levels = getLevels();
return levels;
}
},
// level: {
// type: GraphQLList(levelType),
// description: 'One DJ Max Respect V level',
// args: { difficulty: { type: GraphQLString }},
// resolve: async (parent,args) => {
// let level;
// const client = new pg.Client(dbConnection);
// await client.connect()
// .catch((err) => console.log('Error connecting to DB', err));
// let query;
// let searchParam;
// if(args.difficulty){
// query = 'Select * from levels where difficulty like $1';
// }
// console.log(query)
// await client.query(query, [args.difficulty])
// .then((res)=>{level = res.rows})
// .catch((err)=>{console.log('Oops', err)})
// console.log(level)
// return level
// }
// }
})
});
// const rootMutation = new GraphQLObjectType({
// name: 'Mutation',
// description: 'Root Mutation',
// fields: ()=> ({
// })
// })
const schema = new GraphQLSchema({
query: rootQuery,
// mutation : rootMutation,
});
app.use('/graphql', expressGraphQL({
graphiql: true,
schema: schema,
}));
// app.post('/test', expressGraphQL({
// graphiql: true,
// schema: schema,
// }));
app.listen(PORT, () => {
console.log('Server up');
});