payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
25.71k stars 1.64k forks source link

Local API returns `draft` documents with null fields in production #8077

Closed PlagueFPS closed 1 month ago

PlagueFPS commented 2 months ago

Link to reproduction

No response

Environment Info

"payload": "^3.0.0-beta.97",
"next": "^15.0.0-canary.136",
"node": 21.0.0

Describe the Bug

In production, the local API returns draft documents with the id, _status, updatedAt, and createdAt fields filled in correctly while all other fields are returned as null. The local API shouldn't return drafts at all unless draftMode is enabled in production.

Reproduction Steps

  1. setup payload
  2. enable versions and drafts
  3. create a draft
  4. query that collection with the draft property in the query, passing true if in draftMode or in development and false anywhere else
  5. console.log() the result to find the draft document returned with mostly null fields except the ones explained above.

Adapters and Plugins

No response

paulpopus commented 2 months ago

I tried to reproduce this but couldn't. I'm getting the draft data as expected with my fields filled in.

If you're still encountering this on the latest beta, could you provide a reproduction please?

wkentdag commented 2 months ago

sounds like it could be the same issue as this one: https://github.com/payloadcms/payload/issues/6813

I provided some repro steps there

Edit: nvm sorry 😅

PlagueFPS commented 2 months ago

I tried to reproduce this but couldn't. I'm getting the draft data as expected with my fields filled in.

If you're still encountering this on the latest beta, could you provide a reproduction please?

What? That's even worse, the issue I was stating is that there shouldn't be any drafts being returned at all. It's a production environment why are drafts being returned via the local API if we are not in draftMode

Collexi-Dev commented 2 months ago

sounds like it could be the same issue as this one: #6813

I provided some repro steps there

Commented on that issue but linking here for visibility: https://github.com/payloadcms/payload/issues/6813#issuecomment-2358997794

Also experiencing the same where I am getting draft docs while explicitly specifying drafts: false using the Local API

JarrodMFlesch commented 1 month ago

I am going to close this issue as I do not think it is real.

When querying with draft: false, currently this means the returned documents will be the last published version of a doc or the last saved version if no published versions exist for a document.

If you are looking to query only published documents you will need to constrain your query with

payload.find({
  collections: 'posts',
  where: {
    status: {
      equals: 'published'
    }
  }
})

The current draft argument is more of a "latest" query, as in, give me the newest updates on a document. I hope this helps!

PlagueFPS commented 1 month ago

I am going to close this issue as I do not think it is real.

When querying with draft: false, currently this means the returned documents will be the last published version of a doc or the last saved version if no published versions exist for a document.

If you are looking to query only published documents you will need to constrain your query with

payload.find({ collections: 'posts', where: { status: { equals: 'published' } } }) The current draft argument is more of a "latest" query, as in, give me the newest updates on a document. I hope this helps!

Okay, this was a bit unintuitive compared to other CMSs so I didn't realize that the where clause was needed. My expectation was if draft was true, return to me all published and non-published documents, and those published documents if they had any changes would contain them. This worked, the issue I encountered was when draft was false, it would still return me unpublished documents just with all their fields as null. That was the confusing part, now I understand I need to specify both draft: false and the where clause when working with draftMode.

This adds some additional overhead, but it isn't the end of the world. I appreciated your response Jarrod.

github-actions[bot] commented 1 month ago

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.