Skip to content

Commit

Permalink
added graphql server
Browse files Browse the repository at this point in the history
  • Loading branch information
olton committed Oct 7, 2023
1 parent 1b2d988 commit ae5ba4c
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 19 deletions.
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"start": "node .",
"dev": "nodemon ."
"dev": "nodemon -e js,graphql,json,pug ."
},
"repository": {
"type": "git",
Expand All @@ -16,7 +16,8 @@
"mina",
"archive",
"api",
"manager"
"manager",
"explorer"
],
"author": "Serhii Pimenov <[email protected]>",
"license": "MIT",
Expand All @@ -26,15 +27,21 @@
"homepage": "https://github.com/olton/minataur2#readme",
"dependencies": {
"@faustbrian/node-base58": "^1.0.0",
"@graphql-tools/graphql-file-loader": "^8.0.0",
"@graphql-tools/load": "^8.0.0",
"@graphql-tools/schema": "^10.0.0",
"@olton/datetime": "^3.0.2",
"esm": "^3.2.25",
"express": "^4.18.2",
"graphql": "^16.8.1",
"graphql-type-json": "^0.3.2",
"graphql-yoga": "4.0.5",
"node-fetch": "^3.3.2",
"node-html-parser": "^6.1.6",
"node-html-parser": "^6.1.10",
"pg": "^8.11.3",
"pg-cursor": "^2.10.3",
"pug": "^3.0.2",
"serve-favicon": "^2.5.0",
"ws": "^8.13.0"
"ws": "^8.14.2"
}
}
24 changes: 24 additions & 0 deletions server/GraphQL/fetcher/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
async function fetchGraphQL(endpoint, query, variables = {}) {
try {
const result = await fetch(
`${endpoint}`,
{
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
query,
variables,
})
}
)

return result.ok ? await result.json() : null
} catch (e) {
console.error("The Request to GraphQL war aborted! " + e.name + " " + e.message)
return null
}
}

export default fetchGraphQL
18 changes: 18 additions & 0 deletions server/GraphQL/resolvers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import GraphQLJSON, {GraphQLJSONObject} from "graphql-type-json";

const Query = {
hello: () => 'Hello World!'
}

const Mutation = {
}

const Subscription = {
}

const Json = {
JSON: GraphQLJSON,
JSONObject: GraphQLJSONObject,
}

export const resolvers = {Query, Mutation, Subscription, ...Json}
15 changes: 15 additions & 0 deletions server/GraphQL/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {makeExecutableSchema} from "@graphql-tools/schema";
import {loadSchemaSync} from "@graphql-tools/load";
import {GraphQLFileLoader} from "@graphql-tools/graphql-file-loader";
import {resolvers} from "./resolvers/index.js";
import path from "path";
import {fileURLToPath} from "url";

const __graph = path.dirname(fileURLToPath(import.meta.url))

export const schema = makeExecutableSchema({
typeDefs: loadSchemaSync(path.resolve(__graph, 'schemas/**/*.graphql'), {
loaders: [new GraphQLFileLoader()],
}),
resolvers
})
8 changes: 8 additions & 0 deletions server/GraphQL/schemas/root.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
scalar JSON
scalar JSONObject

schema {
query: Query
mutation: Mutation
subscription: Subscription
}
3 changes: 3 additions & 0 deletions server/GraphQL/schemas/root/mutation.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Mutation {
null: String
}
3 changes: 3 additions & 0 deletions server/GraphQL/schemas/root/query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Query {
hello: String!
}
3 changes: 3 additions & 0 deletions server/GraphQL/schemas/root/subscription.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Subscription {
null: String!
}
1 change: 0 additions & 1 deletion server/Modules/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const listen_notifies = async () => {
on_new_block(payload)
}
if (channel === 'new_user_tx_memo') {
console.log(payload)
on_new_user_tx_memo(payload)
}
})
Expand Down
35 changes: 25 additions & 10 deletions server/Modules/webserver.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import path from "path";
import http from "http";
import http, {createServer} from "http";
import express from "express";
import favicon from "serve-favicon"
import {create_websocket_server} from "./websocket.js";
import {websocket} from "./websocket.js";
import {log} from "../Helpers/log.js";
import {WebSocketServer} from "ws";
import { createYoga } from 'graphql-yoga'
import {schema} from "../GraphQL/schema.js";

const app = express()

const routes = () => {
app.use(express.static(path.join(appPath, 'Views')))
app.use(favicon(path.join(appPath, 'Views', 'favicon.ico')))
app.locals.pretty = true
app.set('views', path.resolve(appPath, 'Views'))
app.set('view engine', 'pug')

const client = {...config.client, version: packageJson.version}

app.get('/', async (req, res) => {
Expand Down Expand Up @@ -127,11 +124,29 @@ export const create_web_server = () => {
const {name, host = "localhost", port = 3000} = config.server
const httpServer = http.createServer({}, app)

app.use(express.static(path.join(appPath, 'Views')))
app.use(favicon(path.join(appPath, 'Views', 'favicon.ico')))
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.locals.pretty = true

app.set('views', path.resolve(appPath, 'Views'))
app.set('view engine', 'pug')

routes()

httpServer.listen(port, host, () => {
log(`Minataur running on http://${host}:${port} [${name}]`)
log(`Minataur WebServer is running on http://${host}:${port} [${name}]`)
})

const yoga = createYoga({schema})
const graphqlServer = createServer(yoga)
graphqlServer.listen(config.graphql.port, config.graphql.host,() => {
log(`Minataur GraphQL Server is running on http://${config.graphql.host}:${config.graphql.port}/graphql`)
})

create_websocket_server(httpServer)
globalThis.wss = new WebSocketServer({ server: httpServer })

websocket()
}
4 changes: 1 addition & 3 deletions server/Modules/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ import {
} from "./db.js";
import {ql_get_account_info, ql_get_pool, ql_get_snark_jobs} from "./graphql.js";

export const create_websocket_server = (httpServer) => {
globalThis.wss = new WebSocketServer({ server: httpServer })

export const websocket = () => {
wss.on('connection', (ws, req) => {
const ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress

Expand Down
1 change: 0 additions & 1 deletion server/Modules/whois.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const MINATAUR_ADDRESS = 'B62qrGrWgsHRp1GuHbz2YSdk7DgXTBYwaov7TwWVP58f36u
*/
export const updateWhois = async ({f1: sender_id, f2: receiver_key, f3, f4: amount}) => {
try {
console.log("Receiver Key:", receiver_key, receiver_key === MINATAUR_ADDRESS)
//if (+amount < 1) return
if (receiver_key !== MINATAUR_ADDRESS) return
const memo = decodeMemo(f3)
Expand Down
4 changes: 4 additions & 0 deletions server/Views/js/blockchain/charts.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const updateChartCoinbase = data => {
show: false
},
animations: {
enabled: false,
speed: 300
},
height: 150,
Expand Down Expand Up @@ -62,6 +63,7 @@ const updateChartTrans = data => {
show: false
},
animations: {
enabled: false,
speed: 300
},
height: 150,
Expand Down Expand Up @@ -103,6 +105,7 @@ const updateChartFee = data => {
show: false
},
animations: {
enabled: false,
speed: 300
},
height: 150,
Expand Down Expand Up @@ -144,6 +147,7 @@ const updateChartSlots = data => {
show: false
},
animations: {
enabled: false,
speed: 300
},
height: 150,
Expand Down
1 change: 1 addition & 0 deletions server/Views/js/index/charts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const lineChartOptions = {
show: false
},
animations: {
enabled: false,
speed: 300
},
height: 150,
Expand Down
4 changes: 4 additions & 0 deletions server/Views/js/transactions/charts.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const updateChartTransInBlock = data => {
show: false
},
animations: {
enabled: false,
speed: 300
},
height: 150,
Expand Down Expand Up @@ -64,6 +65,7 @@ const updateChartFees = data => {
show: false
},
animations: {
enabled: false,
speed: 300
},
height: 150,
Expand Down Expand Up @@ -106,6 +108,7 @@ const updateChartAmount = data => {
show: false
},
animations: {
enabled: false,
speed: 300
},
height: 150,
Expand Down Expand Up @@ -147,6 +150,7 @@ const updateChartStatus = data => {
show: false
},
animations: {
enabled: false,
speed: 300
},
height: 172,
Expand Down

0 comments on commit ae5ba4c

Please sign in to comment.