opencypher / openCypher

Specification of the Cypher property graph query language
http://www.opencypher.org
Apache License 2.0
855 stars 150 forks source link

Make it easier to return maps of select properties #178

Open thobe opened 7 years ago

thobe commented 7 years ago

CIR-2017-178

It is a common requirement to return only a handful of properties from a node, in cases where the actual node has many more properties than that. Currently this requires quite repetitive syntax:

MATCH (user:User{username:$username})
RETURN {
    username: user.username,
    email: user.email,
    firstName: user.firstName,
    lastName: user.lastName
} AS user

It would be nice to have a shorter syntax to allow selecting the properties to return:

MATCH (user:User{username:$username})
RETURN user{.username, .email, .firstName, .lastName}
thobe commented 7 years ago

One could also imagine extending this syntax to allow selecting variables for inclusion in the map, and for allowing regular map entries:

MATCH (manager:Employee{employmentId:$managerId})-[:MANAGES]->(employee:Employee)
MATCH (employee)-[:WORKS_IN]->(group)<-[:WORKS_IN]-(colleague)
WITH employee, manager.name AS manager, collect(colleague) AS colleagues
RETURN employee{
// select some properties of the 'employee' node:
    .employmentId,
    .email,
    .name,
// select a variable for inclusion as-is:
    manager,
// include a regular key-value entry:
    co_workers: [co_worker IN colleagues | co_worker{.name, .employmentId}]
}
thobe commented 7 years ago

Combining this with the comprehension subquery suggested in #177 would make things even nicer (and almost directly mappable from Facebook's GraphQL):

MATCH (manager:Employee{employmentId:$managerId})-[:MANAGES]->(employee:Employee)
RETURN employee{
    .employmentId,
    .email,
    .name,
    manager{.employmentId, .email, .name},
    co_workers: [
        MATCH (employee)-[:WORKS_IN]->(group)<-[:WORKS_IN]-(colleague)
        RETURN colleague{.name, .employmentId}
    ]
}
petraselmer commented 7 years ago

(Updated the original comment to include the name of the CIR)