damavrom
damavrom

Reputation: 347

GraphQL filtering the nested objects

I want to get some info about some Uniswap transactions using The Graph but I don't know how to go about filtering.

I want to get only Swaps that one of their tokens is ether. That means that the symbol of the token is "ETH". Is there a way to do it?

The relevant GraphQL schema is:

type Swap @entity {
  id: ID!
  token0: Token!
  token1: Token!
}

type Token @entity {
  id: ID!
  symbol: String!
}

Here is the complete GraphQL schema: https://thegraph.com/hosted-service/subgraph/uniswap/uniswap-v3?query=Example%20query

To make my question clearer, if this was SQL, I would query something like this:

SELECT Swap.id
FROM Swap JOIN Token ON Swap.token1_id == Token.id
WHERE Token.symbol

Thank you!

Upvotes: 2

Views: 2586

Answers (2)

damavrom
damavrom

Reputation: 347

I found another way to query what I wanted even though it doesn't involve nested objects. I looked at the code and figured out how to do it: https://github.com/Uniswap/v3-info

The query that does the work I need is this:

{
  swaps(
    where: {token0: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"}
  ) {
    token0 {symbol}
    token1 {symbol}
  }
}

Where the long hexadecimal is the address of the token.

By the way, it appears that it's quite simple to do this according to this GraphGL docs: https://hasura.io/docs/latest/queries/postgres/query-filters/#the-where-argument

According to it, the query should be

{
  swaps(
    where: {token0: {symbol : {_eq: "WETH"}}}
  ) {
    token0 {symbol}
    token1 {symbol}
  }
}

But it doesn't work with the playground of The Graph.

Upvotes: 1

kfx
kfx

Reputation: 8537

Filtering by subfield is a new feature just added to the Graph.
The syntax supported is as follows: where: {token0_: { subfield : "value" }}

For instance to get swaps where the first token has the symbol "WETH":

{
  swaps(first: 1, where: {token0_: {symbol : "WETH"}}) {
    token0 {symbol}
    token1 {symbol}
    amount0
    amount1
  }
}

Result:

{
  "data": {
    "swaps": [
      {
        "token0": {
          "symbol": "WETH"
        },
        "token1": {
          "symbol": "USDT"
        },
        "amount0": "39.227335678972562535",
        "amount1": "-101256.052655"
      }
    ]
  }
}

A more reliable way to do it would be to filter by token's ID, that way scam token someone named "WETH" would not be included, but I used symbol for the sake of clarity of the example.

Upvotes: 2

Related Questions