Dynamic where clauses #282
-
I'm only first level brown belt in Subgraph/GraphQL so not even sure a feature like this makes sense for the graph-client, but here goes. I'm finding myself writing duplicate queries to handle different https://dev.to/mgustus/filtering-graphql-query-using-typescript-and-typeorm-2l49 Curious if this, or something like it, would make sense for the graph-client? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
In context to this, question, can we do something like this:
Where |
Beta Was this translation helpful? Give feedback.
-
Hi @mpaler ! For example, for this Subgraph, you can see in the docs that the So you can write a query that expects this query filteredUsers($usersFilter: User_filter) { # define the variable
users(where: $usersFilter) { # pair it with the argument
id
liquidityPositions {
id
pair {
token0 {
id
}
token1 {
id
}
}
}
}
} Now, when you run your GraphQL query, you can use this single GraphQL query, but with different (=dynamic) variables for each filter that you might need:
|
Beta Was this translation helpful? Give feedback.
Hi @mpaler !
You can leverage the concept of GraphQL
variables
to achieve that.First, you need to find the actual GraphQL
input
type that's defined for thewhere
argument you with to match, and then use thatinput
type for your GraphQL query, as a variable (similar to the example you shared). You can do that with GraphiQL, or just introspect the schema and check the API docs of your field.For example, for this Subgraph, you can see in the docs that the
User_filter
input type is used for thewhere
:So you can write a query that expects this
input
type as a variable, and then match it to thewhere
argument