I ran the commands in AGE and ran through the following steps: -
Creating my data like this:
SELECT * FROM cypher('test', $$
CREATE (:opt_match_v {prop: 'value1'})-[:opt_match_e]->(:opt_match_v {prop: 'value2'}),
(:opt_match_v {prop: 'value3'})-[:opt_match_e]->(:opt_match_v {prop: 'value4'})
$$) AS (r agtype);
This command works perfectly fine:
SELECT * FROM cypher('test', $$
MATCH p=(n)-[]->(m)
RETURN n.prop AS custom_node ORDER BY n.prop
$$) AS (r agtype);
r
----------
"value1"
"value3"
(2 rows)
Hence, we can see, order by being after return is not really an issue. Using as with return is not an issue either.
However, you were correct, this command does not work.
test=# SELECT * FROM cypher('test', $$
MATCH p=(n)-[]->(m)
RETURN n.prop AS custom_node ORDER BY custom_node
$$) AS (r agtype);
2023-05-26 11:52:08.197 CEST [30849] ERROR: could not find rte for custom_node at character 99
2023-05-26 11:52:08.197 CEST [30849] STATEMENT: SELECT * FROM cypher('test', $$
MATCH p=(n)-[]->(m)
RETURN n.prop AS custom_node ORDER BY custom_node
$$) AS (r agtype);
ERROR: could not find rte for custom_node
LINE 3: RETURN n.prop AS custom_node ORDER BY custom_node
Therefore, solution would be to use this query: -
test=# SELECT * FROM cypher('test', $$
MATCH p=(n)-[]->(m)
WITH n.prop as custom_node RETURN custom_node ORDER BY custom_node
$$) AS (r agtype);
r
----------
"value1"
"value3"
(2 rows)