How can I reuse "this" in reference to a current node, in neo4j-graphql?

I am using neo4j-graphql-js and in one of my cypher statement's I'm referring to "this" which is the current node. It seems that the word "this" in my first MATCH is accurate, but then when I use MATCH the second time, "this" becomes just a general keyword and defaults to an unlabeled/non-specific node (and just returns everything).

Here's the code currently. it finds any posts that are either tagging "Things" that are children of the current Thing node, or where they're tagging the current Thing node specifically.

        MATCH (this)<-[:CHILD_OF]-(t1:Thing)<-[:TAGGED]-(post:Post)
        WITH COLLECT(post) as rows
        MATCH (this)<-[:TAGGED]-(p:Post)
        WITH rows + COLLECT(p) as allRows
        UNWIND allRows as post
        RETURN count(DISTINCT post)

Any ideas how I can reuse "this" the second time? I tried sharing it after WITH COLLECT(post) as rows, like this:

    WITH COLLECT(post) as rows, this

Any ideas would be appreciated, thank you in advance!

Is it possibly that "this" doesn't exist, and I need to instead use OPTIONAL, so that if it fails, it still continues on? I think it might be stopping there?

Is that accurate? So this should work?:

OPTIONAL MATCH (this)<-[:CHILD_OF]-(t1:Thing)<-[:TAGGED]-(post:Post)
WITH COLLECT(post) as rows, this
OPTIONAL MATCH (this)<-[:TAGGED]-(p:Post)
WITH rows + COLLECT(p) as allRows
UNWIND allRows as post
RETURN DISTINCT post
ORDER BY post.date DESC

The clause WITH split your query into what's called query parts.

query part 1
WITH
query part 2

Each query parts have their own variables and if you want to use element of a query part in the next one you must pass it in the WITH clause.

In your case : WITH COLLECT(post) as rows, this

Note: Your query seems to be poorly built for your need.
Do not forget that the MATCH clause loop everything under it except RETURN and WITH who are triggering the end of the query part.

Thank you for your response. Would love any suggestions to optimize this query!

Try this:

MATCH (post:Post)-[:TAGGED]->(t1:Thing)
OPTIONAL MATCH (t1)-[:CHILD_OF]->(this) 
OPTIONAL MATCH (post1:Post)-[:TAGGED]->(this)

WITH (COLLECT(distinct post) + COLLECT(distint post1)) as allRows
UNWIND allRows as allPosts
RETURN count(DISTINCT allPosts)

Have you considered filtering by date? Since you are ordering DESC, this suggests that perhaps some posts are too old to bother considering. (Of course, you'd need to index on date.)

Hi @ameyasoft I think this might return the case where a post tags t1 but t1 isn't the child of this? I'm thinking since the optional Match is separate from the first Match, post could be any post that tags a Thing? Is that right?

It would be nice if you can explain how a Post is connected to 'Thing' and 'this'. These two nodes ('Thing' and 'this') are they connected to same Posts or different Posts?