Skip to content

Commit

Permalink
Fabo/simplify queries (#108)
Browse files Browse the repository at this point in the history
* simplified db queries

Conflicts:
	lib/luniedb-source.js
	lib/queries.js

* switched to yarn

* clean up

* clean up
  • Loading branch information
faboweb authored and colw committed Nov 26, 2019
1 parent bc493ce commit 3f872b1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 93 deletions.
46 changes: 32 additions & 14 deletions lib/luniedb-source.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { RESTDataSource } = require('apollo-datasource-rest')
const config = require('../config')
const queries = require('./queries')
const networksLocal = require('../data/networks-local.js')
const testnet = networksLocal['local-cosmos-hub-testnet']

Expand All @@ -15,20 +14,28 @@ class LunieDBAPI extends RESTDataSource {
request.headers.set('x-hasura-admin-secret', config.hasura_admin_key)
}

async getData(type, selection = '') {
async getData(query) {
const data = await this.post('', {
query:
typeof queries[type] !== 'undefined' ? queries[type](selection) : ''
query
})
if (data.errors) {
throw new Error(data.errors.map(({ message }) => message).join('\n'))
}
return data.data[type]
return data.data
}

async getMaintenance() {
const response = await this.getData('maintenance')
return response
const { maintenance } = await this.getData(`
query {
maintenance {
id
message
show
type
}
}
`)
return maintenance
}

async getValidatorInfoByAddress(validatorId, networkID) {
Expand All @@ -39,19 +46,30 @@ class LunieDBAPI extends RESTDataSource {
const selection = networkID
? `(where: {operator_address: {_eq: "${validatorId}"}})`
: ''
return await this.getData(
networkID.replace(/-/g, '_') + '_validatorprofiles',
selection
)
const schema = networkID.replace(/-/g, '_')
return await this.getData(`
query {
${schema}_validatorprofiles${selection} {
operator_address
picture
}
}
`)
}

async getValidatorsInfo(networkID) {
if (networkID === testnet.id) {
return {}
}
return await this.getData(
networkID.replace(/-/g, '_') + '_validatorprofiles'
)
const schema = networkID.replace(/-/g, '_')
return await this.getData(`
query {
${schema}_validatorprofiles {
operator_address
picture
}
}
`)
}
}

Expand Down
79 changes: 0 additions & 79 deletions lib/queries.js

This file was deleted.

0 comments on commit 3f872b1

Please sign in to comment.