Question about array field PUT rest api

default discord avatar
gonzam88
last year
1 1

I have an array field with different data, to which payload adds an ID to each entry.
What I want to achieve is through a PUT request modify a single entry in this array, using this ID, but I can't find a way. Something like
content: {id: thirdItemID, value: newValue}

The only way for something like this to work is to send a request with the whole array and modifying the desired entry.

content: 
[
  {id: firstItemId, value: originalValue},
  {id: secondItemId, value: originalValue},
  {id: thirdItemID, value: newValue}, // <-- this is the one i'm editing
]

While writing this I thought about making a custom endpoint in my collection, but is there a simpler way around this? Thanks in advanced. Please let me know if i'm not being clear : )

  • discord user avatar
    DanRibbens
    Payload Team
    last year

    Hey @gonzam88,

    As you've figured already, sending the PUT with items missing from the array will update the "content" to be missing your other data and there isn't a way to tell Payload to leave them in. The array IDs was added to allow undefined fields to be saved correctly array objects shift around. There isn't a PATCH operation that would give you this today.

    You would need to make a custom endpoint for that. Alternatively you could structure your data to be an array that contains relationships. In that way the collection would have an endpoint that can modify one single item from having it's own collection, but that would have other implications to editor experience.

    Is there something in your implementation that makes it hard to fetch the full document and PUT it all back with the changes you need?
    Have you looked in to doing it with GraphQL? That might be an option but I need to dig more to find out.

    1 reply
  • default discord avatar
    gonzam88
    last year

    Thanks for the reply, just confirming my suspicions. I will PUT the whole array, just trying to check if there was a more straightforward way around this.

Open the post
Continue the discussion in GitHub
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.