2021-09-01 12:06:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
2021-09-08 16:38:46 +02:00
|
|
|
const { prop, propEq, identity } = require('lodash/fp');
|
2021-09-07 11:23:49 +02:00
|
|
|
|
2021-09-17 07:16:30 +02:00
|
|
|
const LOCALE_SCALAR_TYPENAME = 'I18NLocaleCode';
|
2021-09-07 11:23:49 +02:00
|
|
|
const LOCALE_ARG_PLUGIN_NAME = 'I18NLocaleArg';
|
|
|
|
|
2021-09-01 12:06:51 +02:00
|
|
|
module.exports = ({ strapi }) => ({
|
|
|
|
register() {
|
2021-09-08 16:38:46 +02:00
|
|
|
const { service: getGraphQLService } = strapi.plugin('graphql');
|
|
|
|
const { service: getI18NService } = strapi.plugin('i18n');
|
|
|
|
|
2021-09-17 07:16:30 +02:00
|
|
|
const extensionService = getGraphQLService('extension');
|
|
|
|
|
2021-09-08 16:38:46 +02:00
|
|
|
const getCreateLocalizationMutationType = contentType => {
|
|
|
|
const { getTypeName } = getGraphQLService('utils').naming;
|
|
|
|
|
|
|
|
return `create${getTypeName(contentType)}Localization`;
|
|
|
|
};
|
|
|
|
|
2021-09-17 07:16:30 +02:00
|
|
|
extensionService.shadowCRUD('plugin::i18n.locale').disableMutations();
|
|
|
|
|
|
|
|
extensionService.use(({ nexus, typeRegistry }) => {
|
|
|
|
const i18nLocaleArgPlugin = getI18nLocaleArgPlugin({ nexus, typeRegistry });
|
|
|
|
const i18nLocaleScalar = getLocaleScalar({ nexus });
|
|
|
|
const createLocalizationMutations = getCreateLocalizationMutations({
|
2021-09-15 15:44:42 +02:00
|
|
|
nexus,
|
|
|
|
typeRegistry,
|
2021-09-07 11:23:49 +02:00
|
|
|
});
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-15 15:44:42 +02:00
|
|
|
return {
|
|
|
|
plugins: [i18nLocaleArgPlugin],
|
|
|
|
types: [i18nLocaleScalar, createLocalizationMutations],
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-09-17 07:16:30 +02:00
|
|
|
const getLocaleScalar = ({ nexus }) => {
|
2021-09-08 16:38:46 +02:00
|
|
|
const locales = getI18NService('iso-locales').getIsoLocales();
|
|
|
|
|
|
|
|
return nexus.scalarType({
|
|
|
|
name: LOCALE_SCALAR_TYPENAME,
|
|
|
|
|
|
|
|
description: 'A string used to identify an i18n locale',
|
|
|
|
|
|
|
|
serialize: identity,
|
|
|
|
parseValue: identity,
|
|
|
|
|
|
|
|
parseLiteral(ast) {
|
|
|
|
if (ast.kind !== 'StringValue') {
|
|
|
|
throw new TypeError('Locale cannot represent non string type');
|
|
|
|
}
|
|
|
|
|
|
|
|
const isValidLocale = locales.find(propEq('code', ast.value));
|
|
|
|
|
|
|
|
if (!isValidLocale) {
|
|
|
|
throw new TypeError('Unknown locale supplied');
|
|
|
|
}
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-08 16:38:46 +02:00
|
|
|
return ast.value;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-09-17 07:16:30 +02:00
|
|
|
const getCreateLocalizationMutations = ({ nexus, typeRegistry }) => {
|
2021-09-08 16:38:46 +02:00
|
|
|
const { KINDS } = getGraphQLService('constants');
|
|
|
|
const { isLocalizedContentType } = getI18NService('content-types');
|
|
|
|
|
|
|
|
const localizedContentTypes = typeRegistry
|
|
|
|
.where(
|
|
|
|
({ config }) => config.kind === KINDS.type && isLocalizedContentType(config.contentType)
|
|
|
|
)
|
|
|
|
.map(prop('config.contentType'));
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-17 07:16:30 +02:00
|
|
|
return localizedContentTypes.map(ct => getCreateLocalizationMutation(ct, { nexus }));
|
2021-09-08 16:38:46 +02:00
|
|
|
};
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-17 07:16:30 +02:00
|
|
|
const getCreateLocalizationMutation = (contentType, { nexus }) => {
|
2021-09-08 16:38:46 +02:00
|
|
|
const { getEntityResponseName, getContentTypeInputName } = getGraphQLService('utils').naming;
|
|
|
|
const { createCreateLocalizationHandler } = getI18NService('core-api');
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-08 16:38:46 +02:00
|
|
|
const responseType = getEntityResponseName(contentType);
|
|
|
|
const mutationName = getCreateLocalizationMutationType(contentType);
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-08 16:38:46 +02:00
|
|
|
const resolverHandler = createCreateLocalizationHandler(contentType);
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-08 16:38:46 +02:00
|
|
|
return nexus.extendType({
|
|
|
|
type: 'Mutation',
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-08 16:38:46 +02:00
|
|
|
definition(t) {
|
|
|
|
t.field(mutationName, {
|
|
|
|
type: responseType,
|
2021-09-07 11:23:49 +02:00
|
|
|
|
2021-09-08 16:38:46 +02:00
|
|
|
// The locale arg will be automatically added through the i18n graphql extension
|
|
|
|
args: {
|
|
|
|
id: 'ID',
|
|
|
|
data: getContentTypeInputName(contentType),
|
|
|
|
},
|
|
|
|
|
|
|
|
async resolve(parent, args) {
|
|
|
|
const value = await resolverHandler(args);
|
|
|
|
|
|
|
|
return { value, info: { args, resourceUID: contentType.uid } };
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2021-09-07 11:23:49 +02:00
|
|
|
|
2021-09-17 07:16:30 +02:00
|
|
|
const getI18nLocaleArgPlugin = ({ nexus, typeRegistry }) => {
|
2021-09-08 16:38:46 +02:00
|
|
|
const { isLocalizedContentType } = getI18NService('content-types');
|
2021-09-07 11:23:49 +02:00
|
|
|
|
2021-09-08 16:38:46 +02:00
|
|
|
const addLocaleArg = config => {
|
|
|
|
const { parentType } = config;
|
2021-09-07 11:23:49 +02:00
|
|
|
|
2021-09-08 16:38:46 +02:00
|
|
|
// Only target queries or mutations
|
|
|
|
if (parentType !== 'Query' && parentType !== 'Mutation') {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-07 11:23:49 +02:00
|
|
|
|
2021-09-10 15:52:19 +02:00
|
|
|
const registryType = typeRegistry.get(config.type);
|
|
|
|
|
|
|
|
if (!registryType) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const contentType = registryType.config.contentType;
|
2021-09-07 11:23:49 +02:00
|
|
|
|
2021-09-08 16:38:46 +02:00
|
|
|
const createLocalizationMutationType = getCreateLocalizationMutationType(contentType);
|
2021-09-07 11:23:49 +02:00
|
|
|
|
2021-09-08 16:38:46 +02:00
|
|
|
// Ignore non-localized content types
|
|
|
|
if (
|
|
|
|
!isLocalizedContentType(contentType) ||
|
|
|
|
// Don't add the "locale" arg on "create localization" mutations
|
|
|
|
config.name === createLocalizationMutationType
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-07 11:23:49 +02:00
|
|
|
|
2021-09-08 16:38:46 +02:00
|
|
|
config.args.locale = nexus.arg({ type: LOCALE_SCALAR_TYPENAME });
|
|
|
|
};
|
|
|
|
|
|
|
|
return nexus.plugin({
|
|
|
|
name: LOCALE_ARG_PLUGIN_NAME,
|
|
|
|
|
|
|
|
onAddOutputField(config) {
|
|
|
|
// Add the locale arg to the queries on localized CTs
|
|
|
|
addLocaleArg(config);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|