### 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`](https://github.com/payloadcms/payload/pull/8934)
Ensures `defaultPopulate` doesn't affect GraphQL.
### How?
Implements the property for all operations that have the `depth`
argument.
66 lines
1.6 KiB
TypeScript
66 lines
1.6 KiB
TypeScript
import type { CollectionSlug, Payload, RequestContext, TypedLocale } from '../../../index.js'
|
|
import type { Document, PayloadRequest, PopulateType, SelectType } from '../../../types/index.js'
|
|
import type { DataFromCollectionSlug } from '../../config/types.js'
|
|
|
|
import { APIError } from '../../../errors/index.js'
|
|
import { createLocalReq } from '../../../utilities/createLocalReq.js'
|
|
import { restoreVersionOperation } from '../restoreVersion.js'
|
|
|
|
export type Options<TSlug extends CollectionSlug> = {
|
|
collection: TSlug
|
|
/**
|
|
* context, which will then be passed to req.context, which can be read by hooks
|
|
*/
|
|
context?: RequestContext
|
|
depth?: number
|
|
draft?: boolean
|
|
fallbackLocale?: TypedLocale
|
|
id: string
|
|
locale?: TypedLocale
|
|
overrideAccess?: boolean
|
|
populate?: PopulateType
|
|
req?: PayloadRequest
|
|
select?: SelectType
|
|
showHiddenFields?: boolean
|
|
user?: Document
|
|
}
|
|
|
|
export default async function restoreVersionLocal<TSlug extends CollectionSlug>(
|
|
payload: Payload,
|
|
options: Options<TSlug>,
|
|
): Promise<DataFromCollectionSlug<TSlug>> {
|
|
const {
|
|
id,
|
|
collection: collectionSlug,
|
|
depth,
|
|
overrideAccess = true,
|
|
populate,
|
|
select,
|
|
showHiddenFields,
|
|
} = options
|
|
|
|
const collection = payload.collections[collectionSlug]
|
|
|
|
if (!collection) {
|
|
throw new APIError(
|
|
`The collection with slug ${String(
|
|
collectionSlug,
|
|
)} can't be found. Restore Version Operation.`,
|
|
)
|
|
}
|
|
|
|
const args = {
|
|
id,
|
|
collection,
|
|
depth,
|
|
overrideAccess,
|
|
payload,
|
|
populate,
|
|
req: await createLocalReq(options, payload),
|
|
select,
|
|
showHiddenFields,
|
|
}
|
|
|
|
return restoreVersionOperation(args)
|
|
}
|