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.

Filter related doc fields in local API?

default discord avatar
arielarial10 months ago
2

Hi!


Is it possible to filter which fields will be returned from a document in a relationship field?



I'm building a "Header" global. In it there's an array field thats being used to populate a menu, using a relationship field to the Pages collection.



I'm wondering if there's a way to get only "slug" and "title", for example, instead of getting all the pages with all the content for each one and then filtering them in getStaticProps in some way.



Maybe a way would be to create hidden fields that read the related page and store slug and title, and use afterRead hook to return the related page (the whole document) as empty? It seems that it should be easier, maybe?



Thanks!



Hi

@231592602846232576

, thanks a lot! I was hoping that I missed something in the documentation. But the way you're doing is simple enough and will work just fine for me.



I remember reading somewhere in the docs that is not recommended to change the field type in hooks, and in fact the hook won't change payload-types, so this can lead to some problems in the future. That's why I was thinking about making a separate field (maybe a JSON field). But for now this will do 😬 . Thanks again!



Thats neat! Didn't know about this

  • default discord avatar
    Payload-Bot11 months ago


  • default discord avatar
    justono11 months ago
    @736673642158620680

    I am just learning payload (v2) FYI so take it for what you will. I may have possibly encountered something that would help you. I think basically what you said with the afterRead hook would be a path forward. Just for some context, in my relationship field I'm sort of creating a computed display only field and I was querying assets assigned to an employee and in the end I was returning just the assets ID. I wonder if you just update where my map is in my afterRead hook that you could return any shape or limtied set of data that you would want.



    // Employees collection config
    ...
    {
       name: "assets",
       type: "relationship",
       relationTo: "assets",
       hooks: {
         afterRead: [getAllEmployeeAssets],
       },
    }]}
    
    const getAllEmployeeAssets: FieldHook = async ({ data }) => {
      // Use Local API to get the assets
      const allAssets = await payload.find({
        collection: "assets",
        where: {
          employee: {
            equals: data.id,
          },
        },
      });
      if (allAssets.docs) {
        // Map over and return anything you want (I'm just returning the id)
        return allAssets.docs.map((doc) => doc.id);
      }
    
      return null;
    };


    This blog post isn't SUPER realted but you could see how if you did end up making another field to hold a filtered set of data. Reacting to and updating other fields isn't too bad with their form hooks. Also I think this showcases a pretty interesting use case and also the power of the array field.

    https://payloadcms.com/blog/managing-array-and-block-rows

    You might be able to create and array of other fields then you get types etc cause everything is just fields instead of a JSON object. But depending on how complex you want go this seems like bit to manage where JSON field prolly is better.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.