feat: select fields (#8550)

Adds `select` which is used to specify the field projection for local
and rest API calls. This is available as an optimization to reduce the
payload's of requests and make the database queries more efficient.

Includes:
- [x] generate types for the `select` property
- [x] infer the return type by `select` with 2 modes - include (`field:
true`) and exclude (`field: false`)
- [x] lots of integration tests, including deep fields / localization
etc
- [x] implement the property in db adapters
- [x] implement the property in the local api for most operations
- [x] implement the property in the rest api 
- [x] docs

---------

Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
This commit is contained in:
Sasha
2024-10-29 23:47:18 +02:00
committed by GitHub
parent 6cdf141380
commit dae832c288
116 changed files with 5491 additions and 371 deletions

View File

@@ -1,4 +1,4 @@
import type { JoinQuery, SanitizedConfig, Where } from 'payload'
import type { JoinQuery, SanitizedConfig, SelectType, Where } from 'payload'
import type { ParsedQs } from 'qs-esm'
import {
@@ -22,6 +22,7 @@ type RequestOptions = {
limit?: number
locale?: string
page?: number
select?: SelectType
sort?: string
where?: Where
}
@@ -44,15 +45,27 @@ function generateQueryString(query: RequestOptions['query'], params: ParsedQs):
}
export class NextRESTClient {
private _DELETE: (request: Request, args: { params: { slug: string[] } }) => Promise<Response>
private _DELETE: (
request: Request,
args: { params: Promise<{ slug: string[] }> },
) => Promise<Response>
private _GET: (request: Request, args: { params: { slug: string[] } }) => Promise<Response>
private _GET: (
request: Request,
args: { params: Promise<{ slug: string[] }> },
) => Promise<Response>
private _GRAPHQL_POST: (request: Request) => Promise<Response>
private _PATCH: (request: Request, args: { params: { slug: string[] } }) => Promise<Response>
private _PATCH: (
request: Request,
args: { params: Promise<{ slug: string[] }> },
) => Promise<Response>
private _POST: (request: Request, args: { params: { slug: string[] } }) => Promise<Response>
private _POST: (
request: Request,
args: { params: Promise<{ slug: string[] }> },
) => Promise<Response>
private readonly config: SanitizedConfig
@@ -62,7 +75,9 @@ export class NextRESTClient {
constructor(config: SanitizedConfig) {
this.config = config
if (config?.serverURL) {this.serverURL = config.serverURL}
if (config?.serverURL) {
this.serverURL = config.serverURL
}
this._GET = createGET(config)
this._POST = createPOST(config)
this._DELETE = createDELETE(config)
@@ -118,7 +133,7 @@ export class NextRESTClient {
headers: this.buildHeaders(options),
method: 'DELETE',
})
return this._DELETE(request, { params: { slug } })
return this._DELETE(request, { params: Promise.resolve({ slug }) })
}
async GET(
@@ -134,7 +149,7 @@ export class NextRESTClient {
headers: this.buildHeaders(options),
method: 'GET',
})
return this._GET(request, { params: { slug } })
return this._GET(request, { params: Promise.resolve({ slug }) })
}
async GRAPHQL_POST(options: RequestInit & RequestOptions): Promise<Response> {
@@ -190,7 +205,7 @@ export class NextRESTClient {
headers: this.buildHeaders(options),
method: 'PATCH',
})
return this._PATCH(request, { params: { slug } })
return this._PATCH(request, { params: Promise.resolve({ slug }) })
}
async POST(
@@ -204,6 +219,6 @@ export class NextRESTClient {
headers: this.buildHeaders(options),
method: 'POST',
})
return this._POST(request, { params: { slug } })
return this._POST(request, { params: Promise.resolve({ slug }) })
}
}