You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
query title($uids: string = "[0x1, 0x2, 0x3]") { ... }. The value of the variable is a quoted array.
Note If you want to input a list of uids as a GraphQL variable value, you can have the variable as string type and have the value surrounded by square brackets like ["13", "14"].
This list syntax only works with uids. I don't even know why the example ["13", "14"] is in the docs because those are not hexidecimal uids, and it just adds confusion thinking this feature is already implemented.
Alternatively, when I want to query for a list of items not a uid, I have to dynamically build a more complex query and variable list.
// imagine this is a dynamic list from an inputconstsrcIds=[25,26];constvarList=srcIds.map((_,i)=>`$srcId_${i}`);constquery=` query q(${varList.map(var=>`${var}: string`).join(", ")}){ items(func: eq( srcId,${varList.join(", ")} ) ) { uid srcId } }`;constreq={
query,variables: Object.fromEntries(varList.map((var,i)=>[var,`${srcIds[i]}`]))};
Enhancement
It would be nicer to do this:
// imagine this is a dynamic list from an inputconstsrcIds=[25,26];constquery=` query q($srcIds: string){ items(func: eq( srcId, $srcIds ) ) { uid srcId } }`;constreq={
query,variables: {"$srcIds": `[${srcIds.map((id)=>`"${id}"`)}]`}};
It would be best to support the syntax $srcIds: [string] but for now a quicker fix would just allow an array of strings/ints like the $uids: string accepts.
The text was updated successfully, but these errors were encountered:
There is a question what to do though if the query actually wants to find a string that is equal to what could be evaulated as an array. That is something that will need to be tested and allow a workaround. I know I used strings to store JSON values in Dgraph, and I am sure others do. What I am not sure though is if anyone is querying with eq on those strings. Maybe the better option would be to add the $srcIds: [string] to not break existing functionality of searching for json like strings with $srcIds: string
Use case and current behavior
This list syntax only works with uids. I don't even know why the example
["13", "14"]
is in the docs because those are not hexidecimal uids, and it just adds confusion thinking this feature is already implemented.I can do:
But I can't do:
Alternatively, when I want to query for a list of items not a uid, I have to dynamically build a more complex query and variable list.
Enhancement
It would be nicer to do this:
Solution proposal
No response
Links to Discuss, RFC or previous Issues and PRs
https://discuss.dgraph.io/t/how-to-find-nodes-given-an-array-of-values/3316/7?u=amaster507
Links to examples and research
https://dgraph.io/docs/query-language/graphql-variables/
Additional Information
It would be best to support the syntax
$srcIds: [string]
but for now a quicker fix would just allow an array of strings/ints like the$uids: string
accepts.The text was updated successfully, but these errors were encountered: