feat: add pagination argument to optimize graphql relationships and use in local api (#482)

This commit is contained in:
Dan Ribbens
2022-03-16 14:58:41 -04:00
committed by GitHub
parent ad98b29398
commit 647db5122e
5 changed files with 76 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ export type Arguments = {
depth?: number
req?: PayloadRequest
overrideAccess?: boolean
pagination?: boolean
showHiddenFields?: boolean
draft?: boolean
}
@@ -55,6 +56,7 @@ async function find<T extends TypeWithID = any>(incomingArgs: Arguments): Promis
},
overrideAccess,
showHiddenFields,
pagination = true,
} = args;
// /////////////////////////////////////
@@ -114,6 +116,8 @@ async function find<T extends TypeWithID = any>(incomingArgs: Arguments): Promis
lean: true,
leanWithId: true,
useEstimatedCount,
pagination,
useCustomCountFn: pagination ? undefined : () => Promise.resolve(1),
};
const paginatedDocs = await Model.paginate(query, optionsToExecute);

View File

@@ -12,6 +12,7 @@ export type Options = {
user?: Document
overrideAccess?: boolean
showHiddenFields?: boolean
pagination?: boolean
sort?: string
where?: Where
draft?: boolean
@@ -31,6 +32,7 @@ export default async function find<T extends TypeWithID = any>(options: Options)
showHiddenFields,
sort,
draft = false,
pagination = true,
} = options;
const collection = this.collections[collectionSlug];
@@ -45,6 +47,7 @@ export default async function find<T extends TypeWithID = any>(options: Options)
overrideAccess,
showHiddenFields,
draft,
pagination,
req: {
user,
payloadAPI: 'local',

View File

@@ -117,5 +117,71 @@ describe('Collections - Local', () => {
expect(relationshipBWithHiddenNestedField.post[0].demoHiddenField).toStrictEqual(demoHiddenField);
});
describe('Find', () => {
const title = 'local-find';
beforeAll(async (done) => {
const data = {
title,
description: 'a description',
priority: 1,
nonLocalizedGroup: {
text: 'english',
},
localizedGroup: {
text: 'english',
},
nonLocalizedArray: [
{
localizedEmbeddedText: 'english',
},
],
richTextBlocks: [
{
blockType: 'richTextBlock',
blockName: 'Test Block Name',
content: [{
children: [{ text: 'english' }],
}],
},
],
};
await payload.create({
collection: 'localized-posts',
data,
});
Array.from(Array(10).keys()).map(async (i) => {
const uniqueTitle = `${title}-${i}`;
await payload.create({
collection: 'localized-posts',
data: {
...data,
title: uniqueTitle,
},
});
});
done();
});
it('should find collection with query', async () => {
const result = await payload.find({
collection: 'localized-posts',
where: {
title: {
equals: title,
},
},
});
const doc = result.docs[0];
expect(doc.id).toBeDefined();
expect(doc.title).toStrictEqual(title);
});
it('should allow disable pagination to return all docs', async () => {
const result = await payload.find({
collection: 'localized-posts',
pagination: false,
limit: 5, // limit will not be used
});
expect(result.docs.length).toBeGreaterThan(10);
});
});
});
});

View File

@@ -128,6 +128,7 @@ function buildObjectType(name: string, fields: Field[], parentName: string, base
fallbackLocale,
},
depth: 0,
pagination: false,
};
const relatedDocument = await find(relatedDocumentQuery);
@@ -291,6 +292,7 @@ function buildObjectType(name: string, fields: Field[], parentName: string, base
fallbackLocale,
},
depth: 0,
pagination: false,
});
if (result.docs.length === 1) {