mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 23:24:03 +00:00
init
This commit is contained in:
parent
54b51d9d51
commit
a408dc1940
@ -73,6 +73,7 @@ const generateSchema = () => {
|
||||
${Types.addInput()}
|
||||
|
||||
${PublicationState.definition}
|
||||
|
||||
type AdminUser {
|
||||
id: ID!
|
||||
username: String
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
|
||||
module.exports = {
|
||||
definition: `
|
||||
enum PublicationState {
|
||||
LIVE
|
||||
PREVIEW
|
||||
}
|
||||
enum PublicationState {
|
||||
LIVE
|
||||
PREVIEW
|
||||
}
|
||||
`,
|
||||
resolver: {
|
||||
PublicationState: {
|
||||
|
||||
@ -12,6 +12,8 @@ module.exports = () => {
|
||||
const contentTypeService = getService('content-types');
|
||||
const coreApiService = getService('core-api');
|
||||
|
||||
_.set(strapi.plugins.i18n.config, 'schema.graphql', {});
|
||||
|
||||
Object.values(strapi.contentTypes).forEach(contentType => {
|
||||
if (contentTypeService.isLocalized(contentType)) {
|
||||
const { attributes, modelName } = contentType;
|
||||
@ -34,6 +36,7 @@ module.exports = () => {
|
||||
});
|
||||
|
||||
coreApiService.addCreateLocalizationAction(contentType);
|
||||
coreApiService.addGraphqlLocalizationAction(contentType);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@ -110,17 +110,17 @@ const createLocalizationHandler = contentType => {
|
||||
const { findByCode } = getService('locales');
|
||||
|
||||
if (!has('locale', data)) {
|
||||
return ctx.badRequest('locale.missing');
|
||||
throw strapi.errors.badRequest('locale.missing');
|
||||
}
|
||||
|
||||
const matchingLocale = await findByCode(data.locale);
|
||||
if (!matchingLocale) {
|
||||
return ctx.badRequest('locale.invalid');
|
||||
throw strapi.errors.badRequest('locale.invalid');
|
||||
}
|
||||
|
||||
const usedLocales = getAllLocales(entry);
|
||||
if (usedLocales.includes(data.locale)) {
|
||||
return ctx.badRequest('locale.already.used');
|
||||
throw strapi.errors.badRequest('locale.already.used');
|
||||
}
|
||||
|
||||
const sanitizedData = {
|
||||
@ -145,7 +145,7 @@ const createLocalizationHandler = contentType => {
|
||||
const entry = await strapi.query(contentType.uid).findOne();
|
||||
|
||||
if (!entry) {
|
||||
return ctx.notFound('Invalid baseEntityId');
|
||||
throw strapi.errors.notFound('baseEntryId.invalid');
|
||||
}
|
||||
|
||||
await createFromBaseEntry(ctx, entry);
|
||||
@ -158,7 +158,7 @@ const createLocalizationHandler = contentType => {
|
||||
const entry = await strapi.query(contentType.uid).findOne({ id: baseEntryId });
|
||||
|
||||
if (!entry) {
|
||||
return ctx.notFound('baseEntryId.invalid');
|
||||
throw strapi.errors.notFound('baseEntryId.invalid');
|
||||
}
|
||||
|
||||
await createFromBaseEntry(ctx, entry);
|
||||
@ -205,7 +205,42 @@ const addCreateLocalizationAction = contentType => {
|
||||
_.set(strapi, coreApiControllerPath, handler);
|
||||
};
|
||||
|
||||
/**
|
||||
* Add localization mutation & filters to use with the graphql plugin
|
||||
* @param {object} contentType
|
||||
*/
|
||||
const addGraphqlLocalizationAction = contentType => {
|
||||
const { modelName } = contentType;
|
||||
|
||||
if (!strapi.plugins.graphql) {
|
||||
return;
|
||||
}
|
||||
|
||||
const typeName = _.capitalize(modelName);
|
||||
_.mergeWith(
|
||||
strapi.plugins.i18n.config.schema.graphql,
|
||||
{
|
||||
mutation: `
|
||||
create${typeName}Localization(input: update${typeName}Input!): ${typeName}!
|
||||
`,
|
||||
resolver: {
|
||||
Mutation: {
|
||||
[`create${typeName}Localization`]: {
|
||||
resolver: `application::${modelName}.${modelName}.createLocalization`,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
(dest, src) => {
|
||||
if (typeof dest === 'string') {
|
||||
return `${dest}\n${src}`;
|
||||
}
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
addCreateLocalizationAction,
|
||||
addGraphqlLocalizationAction,
|
||||
createSanitizer,
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user