Get directly connected nodes for multiple nodes in one query?

Hello, This is my first time playing with neo4j, I modeled a graph that looks like this:

A ---> B
A ---> C
B ----> D
B ----> E
C ----> F

now, i my input can contain several nodes, let's say i get "A and B", and i want to find the direct connections of A + the direct ones of B, meaning, to return the nodes:
"B", "C", "D","E"

how to do it in a single query?

Try the following:

unwind ['A', 'B'] as input
match(n where = input)
match (n)-[:REL]->(m)
return, collect(m) as children

Replace the literal array ['A','B'] with your parameter containing your inputs. Also, replace the match condition with your unique identifier and the relationship type in the pattern match with yours. The result is a row for each of your inputs, with a list of each input's direct connected nodes.

Test Data:


Screen Shot 2022-01-22 at 8.34.39 PM

If you prefer a list instead of a collection, replace the 'return' clause with this return clause 'return, m', giving the following alternative result:

Screen Shot 2022-01-22 at 8.40.10 PM

If you want just a list of the child nodes, replace the return clause with this return clause 'return collect(m)', giving the following result: