Prisma1: Order by related fields

Created on 9 Feb 2017  路  48Comments  路  Source: prisma/prisma1

Example: a User has many Posts. Should be possible to order the Posts by User name.

Maybe:

query {
  allPosts(orderByRelated: [user_name_ASC]) {
    id
  }
}
kinfeature areapi

Most helpful comment

This would be a lifesaver!

All 48 comments

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.

screen shot 2018-02-24 at 11 47 25 am

@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 馃挴

VERY needed

is it yet not built???? how do I order my posts by number of upvotes???

Would be an amazing feature

Any update on this?

it's a really important feature. This issue was created two years ago and hasn't been fixed.

Definitely agree, we are patiently waiting for this feature to push Prisma to production here :)

Any update on this?

Any update on this??? :(

Can people please stop spamming this thread with "UPDATES????" and instead upvote the first comment? I does not help anything, they are currently working on Prisma 2 (where this feature will be integrated) and that will take a while. In the meantime, you're just notifiying 35 participants with no new information and thus wasting their time. I think they will update us once this feature is implemented.

Can people please stop spamming this thread with "UPDATES????" and instead upvote the first comment? I does not help anything, they are currently working on Prisma 2 (where this feature will be integrated) and that will take a while. In the meantime, you're just notifiying 35 participants with no new information and thus wasting their time. I think they will update us once this feature is implemented.

I upvoted the first comment months ago.
I guess people wouldn't "spam" if the Prisma team was a little bit more communicative, don't you think?

Another workaround to avoid ordering client-side is to put an index attribute on the model related to another field

type Vendor {
 id: ID!
 name: String
} 

type Product {
  _vendor_name: String
 vendor: Vendor
}

You have to handle _vendor_name updates manually but then you can

query {
  products(orderBy: _vendor_name_ASC) {  
   id 
   name
   vendor {
     name
    } 
  }
}

Yeah it's cumbersome, but works fine

If you want ordering by multiple fields then do it client side, it's the only solution

Or write a raw query, then use prisma.$fragment to get the type of data you need.

@bkstorm could you provide us an example please?

Is this feature will never be launched?

it is a must to have feature, waiting almost 3 years?

Prisma team, Are you working on this feature for Prisma V2?

Or write a raw query, then use prisma.$fragment to get the type of data you need.

@bkstorm Do you have an example of this please?

Or write a raw query, then use prisma.$fragment to get the type of data you need.

@bkstorm Do you have an example of this please?

I don't use Prisma 1 any more due to it lacks a lot of features, like this one, but I will try to create an example code for you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

schickling picture schickling  路  36Comments

marktani picture marktani  路  71Comments

mcmar picture mcmar  路  57Comments

sorenbs picture sorenbs  路  48Comments

marktani picture marktani  路  44Comments