Is it possible to find all nodes matching a value within an array in a single query?
For example, say our database contains nodes representing a business’ clients with properties name and email. We want to send an email to a set of clients with a personalized header: “Hello {name}!”.
If we already have the emails in an array, how would we go about creating a query that retrieves all the names?
Particularly, sample code using dgraph-js would be great.
UPDATE(2020): if you are reading this. Take into account that Dgraph has changed a lot since this post. Some answers and tips here are just examples. For sure you can do the same thing mentioned here in the comments in other ways. For example, a list of strings can be passed via DQL Variable(which was called “graphql variable” back then). You can pass a list of string-like $mylist='[loren, lorem, loren]' and this will be accepted in the DQL variable as a list of args.
This question is somewhat ambiguous, could not understand well.
If you want to find several nodes matching to single value. This is a simple query. You can mix with Has, regexp, Inequality, Connectives (AND, OR and NOT) and so on.
e.g:
{
names(func: has(email)) @filter(regexp(email /@some.com/i)) {
name
email
}
}
You gonna have an array.
In the query ask for the email and the name at the same time. Otherwise you will make several queries later (or a great extra).
Dgraph generally only delivers an array of requested nodes (per query and/or query block). All work of treatment of the result has to be done via application. Some things can be done with Dgraph, but you need to understand the whole language “GraphQL + -”.
Nop, This is a specification for GraphQL Variables. There is no array for GQL Variables. Check https://graphql.org/learn/queries/#variables You use ES6 template Strings is the most recommended.
I’m trying to do a similar thing using the Python client pydgraph that you demonstrated in a previous JS code snippet but running into an unexpected result/error.
When the query is executed, the following exception is thrown:
'Traceback (most recent call last):\n File "../.vscode/extensions/ms-python.python-2020.11.371526539/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_resolver.py", line 193, in _get_py_dictionary\n attr = getattr(var, name)\nAttributeError: Extensions\n'
Am I doing something wrong? I have success writing the same exact query without the use of variables (also tested using ratel UI) but that doesn’t satisfy my use case where the values I’m passing to the eq function are dynamic and passed in as an argument to a function.