feat: refactors buildQuery to rely on fields instead of mongoose

This commit is contained in:
James
2023-04-17 16:08:44 -04:00
parent 2d0441a72e
commit d187b809d7
14 changed files with 487 additions and 256 deletions

View File

@@ -4,6 +4,8 @@ import { buildConfig } from '../buildConfig';
export const slug = 'global';
export const arraySlug = 'array';
export const accessControlSlug = 'access-control';
export const englishLocale = 'en';
export const spanishLocale = 'es';
@@ -51,6 +53,33 @@ export default buildConfig({
},
],
},
{
slug: accessControlSlug,
access: {
read: ({ req: { user } }) => {
if (user) {
return true;
}
return {
enabled: {
equals: true,
},
};
},
},
fields: [
{
name: 'title',
type: 'text',
required: true,
},
{
name: 'enabled',
type: 'checkbox',
},
],
},
],
onInit: async (payload) => {
await payload.create({
@@ -60,5 +89,12 @@ export default buildConfig({
password: devUser.password,
},
});
await payload.updateGlobal({
slug: accessControlSlug,
data: {
title: 'hello',
},
});
},
});

View File

@@ -1,6 +1,6 @@
import { GraphQLClient } from 'graphql-request';
import { initPayloadTest } from '../helpers/configHelpers';
import configPromise, { arraySlug, englishLocale, slug, spanishLocale } from './config';
import configPromise, { accessControlSlug, arraySlug, englishLocale, slug, spanishLocale } from './config';
import payload from '../../src';
import { RESTClient } from '../helpers/rest';
@@ -144,6 +144,29 @@ describe('globals', () => {
expect(en).toMatchObject(localized.en);
expect(es).toMatchObject(localized.es);
});
it('should respect valid access query constraint', async () => {
const emptyGlobal = await payload.findGlobal({
slug: accessControlSlug,
overrideAccess: false,
});
expect(Object.keys(emptyGlobal)).toHaveLength(0);
await payload.updateGlobal({
slug: accessControlSlug,
data: {
enabled: true,
},
});
const hasAccess = await payload.findGlobal({
slug: accessControlSlug,
overrideAccess: false,
});
expect(hasAccess.title).toBeDefined();
});
});
describe('graphql', () => {