-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add endpoint for leaderboards (#839)
* feat: add endpoint for leaderboards * refactor: return User and Space object, instead of ID * feat: add `proposal_count` and `vote_count` to User * fix: rename column for consistency * fix: fix collation error * fix: fix wrong filter field name * fix: update schema * refactor: return space and user as simple string * fix: add `lastVote` to User queries * fix: remove obsolete BINARY casting * fix: remove duplicate definition * fix: format `user` input as address
- Loading branch information
Showing
5 changed files
with
125 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import db from '../../helpers/mysql'; | ||
import log from '../../helpers/log'; | ||
import { buildWhereQuery, checkLimits } from '../helpers'; | ||
import { capture } from '@snapshot-labs/snapshot-sentry'; | ||
|
||
export default async function (parent, args) { | ||
const { first, skip, where = {} } = args; | ||
|
||
checkLimits(args, 'leaderboards'); | ||
|
||
const fields = { | ||
user: 'EVMAddress', | ||
space: 'string', | ||
vote_count: 'number', | ||
proposal_count: 'number' | ||
}; | ||
const whereQuery = buildWhereQuery(fields, 'l', where); | ||
const defaultOrder = 'votesCount DESC, proposalsCount DESC'; | ||
|
||
const orderBy = Object.keys(fields).includes(args.orderBy) | ||
? args.orderBy | ||
: null; | ||
let orderDirection = (args.orderDirection || 'desc').toUpperCase(); | ||
if (!['ASC', 'DESC'].includes(orderDirection)) orderDirection = 'DESC'; | ||
|
||
const query = ` | ||
SELECT l.*, | ||
l.vote_count as votesCount, | ||
l.proposal_count as proposalsCount, | ||
l.last_vote as lastVote | ||
FROM leaderboard l | ||
WHERE 1=1 ${whereQuery.query} | ||
ORDER BY ${ | ||
orderBy ? `l.${orderBy} ${orderDirection}` : defaultOrder | ||
} LIMIT ?, ? | ||
`; | ||
|
||
try { | ||
return db.queryAsync(query, [...whereQuery.params, skip, first]); | ||
} catch (e) { | ||
log.error(`[graphql] leaderboards, ${JSON.stringify(e)}`); | ||
capture(e, { args }); | ||
return Promise.reject(new Error('request failed')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters