Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Depth of `doc` args in `CollectionAfterChangeHook`

default discord avatar
allannnclast year
12

Hello!



I'm on payload

1.10.1

In a

CollectionAfterChangeHook

, TS is telling me that

updatedDoc.someRelationField: string | RelatedCollection

, so I have to add conditionals for this



But, I can't think of a scenario where the

updatedDoc

would come through with

depth > 0

.



Can I safely assume that

depth=0

in this hook to avoid an

if

statement? I would prefer to do something like

updatedDoc.someRelationshipField as string

if we can assume this

  • default discord avatar
    notchrlast year

    @allannnc Good morning!



    I believe it can be a string or related collection, because if the depth is specified to shallow, the document wont be returned, only the document ID (string)



    I think your

    as

    solution is acceptable in this case, as the types are to help you as a tool but you have the final say

  • default discord avatar
    allannnclast year

    Thanks for the answer!



    The catch is I'm not sure what the final say is though - it boils down to how does this get populated?

  • default discord avatar
    notchrlast year

    Good question! I'm not 100% sure, but some other members here may be able to provide some details.



    What do you mean by populated?

  • default discord avatar
    allannnclast year

    Yaknow, req comes in, docs get attached

  • It gets populated in the afterRead field hook (before the collection afterChange hook), which takes into account

    depth

    . So if you run a payload.update and use a depth param, the docs may or may not be populated depending on depth number

  • default discord avatar
    ssyberglast year

    @jarrod_not_jared I just stumbled on this and there does appear to be some default depth in these hooks on an

    payload.update

    in my testing I was seeing nested relationships fully available in a collection

    afterChange

    hook, even when I specify no

    depth

    is that expected behavior?

  • I believe it is 1 by default

  • default discord avatar
    ssyberglast year

    hmm def seeing deeper than that

  • looks like 2



    I used a recursive relationship to test



    But I feel like the point still stands, if you pass depth: 0 inside the payload.update, then the type will be string

    CleanShot_2023-08-24_at_15.45.44.png
  • default discord avatar
    ssyberglast year

    cool

  • default discord avatar
    steadysnaillast year

    I've been running into a similar/related issue quite regularly, that definitely seems like a bug. I opened an issue on github:

    https://github.com/payloadcms/payload/discussions/3218

    . The depth/format of the doc argument of several of my CollectionAfterChangeHook's differs depending on whether the "change" was caused by a manual user action or a programatic change (such as another hook). I am not setting the depth in any of my payload.update/payload.create calls that would effect the hooks.

  • default discord avatar
    ssyberglast year

    Yea I'm finding this is breaking some of our filters and related functionality, but it's been very hard to pinpoint the issue

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

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