Search code examples
graphqlethereumuniswapthegraph

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!


Solution

  • 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.