neo4j / graphql

A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations.
https://neo4j.com/docs/graphql-manual/current/
Apache License 2.0
493 stars 147 forks source link

Proposal: deep sorting #145

Open Andy2003 opened 3 years ago

Andy2003 commented 3 years ago

As explained in https://github.com/neo4j-graphql/neo4j-graphql-java/issues/64 I would suggest to change the way sorting is defined.

Given the Schema:

type Movie {
  title: String
  publishedBy: Publisher @relation(name: "PUBLISHED_BY", direction: OUT)
}

type Publisher {
  name: ID!
}

The augmentation would be:

type Query {
  movie(sort: [_MovieSorting!]): [Movie!]!
}
enum SortDirection{
  asc,
  desc,
}

input _MovieSorting {
  title: SortDirection
  publishedBy: [_PublisherSorting!]
}

input _PublisherSorting {
  name: SortDirection
}

So a user can get all movies ordered by Publisher.name and title by calling

query {
  movie(sort: [{publishedBy: [{name: desc}]}, {title: asc}]) {
    title
  }
}

which results in this query

MATCH (m:Movie)
WITH m
  ORDER BY head([(m)-[:PUBLISHED_BY]->(p:Publisher) | p.name]) DESC, m.name ASC
RETURN m{.title}

This sorting should be possible for all 1..1 relations.

darrellwarde commented 3 years ago

This is great @Andy2003, thanks for the proposal. Today is the deadline for us to propose new pieces of work for the next development phase, so we sadly haven't got time to fully elaborate this in time.

However, I have proposed refactoring from our current enum sort types to object sort types better aligned with the Java library, which will put us in a much better position to implement this feature in the future, without making any breaking changes.

Andy2003 commented 3 years ago

@darrellwarde I'm currently scanning the API to find the work to do, to align the java-version. I think, now is a good point in time to optimize the API. Sad to hear that the Deadline is so close.

darrellwarde commented 3 years ago

For sure, for me it's very much about getting as many breaking changes as possible out of the way before we release version 1.0.0. For clean additions of functionality, not so much of a problem, but this change would require underlying changes to enable it. At the very least, we can get the underlying change requirements out of the way, and then we can cleanly add it later without breaking any schemas out in the wild. 🙂

Yes, there is a bit of a deadline, but we often have time to squeeze other bits of work in, so please don't slow down on raising these kinds of issues! 🔥

rcbevans commented 2 years ago

Hey @darrellwarde, any update on this?

I have many examples of places where I need to be able to sort results based on connected edges, for example, sort posts by the number of comments, or sort author by most recent post.

I attempted to work around the issue by adding a virtual field backed by a cypher query to return the result to sort on, hoping the GraphQL library would generate an order by augmentation for the property, but it seems it doesn't for fields backed by Cypher.

In some cases I'm able to mitigate by denormalizing the data and storing the count on the node as well, but this opens the opportunity for the property to be wrong which isn't great.

morningcloud commented 1 year ago

Following this issue for updates after posting to the discord channel. Looking forward to when this feature will be addressed.

morningcloud commented 1 year ago

Hi @darrellwarde, are the sorting-related features abandoned or is there any plan to proceed with the Pagination and Sorting 2.0 milestone?

Hi @rcbevans, I am interested to know if you managed to apply some reasonable alternative approaches to support the use case you mentioned for sorting

darrellwarde commented 1 year ago

Definitely not abandoned, just a lot more to do first (updateOrCreate, root-level Interface and Union queries, etc.). And we're also more likely to implement sorting by aggregations first, as that has more utility than this. So don't expect anything too soon on this!

MichaelCDormann commented 1 year ago

Hi @darrellwarde, just wondering if this is still not abandoned. This would be a great feature to be able to use.

Currently if I need sorting on related node properties I have to give up on pagination, get all results, and sort on the client side. Curious if anyone has any better solutions.

ghost commented 11 months ago

Hi @darrellwarde Just wanted to know if this feature is still being developed?

We have a use case where we have to sort based on the properties of nested node in rich relationship. It would be great if we have this feature. Thanks