How to find nodes given an array of values

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.

1 Like

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.

https://docs.dgraph.io/query-language/#regular-expressions
https://docs.dgraph.io/query-language/#has
https://docs.dgraph.io/query-language/#inequality
https://docs.dgraph.io/query-language/#and-or-and-not

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 + -”.

Check One JS example with Dgraph-js for beginners. Using graphQL instead of REST
There are two examples to which you can get what you need.

Cheers.

Hi Michel, thanks for the answer. I’ll try to clarify what I’m trying to achieve.

I have an array with many emails,

[e1@domain1.com, e2@domain2.com, ..., eN@domainN.com]

Is it possible to retrieve the names of all these users in a single query? Or do I need N queries?

Yes, build your query to englobe your array.

As you can see in the documentation of inequality you have a syntax option with array.

  • eq(predicate, [val1, val2, ..., valN])

e.g:

{
  me(func: eq(predicate, [val1, val2, ..., valN])) {
    name@en
    genre {
    	name@en
    }
  }
}

or in JS


const many_emails = [e1@domain1.com, e2@domain2.com, ..., eN@domainN.com]

const query = `
    {
  me(func: eq(predicate, ${many_ emails})) {
    name@en
    genre {
    	name@en
    }
  }
}
       `;
3 Likes

Hi Michel, thanks for the quick answer. That’s just what I was looking for :slight_smile: Thanks!

1 Like

After having implemented your suggestion, I have three follow up questions:

  • Can an array be used with transaction.queryWithVars()?
  • If so, what would be the type that we would supply to the query below?
query allNames($emails: ???) {
  allNames(func: eq(name, $emails) {
    name
  }
}
  • What are the advantages of using queryWithVars() over template string variables?

Thanks for your help Michel

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.

ref: https://docs.dgraph.io/query-language/#graphql-variables

Backward compatibility with GraphQL only.

Cheers.

1 Like