Skip to content

Add support for Collect function in Cypher queries #43

@amansinghoriginal

Description

@amansinghoriginal

Overview of feature request

If one has a graph of orders, line items and products, then one can write a query today like:

MATCH (o:Order {orderId: 'order123'})-[:HAS]->(li:LineItem)
RETURN o.orderId AS orderId,
       o.date AS orderDate,
       li.productId AS productId,
       li.quantity AS quantity

If one order has 3 line items, the above query will return 3 rows in the result for that same order - once for each line item.
The output would look like this:

{
  "orderId": "order123",
  "orderDate": "2025-04-07",
  "productId": "prod001",
  "quantity": 2
}
{
  "orderId": "order123",
  "orderDate": "2025-04-07",
  "productId": "prod002",
  "quantity": 5
}
{
  "orderId": "order123",
  "orderDate": "2025-04-07",
  "productId": "prod003",
  "quantity": 1
}

But using the collect function I could write a query like this:

MATCH (o:Order {orderId: 'order123'})-[:HAS]->(li:LineItem)
RETURN o.orderId AS orderId, 
       o.date AS orderDate, 
       collect({productId: li.productId, quantity: li.quantity}) AS lineItems

This should given an output like:

{
  "orderId": "order123",
  "orderDate": "2025-04-07",
  "lineItems": [
    { "productId": "prod001", "quantity": 2 },
    { "productId": "prod002", "quantity": 5 },
    { "productId": "prod003", "quantity": 1 }
  ]
}

When the result set changes, drasi emits diffs.
In the example above, when the collection changes, we can emit a diff.
For instance, if a new line item gets added, drasi could emit a diff like:

Updating {
  before: {
    "key": "order1", 
    "orderId": "order123",
    "userId": "user456", 
    "orderTotal": 99.95,
    "lineItems": [
      {"productId": "prod001", "quantity": 2},
      {"productId": "prod002", "quantity": 5}
    ]
  },
  after: {
    "key": "order1", 
    "orderId": "order123",
    "userId": "user456", 
    "orderTotal": 99.95,
    "lineItems": [
      {"productId": "prod001", "quantity": 2},
      {"productId": "prod002", "quantity": 5},
      {"productId": "prod003", "quantity": 1}
    ]
  }
}

Acceptance criteria

No response

Additional context

No response

Would you like to support us?

  • Yes, I would like to support you

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions