Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add distinct_on option for queries #145

Open
maaft opened this issue Jan 2, 2025 · 3 comments
Open

add distinct_on option for queries #145

maaft opened this issue Jan 2, 2025 · 3 comments

Comments

@maaft
Copy link
Contributor

maaft commented Jan 2, 2025

My project emits Event like this:

emit StateChanged(gameId, state);

I want to be able to query the most-recent event for a gameId. For example Hasura solves this with distinct_on query parameters.

I think that would be a great addition.

My current workaround is, to query all sorted and then do the filtering client side. This adds a lot of traffic overhead of course.

Or maybe there is another solution that I'm not aware of?

@joshstevens19
Copy link
Owner

joshstevens19 commented Jan 2, 2025

why cant you use the order by and first 1 in the graphql aka like below?

query AllTransfers {
  allTransfers(first: 1, orderBy: [BLOCK_NUMBER_DESC]) {
    nodes {
      blockHash
      blockNumber
      contractAddress
      from
      network
      nodeId
      to
      txHash
      value
    }
    pageInfo {
      endCursor
      hasNextPage
      hasPreviousPage
      startCursor
    }
  }
}

https://rindexer.xyz/docs/accessing-data/graphql#ordering

@maaft
Copy link
Contributor Author

maaft commented Jan 2, 2025

I want to get a list of "most recent" games, not a single game.

In my case a GameEvent is defined by gameId and state. I want for example to get the first 10 games, unique by game_id, but ordered by block_number_desc. I don't see any possibility with current API to achieve such thing.

What I would like to do (see distinct_on param):

query GetGames {
  allGameEvents(first: 10 offset: 0 orderBy: [BLOCK_NUMBER_DESC], distinct_on: [game_id]) {
    nodes {
      gamedId
      blockNumber
      state
    }
    totalCount
  }
}

Without this, we have currently these limitations:

  1. need to filter-out duplicate game_ids on client-side
  2. not possible to reliably "fetch X unique games" since there can be multiple GameEvents for the same gameId in neighboring block-numbers. Example event ordering:
(gameId, state, block_number)
- 0, created, 0
- 0, active, 1
- 1, created, 2
- 2, created, 3

When I want to fetch let's say most recent 3 unique games for my pagination so I set first: 3, with current API I'll get on client-side:

- 0, created, 0
- 0, active, 1
- 1, created, 2

After filtering this list will be:

- 0, active, 1
- 1, created, 2

So my pagination breaks.

@maaft
Copy link
Contributor Author

maaft commented Jan 2, 2025

maybe custom queries could be an option?

https://www.graphile.org/postgraphile/custom-queries/

Update:

yep, solved it with custom queries:

  1. create custom postgres function according to above documentation and your needs
  2. it will be exposed by graphql API
  3. use it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants