From 489b46f264150168f7b4e1392a3b963922647a51 Mon Sep 17 00:00:00 2001 From: EG-easy <1018eguchi@gmail.com> Date: Wed, 6 Oct 2021 20:00:21 +0900 Subject: [PATCH 1/4] update validator response ZZZ --- graphql/schema.graphql | 2 ++ service/Service.js | 32 +++++++++++++++++++++++++++--- service/types/validatorResponse.js | 28 ++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 service/types/validatorResponse.js diff --git a/graphql/schema.graphql b/graphql/schema.graphql index 0b91ac7..cb18d30 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -22,6 +22,8 @@ type Validator { moniker: String operator_address: String address: String + totalTxs: Int + totalPoints: Int } type TxCount { diff --git a/service/Service.js b/service/Service.js index 5b7497b..9be5e0b 100644 --- a/service/Service.js +++ b/service/Service.js @@ -1,5 +1,6 @@ const { sequelize, Mission, Validator } = require('../models') const { QueryTypes } = require('sequelize') +const ValidatorResponse = require('./types/validatorResponse') class Service { async fetchMissions () { @@ -23,12 +24,37 @@ class Service { const validator = await Validator.findOne( { where: { id: args.validatorID } } ) - return validator + // get total_txs data + const txResult = await sequelize.query( + 'SELECT COUNT(*) as totalTxs FROM `TXS` WHERE ( SELECT address FROM `validators` WHERE id = ?)', + { + replacements: [args.validatorID], + type: QueryTypes.SELECT + } + ) + + // get mission clear point data + const pointResult = await sequelize.query( + 'SELECT sum(point) as totalPoints FROM missions WHERE id in (SELECT mission_id FROM point_histories WHERE validator_id = ?)', + { + replacements: [args.validatorID], + type: QueryTypes.SELECT + } + ) + + const res = new ValidatorResponse() + .setOperatorAddress(validator.operator_address) + .setAddress(validator.address) + .setMoniker(validator.moniker) + .setTotalTxs(txResult[0].totalTxs) + .setTotalPoints(pointResult[0].totalPoints) + + return res } async fetchTxCount (args) { const result = await sequelize.query( - 'SELECT COUNT(*) as total_txs FROM `TXS` WHERE ( SELECT address FROM `validators` WHERE id = ?)', + 'SELECT COUNT(*) as totalTxs FROM `TXS` WHERE ( SELECT address FROM `validators` WHERE id = ?)', { replacements: [args.validatorID], type: QueryTypes.SELECT @@ -52,7 +78,7 @@ class Service { async fetchMyPoint (args) { const result = await sequelize.query( - 'SELECT sum(point) as total_point FROM missions WHERE id in (SELECT mission_id FROM point_histories WHERE validator_id = ?)', + 'SELECT sum(point) as totalPoints FROM missions WHERE id in (SELECT mission_id FROM point_histories WHERE validator_id = ?)', { replacements: [args.validatorID], type: QueryTypes.SELECT diff --git a/service/types/validatorResponse.js b/service/types/validatorResponse.js new file mode 100644 index 0000000..2236f01 --- /dev/null +++ b/service/types/validatorResponse.js @@ -0,0 +1,28 @@ +class ValidatorResponse { + setMoniker (a) { + this.moniker = a + return this + } + + setOperatorAddress (a) { + this.operator_address = a + return this + } + + setAddress (a) { + this.address = a + return this + } + + setTotalTxs (a) { + this.totalTxs = a + return this + } + + setTotalPoints (a) { + this.totalPoints = a + return this + } +} + +module.exports = ValidatorResponse From 00d7349a0cc1fa4a6b3abbb990c91fdbd98cfcc5 Mon Sep 17 00:00:00 2001 From: EG-easy <1018eguchi@gmail.com> Date: Wed, 6 Oct 2021 20:09:41 +0900 Subject: [PATCH 2/4] update tests --- tests/__tests__/services/fetchMissionResult.js | 2 +- tests/__tests__/services/fetchValidators.js | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/__tests__/services/fetchMissionResult.js b/tests/__tests__/services/fetchMissionResult.js index 39c2b33..996a86f 100644 --- a/tests/__tests__/services/fetchMissionResult.js +++ b/tests/__tests__/services/fetchMissionResult.js @@ -14,6 +14,6 @@ describe('Mission Histories', () => { test('Total of point is 30', async () => { const q = { validatorID: 1 } const res = await service.fetchMyPoint(q) - expect(res.total_point).toBe(30) + expect(res.totalPoints).toBe(30) }) }) diff --git a/tests/__tests__/services/fetchValidators.js b/tests/__tests__/services/fetchValidators.js index 8db5722..78ad7b2 100644 --- a/tests/__tests__/services/fetchValidators.js +++ b/tests/__tests__/services/fetchValidators.js @@ -12,11 +12,11 @@ describe('Validators Model', () => { describe('Validators Model', () => { test('Check validators columns', async () => { const q = { validatorID: 1 } - const validator = await service.fetchValidator(q) + const res = await service.fetchValidator(q) - const columns = Object.keys(validator.rawAttributes) - expect(columns.indexOf('operator_address')).toBe(1) - expect(columns.indexOf('moniker')).toBe(2) - expect(columns.indexOf('address')).toBe(3) + expect(res.operator_address).toBe('cosmosvaloper156gqf9837u7d4c4678yt3rl4ls9c5vuursrrzf') + expect(res.moniker).toBe('Binance Staking') + expect(res.address).toBe('cosmos156gqf9837u7d4c4678yt3rl4ls9c5vuuxyhkw6') + expect(res.totalPoints).toBe(30) }) }) From 7cf3ca785f57717a046f9c268f0796d9dfe73385 Mon Sep 17 00:00:00 2001 From: EG-easy <1018eguchi@gmail.com> Date: Wed, 6 Oct 2021 20:17:26 +0900 Subject: [PATCH 3/4] add cors setting --- graphql/index.js | 2 ++ package-lock.json | 12 ++++++++++-- package.json | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/graphql/index.js b/graphql/index.js index b349e01..baa1eb8 100644 --- a/graphql/index.js +++ b/graphql/index.js @@ -1,5 +1,6 @@ const express = require('express') const { graphqlHTTP } = require('express-graphql') +const cors = require('cors') // database const schema = require('./schema') @@ -17,4 +18,5 @@ app.use('/graphql', }) ) +app.use(cors()) app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql')) diff --git a/package-lock.json b/package-lock.json index a626842..04c11d0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2345,6 +2345,15 @@ "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==", "dev": true }, + "cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "requires": { + "object-assign": "^4", + "vary": "^1" + } + }, "cosmjs-types": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/cosmjs-types/-/cosmjs-types-0.2.0.tgz", @@ -6033,8 +6042,7 @@ "object-assign": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", - "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=", - "dev": true + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" }, "object-hash": { "version": "2.2.0", diff --git a/package.json b/package.json index 4c58c86..635a377 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "@cosmjs/stargate": "^0.26.0", "@graphql-tools/graphql-file-loader": "^7.1.0", "@graphql-tools/load": "^7.3.2", + "cors": "^2.8.5", "dotenv": "^10.0.0", "express": "^4.17.1", "express-graphql": "^0.12.0", From 42a96907f6d94328249b3aa63ec51c6fbfce07e9 Mon Sep 17 00:00:00 2001 From: padi-dev-hoangpa Date: Thu, 7 Oct 2021 09:34:52 +0700 Subject: [PATCH 4/4] Fix cors --- graphql/index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/graphql/index.js b/graphql/index.js index baa1eb8..95e8cb9 100644 --- a/graphql/index.js +++ b/graphql/index.js @@ -10,6 +10,11 @@ const resolver = new Root(new Service()).init() // Create an express server and a GraphQL endpoint const app = express() +const corsOpts = { + origin: '*' +} + +app.use(cors(corsOpts)) app.use('/graphql', graphqlHTTP({ schema: schema, @@ -18,5 +23,4 @@ app.use('/graphql', }) ) -app.use(cors()) app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'))