De-referencing Relationships for Drag & Drop Ordering?

default discord avatar
TheDunco
8 months ago
11

Are you able to de-reference relationship objects when querying them? As in, can I have a relationship field wherein I query that field directly and then de-reference the result to get the data from the "related" documents in the order they appear in the relationship field?



I'm essentially trying to create hard-linked, drag & drop ordering of specific documents, as opposed to sorting by an arbitrary field on said documents. Is there a better way to do this without creating another field on the related documents? (wow I said "documents" a lot, sorry if this was confusing I hope I got my point across)

  • default discord avatar
    Alessio 🍣
    8 months ago
    a relationship field wherein I query that field directly and then de-reference the result to get the data from the "related" documents in the order they appear in the relationship field

    Just trying to understand - wouldn't increasing the "depth" in your query do that?

  • default discord avatar
    TheDunco
    8 months ago

    Is that what the query depth does? I'm honestly not familiar with how that works. Is this a GraphQL thing or a Payload thing? I'd assume a Payload thing and I will look into that if you think that could accomplish what I want here. That may be what I'm looking for



    Oh, yeah, I see this now:

    https://payloadcms.com/docs/getting-started/concepts#depth

    . That's awesome

  • default discord avatar
    Alessio 🍣
    8 months ago

    depth makes sure to populate the relationship's data instead of just displaying its ID - it's a payload thing. It's meant to be used in the Rest API and not GraphQL, though

  • default discord avatar
    TheDunco
    8 months ago
    depth makes sure to populate the relationship's data instead of just displaying its ID - it's a payload thing

    Yeah, that's awesome!



    It's meant to be used in the Rest API and not GraphQL, though

    This, however, is unfortunate since I'm only using GraphQL and have a whole abstracted system based entirely on fetching my content via GraphQL. Is it possible to specify this in the field config object and then query with GraphQL? I know you can set

    maxDepth

    in some places, but can you just set

    depth

    on a field level and have that apply to every query?

  • default discord avatar
    Alessio 🍣
    8 months ago
    but can you just set depth on a field level and have that apply to every query?

    Oh correct me if I'm wrong but I don't think so. Not sure if it would make a lot of sense for GraphQL there. Why not just specify all fields for that relationship which you need, so GraphQL populates those?

  • default discord avatar
    TheDunco
    8 months ago

    Would it work to just specify those fields? Would GraphQL essentially de-reference the relationship then?

  • default discord avatar
    Alessio 🍣
    8 months ago

    yup!

  • default discord avatar
    TheDunco
    8 months ago

    Oh sweet, that's the answer I needed then, thanks! Is that a GraphQL thing or is that behavior implemented by Payload?

  • default discord avatar
    Alessio 🍣
    8 months ago

    good question actually! Prob a mix of both but I've never looked at the implementation

  • default discord avatar
    TheDunco
    8 months ago

    Thanks so much for your help, I verified this will work for my purposes!

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.