Skip to content

Commit 00bd059

Browse files
rootJohnny-Q
root
andcommitted
set up querying and mutations and MySQL queries
Co-authored-by: Johnny <[email protected]>
1 parent 0b401a7 commit 00bd059

15 files changed

+4340
-57
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
2-
package-lock.json
2+
package-lock.json
3+
nohup.out
4+
build

.vscode/settings.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
123,125
1+
{
2+
"extensions.ignoreRecommendations": true,
3+
"files.autoSave": "off"
4+
}

build/index.js

-23
This file was deleted.

client.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const ws = require("ws");
2+
3+
var asdf = new ws("http://api.osn-reo.org:5000");
4+

mysql_sample_db/mysqlsampledatabase.sql

+4,065
Large diffs are not rendered by default.

mysql_sample_db/sample_db.zip

53.1 KB
Binary file not shown.

package.json

+13-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@
44
"description": "API routing for mobile app",
55
"main": "index.js",
66
"dependencies": {
7-
"@types/node": "^14.0.27",
8-
"express": "^4.17.1",
9-
"express-graphql": "^0.11.0",
7+
"apollo-datasource": "^0.7.2",
8+
"apollo-datasource-rest": "^0.9.3",
9+
"apollo-server": "^2.16.1",
1010
"firebase-admin": "^9.0.0",
11-
"graphql": "^15.3.0"
11+
"google-auth-library": "^6.0.6",
12+
"graphql": "^15.3.0",
13+
"mysql": "^2.18.1",
14+
"ws": "^7.3.1"
15+
},
16+
"devDependencies": {
17+
"@types/graphql": "^14.5.0",
18+
"@types/node": "^14.0.27"
1219
},
13-
"devDependencies": {},
1420
"scripts": {
15-
"test": "echo \"Error: no test specified\" && exit 1"
21+
"test": "echo \"Error: no test specified\" && exit 1",
22+
"start": "node build/server.js"
1623
},
1724
"repository": {
1825
"type": "git",

src/index.ts

-26
This file was deleted.

src/resolvers.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { rootCertificates } from "tls";
2+
3+
export {};
4+
5+
6+
module.exports = {
7+
UserInterface: {
8+
__resolveType(user, context, info){
9+
if(user.isAvailable){
10+
return 'Tutor'
11+
}
12+
else return 'Student'
13+
}
14+
},
15+
16+
Query:{
17+
hello: ()=>{
18+
return "Hello World!";
19+
},
20+
getUserById: (_, { id }, { dataSources })=> dataSources.db.getUserById(id),
21+
getMessageById: (_, { id })=>{
22+
var messages = {
23+
1:{
24+
"id": 1,
25+
"author": {
26+
"id": 1,
27+
"displayName": "johney"
28+
},
29+
"content":"hello world",
30+
"timestamp": "8/11/2020"
31+
},
32+
2:{
33+
"id": 1,
34+
"author": {
35+
"id": 2,
36+
"displayName": "bort"
37+
},
38+
"content":"hello world",
39+
"timestamp": "8/11/2020"
40+
}
41+
}
42+
return messages[id];
43+
}
44+
},
45+
Mutation:{
46+
addUser: (_, {displayName }, { dataSources })=> dataSources.db.addUser(displayName)
47+
}
48+
}

src/schema.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export {};
2+
const { gql } = require("apollo-server");
3+
4+
const typeDefs = gql`
5+
interface UserInterface{
6+
id: Int!
7+
displayName: String!
8+
}
9+
10+
type User{
11+
id: Int!
12+
displayName: String!
13+
}
14+
15+
type Tutor implements UserInterface{
16+
id: Int!
17+
displayName: String!
18+
tutoredSubjects: [String!]!
19+
isAvailable: Boolean!
20+
}
21+
22+
type Student implements UserInterface{
23+
id: Int!
24+
displayName: String!
25+
}
26+
27+
type Message{
28+
id: Int!
29+
author: UserInterface!
30+
content: String!
31+
timestamp: String!
32+
}
33+
34+
type Channel{
35+
id: ID!
36+
users: [UserInterface!]!
37+
messages: [Message!]!
38+
}
39+
40+
type RequestTutorReturn{
41+
requestId: ID!
42+
assignedTutor: Tutor!
43+
}
44+
45+
type Query{
46+
requestTutor(reqBy: ID!, subject: String!, timestamp: String!): RequestTutorReturn
47+
getUserById(id: Int!): UserInterface
48+
getMessageById(id: Int!): Message
49+
hello: String
50+
}
51+
52+
type Mutation{
53+
deleteMessageById(id: ID!): Int!
54+
addUser(displayName: String!): UserInterface
55+
}
56+
`;
57+
58+
module.exports = typeDefs;

src/server.ts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
export {};
2+
const { ApolloServer } = require("apollo-server");
3+
const mysql = require('mysql');
4+
const typeDefs = require("./schema");
5+
const resolvers = require("./resolvers");
6+
const SQLSource = require("./sql_datasource");
7+
const fs = require("fs");
8+
const ws = require("ws");
9+
const socket_server_port = 5000;
10+
//const dbSource = require('./db')
11+
12+
//websocket server stuff
13+
14+
15+
16+
//apollo graph ql stuff
17+
var db = createStore();
18+
19+
const server = new ApolloServer({
20+
typeDefs,
21+
resolvers,
22+
dataSources: ()=>({
23+
"db": new SQLSource(db)
24+
}),
25+
context: ({req})=>{
26+
if(req.headers.authorization != "bread123") throw new Error("Unauthorized");
27+
}
28+
});
29+
30+
function createStore(){
31+
var db = mysql.createConnection({
32+
host: 'localhost',
33+
user: 'root',
34+
password: 'bread123',
35+
database: 'test_db',
36+
insecureAuth: true,
37+
multipleStatements: true
38+
})
39+
db.connect(function(err){
40+
if(err) throw err;
41+
console.log('Connected Successfully')
42+
});
43+
fs.readdir("./stored_procedures/", (err, files)=>{
44+
if(err) throw err;
45+
files.forEach(file => {
46+
fs.readFile('./stored_procedures/'+file, 'utf-8', async function(err, contents){
47+
if(err) throw err;
48+
new Promise((resolve, reject)=>{
49+
db.query(contents), function(err, res, fields){
50+
if(err) reject(err);
51+
resolve();
52+
}
53+
})
54+
})
55+
});
56+
})
57+
return db;
58+
}
59+
60+
61+
server.listen({port: 80}).then(({ url }: any)=>{
62+
console.log(`Server started on ${url}`);
63+
});

src/sql_datasource.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { exception } from "console";
2+
3+
export {};
4+
const { DataSource } = require("apollo-datasource");
5+
const mysql = require('mysql');
6+
7+
class SQLSource extends DataSource {
8+
constructor(db){
9+
super();
10+
this.store = db;
11+
this.connected = [];
12+
console.log('done');
13+
}
14+
initialize (config){
15+
this.context = config.context;
16+
}
17+
async getUserById(id){
18+
try{
19+
var queryReturn = await this.query("select * from users where id = " + id);
20+
return queryReturn[0];
21+
}catch(err){
22+
//if promise rejects;
23+
}
24+
25+
}
26+
27+
async addUser(displayName){
28+
var queryReturn = await this.query(`call insert_user("?")`, [ displayName ]);
29+
console.log(queryReturn);
30+
return queryReturn[0][0];
31+
}
32+
33+
query(selector, params=[]){
34+
return new Promise((resolve, reject)=>{
35+
this.store.query(selector, params, function(err, res, fields){
36+
if(err) reject(err);
37+
resolve(res);
38+
});
39+
});
40+
}
41+
42+
addConnectedWs(ws: WebSocket){
43+
this.connected.push(ws);
44+
}
45+
}
46+
47+
module.exports = SQLSource;

src/websocket.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
export {};
3+
const ws = require("ws");
4+
const socket_server_port = 5000;
5+
6+
const wss= new ws.Server({
7+
"port": socket_server_port
8+
});
9+
10+
var connected = {
11+
//id: websocket connection
12+
};
13+
wss.on("connection", (ws: WebSocket, req: Request)=>{
14+
var id = req.body.id;
15+
connected[id] = ws;
16+
console.log("websocket connected");
17+
});
18+
19+
function sendMessage(id, message){
20+
connected[id].sendMessage(message);
21+
}
22+
23+
module.exports = {
24+
25+
};
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
create table if not exists users(
2+
id int auto_increment primary key,
3+
displayName VARCHAR(255) not null
4+
);

stored_procedures/insert_user.sql

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
drop procedure if exists insert_user;
2+
create procedure insert_user(in p_displayName varchar(255))
3+
begin
4+
insert into users (displayName) values (p_displayName);
5+
select * from users where id = last_insert_id();
6+
end

0 commit comments

Comments
 (0)