2021-09-01 12:06:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
2021-09-07 11:23:49 +02:00
|
|
|
const { propEq, identity } = require('lodash/fp');
|
|
|
|
|
|
|
|
const LOCALE_SCALAR_TYPENAME = 'Locale';
|
|
|
|
const LOCALE_ARG_PLUGIN_NAME = 'I18NLocaleArg';
|
|
|
|
|
2021-09-01 12:06:51 +02:00
|
|
|
module.exports = ({ strapi }) => ({
|
|
|
|
register() {
|
2021-09-07 11:23:49 +02:00
|
|
|
strapi
|
2021-09-01 12:06:51 +02:00
|
|
|
.plugin('graphql')
|
|
|
|
.service('extension')
|
2021-09-07 11:23:49 +02:00
|
|
|
.for('content-api')
|
|
|
|
.use(({ nexus, typeRegistry }) => {
|
|
|
|
const i18nLocaleArgPlugin = createI18nLocaleArgPlugin({ nexus, strapi, typeRegistry });
|
|
|
|
const i18nLocaleScalar = createLocaleScalar({ nexus, strapi });
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-07 11:23:49 +02:00
|
|
|
return {
|
|
|
|
plugins: [i18nLocaleArgPlugin],
|
|
|
|
types: [i18nLocaleScalar],
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-07 11:23:49 +02:00
|
|
|
const createLocaleScalar = ({ nexus, strapi }) => {
|
|
|
|
const locales = strapi
|
|
|
|
.plugin('i18n')
|
|
|
|
.service('iso-locales')
|
|
|
|
.getIsoLocales();
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-07 11:23:49 +02:00
|
|
|
return nexus.scalarType({
|
|
|
|
name: LOCALE_SCALAR_TYPENAME,
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-07 11:23:49 +02:00
|
|
|
description: 'A string used to identify an i18n locale',
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-07 11:23:49 +02:00
|
|
|
serialize: identity,
|
|
|
|
parseValue: identity,
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-07 11:23:49 +02:00
|
|
|
parseLiteral(ast) {
|
|
|
|
if (ast.kind !== 'StringValue') {
|
|
|
|
throw new TypeError('Locale cannot represent non string type');
|
|
|
|
}
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-07 11:23:49 +02:00
|
|
|
const isValidLocale = locales.find(propEq('code', ast.value));
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-07 11:23:49 +02:00
|
|
|
if (!isValidLocale) {
|
|
|
|
throw new TypeError('Unknown locale supplied');
|
|
|
|
}
|
2021-09-01 12:06:51 +02:00
|
|
|
|
2021-09-07 11:23:49 +02:00
|
|
|
return ast.value;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const createI18nLocaleArgPlugin = ({ nexus, strapi, typeRegistry }) => {
|
|
|
|
const { isLocalizedContentType } = strapi.plugin('i18n').service('content-types');
|
|
|
|
|
|
|
|
const addLocaleArg = config => {
|
|
|
|
const { parentType } = config;
|
|
|
|
|
|
|
|
// Only target queries or mutations
|
|
|
|
if (parentType !== 'Query' && parentType !== 'Mutation') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const contentType = typeRegistry.get(config.type).config.contentType;
|
|
|
|
|
|
|
|
// Ignore non-localized content types
|
|
|
|
if (!isLocalizedContentType(contentType)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|