fix: #2937, depth not being respected in graphql rich text fields

This commit is contained in:
James
2023-06-29 20:08:39 -04:00
parent 0112f4c4ab
commit f84b4323e2
7 changed files with 33 additions and 9 deletions

View File

@@ -11,6 +11,7 @@ function loginResolver(collection: Collection) {
},
req: context.req,
res: context.res,
depth: 0,
};
const result = login(options);

View File

@@ -6,6 +6,7 @@ function meResolver(collection: Collection): any {
const options = {
collection,
req: context.req,
depth: 0,
};
return me(options);
}

View File

@@ -18,6 +18,7 @@ function refreshResolver(collection: Collection) {
token,
req: context.req,
res: context.res,
depth: 0,
};
const result = await refresh(options);

View File

@@ -13,6 +13,7 @@ function resetPasswordResolver(collection: Collection) {
req: context.req,
res: context.res,
api: 'GraphQL',
depth: 0,
};
const result = await resetPassword(options);

View File

@@ -23,6 +23,7 @@ export type Arguments = {
req: PayloadRequest
overrideAccess?: boolean
res?: Response
depth?: number
}
async function resetPassword(args: Arguments): Promise<Result> {
@@ -45,6 +46,7 @@ async function resetPassword(args: Arguments): Promise<Result> {
},
overrideAccess,
data,
depth,
} = args;
// /////////////////////////////////////
@@ -119,7 +121,7 @@ async function resetPassword(args: Arguments): Promise<Result> {
args.res.cookie(`${config.cookiePrefix}-token`, token, cookieOptions);
}
const fullUser = await payload.findByID({ collection: collectionConfig.slug, id: user.id, overrideAccess });
const fullUser = await payload.findByID({ collection: collectionConfig.slug, id: user.id, overrideAccess, depth });
return { token, user: fullUser };
}

View File

@@ -33,13 +33,8 @@ export async function afterRead<T = any>(args: Args): Promise<T> {
const fieldPromises = [];
const populationPromises = [];
let depth = 0;
if (req.payloadAPI === 'REST' || req.payloadAPI === 'local') {
depth = (incomingDepth || incomingDepth === 0) ? parseInt(String(incomingDepth), 10) : req.payload.config.defaultDepth;
if (depth > req.payload.config.maxDepth) depth = req.payload.config.maxDepth;
}
let depth = (incomingDepth || incomingDepth === 0) ? parseInt(String(incomingDepth), 10) : req.payload.config.defaultDepth;
if (depth > req.payload.config.maxDepth) depth = req.payload.config.maxDepth;
const currentDepth = incomingCurrentDepth || 1;

View File

@@ -1,4 +1,5 @@
import type { IndexDirection, IndexOptions } from 'mongoose';
import { GraphQLClient } from 'graphql-request';
import { initPayloadTest } from '../helpers/configHelpers';
import { RESTClient } from '../helpers/rest';
import configPromise from '../uploads/config';
@@ -11,10 +12,14 @@ import { blocksFieldSeedData } from './collections/Blocks';
import { localizedTextValue, namedTabDefaultValue, namedTabText, tabsDoc, tabsSlug } from './collections/Tabs';
import { defaultNumber, numberDoc } from './collections/Number';
import { dateDoc } from './collections/Date';
import type { PaginatedDocs } from '../../src/mongoose/types';
import type { RichTextField } from './payload-types';
let client;
let graphQLClient: GraphQLClient;
let serverURL;
let config;
let token;
describe('Fields', () => {
beforeAll(async () => {
@@ -22,7 +27,9 @@ describe('Fields', () => {
config = await configPromise;
client = new RESTClient(config, { serverURL, defaultSlug: 'point-fields' });
await client.login();
const graphQLURL = `${serverURL}${config.routes.api}${config.routes.graphQL}`;
graphQLClient = new GraphQLClient(graphQLURL);
token = await client.login();
});
describe('text', () => {
@@ -730,5 +737,21 @@ describe('Fields', () => {
expect(typeof child.doc.value.id).toBe('string');
expect(child.doc.value.items).toHaveLength(6);
});
it('should respect rich text depth parameter', async () => {
const query = `query {
RichTextFields {
docs {
richText(depth: 2)
}
}
}`;
const response = await graphQLClient.request(query, {}, {
Authorization: `JWT ${token}`,
});
const { docs }: PaginatedDocs<RichTextField> = response.RichTextFields;
const uploadElement = docs[0].richText.find((el) => el.type === 'upload') as any;
expect(uploadElement.value.media.filename).toStrictEqual('payload.png');
});
});
});