Files
payload/test/dataloader/int.spec.ts
Dan Ribbens 975bbb756f feat: add find to payloadDataLoader to cache local API queries (#11685)
### What?
Extends our dataloader to add a momiozed payload find function. This way
it will cache the query for the same find request using a cacheKey from
find operation args.

### Why?
This was needed internally for `filterOptions` that exist in an array or
other sitautions where you have the same exact query being made and
awaited many times.

### How?

- Added `find` to payloadDataLoader. Marked `@experimental` in case it
needs to change.
- Created a cache key from the args
- Validate filterOptions changed from `payload.find` to
`payloadDataLoader.find`
- Made `payloadDataLoader` no longer optional on `PayloadRequest`, since
other args are required which are created from createLocalReq (context
for example), I don't see a reason why dataLoader shouldn't be required
also.

Example usage: 
```ts
const result = await req.payloadDataLoader.find({
    collection,
    req,
    where,
  })
```
2025-03-18 21:14:33 +00:00

216 lines
5.1 KiB
TypeScript

import type { CollectionSlug, Payload } from 'payload'
import path from 'path'
import { createLocalReq } from 'payload'
import { fileURLToPath } from 'url'
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
import { devUser } from '../credentials.js'
import { initPayloadInt } from '../helpers/initPayloadInt.js'
import { postDoc } from './config.js'
let restClient: NextRESTClient
let payload: Payload
let token: string
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
describe('dataloader', () => {
beforeAll(async () => {
;({ payload, restClient } = await initPayloadInt(dirname))
const loginResult = await payload.login({
collection: 'users',
data: {
email: devUser.email,
password: devUser.password,
},
})
if (loginResult.token) {
token = loginResult.token
}
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('graphql', () => {
it('should allow multiple parallel queries', async () => {
for (let i = 0; i < 100; i++) {
const query = `
query {
Shops {
docs {
name
items {
name
}
}
}
Items {
docs {
name
itemTags {
name
}
}
}
}`
const { data } = await restClient
.GRAPHQL_POST({
body: JSON.stringify({ query }),
headers: {
Authorization: `JWT ${token}`,
},
})
.then((res) => res.json())
const normalizedResponse = JSON.parse(JSON.stringify(data))
expect(normalizedResponse).toStrictEqual({
Items: { docs: [{ name: 'item1', itemTags: [{ name: 'tag1' }] }] },
Shops: { docs: [{ name: 'shop1', items: [{ name: 'item1' }] }] },
})
}
})
it('should allow querying via graphql', async () => {
const query = `query {
Posts {
docs {
title
owner {
email
}
}
}
}`
const { data } = await restClient
.GRAPHQL_POST({
body: JSON.stringify({ query }),
headers: {
Authorization: `JWT ${token}`,
},
})
.then((res) => res.json())
const { docs } = data.Posts
expect(docs[0].title).toStrictEqual(postDoc.title)
})
it('should avoid infinite loops', async () => {
const relationA = await payload.create({
collection: 'relation-a',
data: {
richText: [
{
children: [
{
text: 'relation a',
},
],
},
],
},
})
const relationB = await payload.create({
collection: 'relation-b',
data: {
relationship: relationA.id,
richText: [
{
children: [
{
text: 'relation b',
},
],
},
],
},
})
expect(relationA.id).toBeDefined()
expect(relationB.id).toBeDefined()
await payload.update({
id: relationA.id,
collection: 'relation-a',
data: {
relationship: relationB.id,
richText: [
{
children: [
{
text: 'relation a',
},
],
},
{
type: 'relationship',
children: [
{
text: '',
},
],
relationTo: 'relation-b',
value: {
id: relationB.id,
},
},
],
},
})
const relationANoDepth = await payload.findByID({
id: relationA.id,
collection: 'relation-a',
depth: 0,
})
expect(relationANoDepth.relationship).toStrictEqual(relationB.id)
const relationAWithDepth = await payload.findByID({
id: relationA.id,
collection: 'relation-a',
depth: 4,
})
const innerMostRelationship =
// @ts-expect-error Deep typing not worth doing
relationAWithDepth.relationship.relationship.richText[1].value.relationship.relationship
expect(innerMostRelationship).toStrictEqual(relationB.id)
})
})
describe('find', () => {
it('should call the same query only once in a request', async () => {
const req = await createLocalReq({}, payload)
const spy = jest.spyOn(payload, 'find')
const findArgs = {
collection: 'items' as CollectionSlug,
req,
depth: 0,
where: {
name: { exists: true },
},
}
void req.payloadDataLoader.find(findArgs)
void req.payloadDataLoader.find(findArgs)
await req.payloadDataLoader.find(findArgs)
expect(spy).toHaveBeenCalledTimes(1)
})
})
})