EndOfTheGlory
EndOfTheGlory

Reputation: 309

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\")})"
    }
  ]
}```

Upvotes: 0

Views: 3869

Answers (2)

Pompedup
Pompedup

Reputation: 636

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

Upvotes: 2

Greg Brodzik
Greg Brodzik

Reputation: 1817

The orderBy and orderDirection are separate query params, and orderDirection needs to be lowercase for their enum syntax.

{
  platforms(first: 5) {
    id
    pools {
      id
      dailyPoolSnapshots(first: 1, orderBy: timestamp, orderDirection: asc) {
        timestamp
      }
    }
    poolAddresses
    latestPoolSnapshot
  }
  registries(first: 5) {
    id
  }
}

Upvotes: 1

Related Questions