fix(content-releases): exclude release content-types from graphql (#19703)

This commit is contained in:
markkaylor 2024-03-08 14:39:10 +01:00 committed by GitHub
parent 7a700b4bbd
commit a9d79bec77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 9 deletions

View File

@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable node/no-missing-require */
import { ACTIONS } from '../constants';
import { ACTIONS, RELEASE_ACTION_MODEL_UID, RELEASE_MODEL_UID } from '../constants';
const { features } = require('@strapi/strapi/dist/utils/ee');
const { register } = require('../register');
@ -18,18 +18,31 @@ jest.mock('../utils', () => ({
getService: jest.fn(),
}));
const mockGraphQlDisable = jest.fn();
const mockGraphQlShadowCrud = jest.fn(() => ({
disable: mockGraphQlDisable,
}));
describe('register', () => {
const strapi = {
features: {
future: {
isEnabled: () => true,
isEnabled: jest.fn(() => true),
},
},
plugin: jest.fn(() => ({
service: jest.fn(() => ({
addDestroyListenerCallback: jest.fn(),
})),
})),
plugins: {
'content-releases': {
service: jest.fn(() => ({
addDestroyListenerCallback: jest.fn(),
})),
},
graphql: {
service: jest.fn(() => ({
shadowCRUD: mockGraphQlShadowCrud,
})),
},
},
// @ts-expect-error ignore
plugin: (plugin) => strapi.plugins[plugin],
hook: jest.fn(() => ({
register: jest.fn().mockReturnThis(),
})),
@ -50,7 +63,9 @@ describe('register', () => {
it('should register permissions if cms-content-releases feature is enabled', () => {
features.isEnabled.mockReturnValue(true);
register({ strapi });
expect(strapi.admin.services.permission.actionProvider.registerMany).toHaveBeenCalledWith(
ACTIONS
);
@ -58,9 +73,31 @@ describe('register', () => {
it('should not register permissions if cms-content-releases feature is disabled', () => {
features.isEnabled.mockReturnValue(false);
register({ strapi });
expect(strapi.admin.services.permission.actionProvider.registerMany).not.toHaveBeenCalled();
});
it('should exclude the release and release action models from the GraphQL schema when the feature is enabled', async () => {
features.isEnabled.mockReturnValue(true);
await register({ strapi });
expect(mockGraphQlShadowCrud).toHaveBeenNthCalledWith(1, RELEASE_MODEL_UID);
expect(mockGraphQlShadowCrud).toHaveBeenNthCalledWith(2, RELEASE_ACTION_MODEL_UID);
expect(mockGraphQlDisable).toHaveBeenCalledTimes(2);
});
it('should exclude the release and release action models from the GraphQL schema when the feature is disabled', async () => {
features.isEnabled.mockReturnValue(false);
await register({ strapi });
expect(mockGraphQlShadowCrud).toHaveBeenNthCalledWith(1, RELEASE_MODEL_UID);
expect(mockGraphQlShadowCrud).toHaveBeenNthCalledWith(2, RELEASE_ACTION_MODEL_UID);
expect(mockGraphQlDisable).toHaveBeenCalledTimes(2);
});
});
describe('bootstrap', () => {

View File

@ -22,8 +22,10 @@ const getPlugin = () => {
};
}
// We keep returning contentTypes to avoid lost the data if feature is disabled
return {
// Always return register, it handles its own feature check
register,
// Always return contentTypes to avoid losing data when the feature is disabled
contentTypes,
};
};

View File

@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import type { LoadedStrapi } from '@strapi/types';
import { ACTIONS } from './constants';
import { ACTIONS, RELEASE_MODEL_UID, RELEASE_ACTION_MODEL_UID } from './constants';
import {
deleteActionsOnDeleteContentType,
deleteActionsOnDisableDraftAndPublish,
@ -28,4 +28,11 @@ export const register = async ({ strapi }: { strapi: LoadedStrapi }) => {
.register(revalidateChangedContentTypes)
.register(migrateIsValidAndStatusReleases);
}
if (strapi.plugin('graphql')) {
const graphqlExtensionService = strapi.plugin('graphql').service('extension');
// Exclude the release and release action models from the GraphQL schema
graphqlExtensionService.shadowCRUD(RELEASE_MODEL_UID).disable();
graphqlExtensionService.shadowCRUD(RELEASE_ACTION_MODEL_UID).disable();
}
};