Querying through multiple relationships

Consider the following graph. Red nodes are called Entities and beige nodes are call Components. The CHILD_OF relationship between Entities, forms a tree of Entities. There are no cycles, and a single root, with id '#root'. Entities can have one or more Components attached to them, using the ATTACHED_TO relationship.

I need a query that takes an Entity ID and returns all the entities and components that are descendants of the entity with the given ID. The following query returns all the Entities, but does not return the Components of those entities.

MATCH (e:Entity {id: '#root'})
WITH e, [(e)<-[:CHILD_OF*]-(x) | x] as descendants
RETURN e, descendants

How can I modify this query to return the components also?

Try this:

MATCH (e:Entity {id: '#root'})
CALL apoc.path.spanningTree(e, {maxLevel:5}) YIELD path
RETURN path

This should produce the graph you displayed.

Thanks. I'm using Neo4j desktop and I get the following message to this query.

There is no procedure with the name `apoc.path.spanningTree` registered for this database instance. Please ensure you've spelled the procedure name correctly and that the procedure is properly deployed.

I assume this means I need to install an extension. I'll look into that.

Can my query be modified to do what I want without an extension? I assume so.

Thanks!

Got it installed. Thanks.

You can use variable path length:
MATCH (e:Entity {id: '#root'})
MATCH p = (e)-[*..5]-(d)
RETURN p