Filtering#
Filtering subgraphs in subgrounds
is done via the where
argument in FieldPath
. A subgraph's GraphQL provides several options to filter based on nearly any field path.
Click for Interactive Documentation
Clicking this button will enable editing and execution of the code-blocks on this page. Learn more here.
from subgrounds import Subgrounds
sg = Subgrounds()
curve = sg.load_subgraph(
"https://api.thegraph.com/subgraphs/name/messari/curve-finance-ethereum")
pool = curve.LiquidityPool # shorthand for examples
Note
In some of the following examples, multiple conditions are stacked on top of each other. This would not result in any data being returned since the multiple conditions would likely conflict with each other.
Matching Values#
Using the ==
and =!
operators in Python, matching exact or negated values on field paths is pretty straight forward:
sg.query_df(
curve.Query.liquidityPools(
where=[
isSingleSided == False,
# or
isSingleSided != True,
]
)
)
sg.query_df(
curve.Query.liquidityPools(
where={
"isSingleSided": False,
# or
"isSingleSided_not": False,
}
)
)
Comparisons#
Filtering can also be based on standard comparison logic on any field path, such as "greater than", "less than", etc — generally more useful for numeric fields.
sg.query_df(
curve.Query.liquidityPools(
where=[
pool.cumulativeVolumeUSD > 150000000,
pool.cumulativeVolumeUSD >= 150000000,
pool.cumulativeVolumeUSD < 150000000,
pool.cumulativeVolumeUSD <= 150000000,
]
)
)
sg.query_df(
curve.Query.liquidityPools(
where={
"cumulativeVolumeUSD_gt": 150000000,
"cumulativeVolumeUSD_gte": 150000000,
"cumulativeVolumeUSD_lt": 150000000,
"cumulativeVolumeUSD_lte": 150000000,
}
)
)
Nested Filtering#
Entities can have any layer of nestable objects which thereby are also filterable in the where
clause:
sg.query_df(
curve.Query.liquidityPools(
where=[
pool.hourlySnapshots.hourlyVolumeUSD > 1000
]
)
)
sg.query_df(
curve.Query.liquidityPools(
where={
"hourlySnapshots_": {"hourlyVolumeUSD_gt": 14720000}
}
)
)
Note
The trailing _
prefix is needed in the GraphQL form since without it, GraphQL assumes you are matching the exact value!