graphqlcryptocurrencygithub-graphql

How to use orderBy for graphQL


I am trying to sort reservesUSD of nested object dailyPoolSnapshots In descending order by timestamp and return it's first value (in other words, return the latest entry). I know almost nothing of GraphQL and it's documentation seems confusing and scarce. Can someone help me to figure out how to sort my objects?

I am using subgraphs on the Ethereum mainnet for Curve.fi to get information about pools

My code:

  pools(first: 1000) {
    name
    address
    coins
    coinDecimals
    dailyPoolSnapshots(first: 1, 
      orderBy:{field: timestamp, order: DESC}) {
      reservesUSD
      timestamp
    }
  }
}

It throws and error:

  "errors": [
    {
      "locations": [
        {
          "line": 0,
          "column": 0
        }
      ],
      "message": "Invalid value provided for argument `orderBy`: Object({\"direction\": Enum(\"DESC\"), \"field\": Enum(\"timestamp\")})"
    }
  ]
}```

Solution

  • Here is your solution

    {
        pools(first: 1000) {
        name
        address
        coins
        coinDecimals
        dailyPoolSnapshots(first: 1, 
          orderBy: timestamp, orderDirection:desc) {
          reservesUSD
          timestamp
        }
      }
    }
    

    In the playground, you have the doc on the right, you can search dailyPoolSnapshots, if you click on it, you will have the documentation of this query

    Sample for this query:

    Type
    [DailyPoolSnapshot!]!
    
    Arguments
    skip: Int = 0
    first: Int = 100
    orderBy: DailyPoolSnapshot_orderBy
    orderDirection: OrderDirection
    where: DailyPoolSnapshot_filter
    block: Block_height
    

    Arguments are all the params you can use