Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add defaultPopulate property to collection config #8934

Merged
merged 6 commits into from
Oct 30, 2024

Conversation

r1tsuu
Copy link
Member

@r1tsuu r1tsuu commented Oct 30, 2024

What?

Adds defaultPopulate property to collection config that allows to specify which fields to select when the collection is populated from another document.

import type { CollectionConfig } from 'payload'

// The TSlug generic can be passed to have type safety for `defaultPopulate`.
// If avoided, the `defaultPopulate` type resolves to `SelectType`.
export const Pages: CollectionConfig<'pages'> = {
  slug: 'pages',
  // I need only slug, NOT the WHOLE CONTENT!
  defaultPopulate: {
    slug: true,
  },
  fields: [
    {
      name: 'slug',
      type: 'text',
      required: true,
    },
  ],
}

Why?

This is essential for example in case of links. You don't need the whole document, which can contain large data but only the slug.

How?

Implements defaultPopulate when populating relationships, including inside of lexical / slate rich text fields.

Copy link
Contributor

@DanRibbens DanRibbens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, just the docs fix was all I can tell is wrong.

docs/queries/select.mdx Outdated Show resolved Hide resolved
@DanRibbens DanRibbens merged commit c41ef65 into beta Oct 30, 2024
48 checks passed
@DanRibbens DanRibbens deleted the feat/default-populate branch October 30, 2024 17:41
Copy link
Contributor

🚀 This is included in version v3.0.0-beta.121

DanRibbens pushed a commit that referenced this pull request Nov 6, 2024
### What?
Adds `populate` property to Local API and REST API operations that can
be used to specify `select` for a specific collection when it's
populated
```ts
const result = await payload.findByID({
  populate: {
   // type safe if you have generated types
    posts: {
      text: true,
    },
  },
  collection: 'pages',
  depth: 1,
  id: aboutPage.id,
})

result.relatedPost // only has text and id properties
``` 

```ts
fetch('https://localhost:3000/api/pages?populate[posts][text]=true') // highlight-line
  .then((res) => res.json())
  .then((data) => console.log(data))
```

It also overrides
[`defaultPopulate`](#8934)

Ensures `defaultPopulate` doesn't affect GraphQL.

### How?
Implements the property for all operations that have the `depth`
argument.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants