prisma1
prisma1 copied to clipboard
Order by related fields
Example: a User has many Posts. Should be possible to order the Posts by User name.
Maybe:
query {
allPosts(orderByRelated: [user_name_ASC]) {
id
}
}
This would be a lifesaver!
@marktani any workaround we can use for the time being?
The best workaround I'm aware of, is to order items client side 🙂
It would be great to be able to order by "to-many" related fields as well (by the count of related items).
For example, to get a list of the top 10 most voted-for links (assuming votes is a related field of type [Vote!]!):
query {
allLinks(first: 10, orderBy: votes_DESC) {
id
url
description
_votesMeta {
count
}
}
}
Currently, to get that list you'd have to query for every Link and then sort/slice it on the client, which is potentially a ton of overfetching.
A question for when they plan to add this improvement?
+1 for this
+1
+1
I see a lot of 👍 Dear GraphCool when do you plan to implement that?
Must-have feature!
In my case this table will have thousands of rows. My users won't be able to sort the table by "createdBy" or "dataset" until this feature lands.
@danielrasmuson you can create some sortable index just as temporary solution :)
Have a look at #62 This would solve the issues:
query{
servers(
orderBy: {
active: DESC
assetNumber: ASC
customer: {
name:ASC
}
}
) {
id
active
assetNumber
customer {
name
}
}
}
+1
+1
Echoing what @PascalSenn said, but updating orderBy to take an array. Arrays have implicit positioning. Objects aren't guaranteed that.
Additionally, every related ordering statement must also take an array, so that we can use multiple sort statements on a related field. (ie customer)
query {
servers(
orderBy: [{
active: DESC
}, {
assetNumber: ASC
}, {
customer: [{
name: ASC
}]
}]
) {
id
active
assetNumber
customer {
name
}
}
}
This proposal also has nice nesting where the input type for CustomerOrderByInput nests inside ServersOrderByInput. That prevents the infinitely expanding types that you would see for customer_name_ASC or customer_paymentInfo_expiryDate_ASC or customer_paymentInfo_issuer_headquarters_region_population_ASC.
The only problem I still have with this proposal is that each object must only have 1 key-value pair, but there's no way in my mind to enforce that. If anyone else has a proposal that would address this concern, please let me know.
This continues to be an important feature for us. I'll update this issue when we have a concrete timeframe. See also this explanation for why we were unable to ship this feature in Q3 as planned.
@mcmar - I like how your proposal is more powerful than the originally proposed syntax. At the same time I am concerned about the added syntactic complexity compared to the original proposal. Another consideration is that this syntax would require a breaking change, which might not be worth it for this extra expressiveness.
@sorenbs If you're switching orderBy from FooOrderByInput to [FooOrderByInput!], wouldn't that be a breaking change anyway? I've noticed there's some magic in my generator that turns create: [FooCreateWithoutBarInput!] into create?: FooCreateWithoutBarInput[] | FooCreateWithoutBarInput and allows me to actually query with a single FooCreateWithoutBarInput even though the schema says otherwise. Maybe you can pull some similar wizardry here?
Haha. Just noticed that you commented on this very issue in the graphql repo 2.5 years ago: https://github.com/graphql/graphql-js/issues/207#issuecomment-221556334
And more recent ones: https://github.com/facebook/graphql/issues/488#issuecomment-412469796
I think an inputUnion type would eventually solve all these problems and allow us to have our cake and eat it too with regards to backwards compatibility. Sadly, that seems to be a long way away.
@mcmar - see the Input Coercion section in the spec. It's specified behaviour, and quite useful for cases like this :-)
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 10 days if no further activity occurs. Thank you for your contributions.
+1 for this
+1 What is the latest workaround
+1
Very interesting feature actually... I think a lot of prisma's users would like this implementation !!
Also looking for a workaround if someone has another solution than client side ordering :)
+1
+1 we need this
Please add this and order by multiple fields!
+1
+1 I have a need for exactly this. :)
+1 💯