mirror of
https://github.com/vale981/Vulcan
synced 2025-03-05 09:31:43 -05:00
got rid of graphql-tools
'
This commit is contained in:
parent
e474414350
commit
6384bbb12b
2 changed files with 25 additions and 28 deletions
|
@ -41,7 +41,6 @@
|
|||
"graphql-anywhere": "^3.0.1",
|
||||
"graphql-date": "^1.0.2",
|
||||
"graphql-tag": "^2.0.0",
|
||||
"graphql-tools": "^3.0.2",
|
||||
"graphql-type-json": "^0.1.4",
|
||||
"handlebars": "^4.0.5",
|
||||
"he": "^1.1.1",
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// see https://github.com/apollographql/graphql-tools/blob/master/docs/source/schema-directives.md#marking-strings-for-internationalization
|
||||
|
||||
import { addGraphQLDirective, addGraphQLSchema } from '../modules/graphql';
|
||||
import { SchemaDirectiveVisitor } from 'graphql-tools';
|
||||
import { SchemaDirectiveVisitor } from 'apollo-server';
|
||||
import { defaultFieldResolver } from 'graphql';
|
||||
import { Collections } from '../modules/collections';
|
||||
import { getSetting } from '../modules/settings';
|
||||
|
@ -15,8 +15,7 @@ import pickBy from 'lodash/pickBy';
|
|||
Create GraphQL types
|
||||
|
||||
*/
|
||||
const intlValueSchemas =
|
||||
`type IntlValue {
|
||||
const intlValueSchemas = `type IntlValue {
|
||||
locale: String
|
||||
value: String
|
||||
}
|
||||
|
@ -34,7 +33,7 @@ Take an array of translations, a locale, and a default locale, and return a matc
|
|||
const getLocaleString = (translations, locale, defaultLocale) => {
|
||||
const localeObject = translations.find(translation => translation.locale === locale);
|
||||
const defaultLocaleObject = translations.find(translation => translation.locale === defaultLocale);
|
||||
return localeObject && localeObject.value || defaultLocaleObject && defaultLocaleObject.value;
|
||||
return (localeObject && localeObject.value) || (defaultLocaleObject && defaultLocaleObject.value);
|
||||
};
|
||||
|
||||
/*
|
||||
|
@ -44,15 +43,15 @@ GraphQL @intl directive resolver
|
|||
*/
|
||||
class IntlDirective extends SchemaDirectiveVisitor {
|
||||
visitFieldDefinition(field, details) {
|
||||
const { resolve = defaultFieldResolver, name } = field;
|
||||
field.resolve = async function (...args) {
|
||||
const [ doc, graphQLArguments, context ] = args;
|
||||
const { resolve = defaultFieldResolver, name } = field;
|
||||
field.resolve = async function(...args) {
|
||||
const [doc, graphQLArguments, context] = args;
|
||||
const fieldValue = await resolve.apply(this, args);
|
||||
const locale = graphQLArguments.locale || context.locale;
|
||||
const defaultLocale = getSetting('locale');
|
||||
const intlField = doc[`${name}_intl`];
|
||||
// Return string in requested or default language, or else field's original value
|
||||
return intlField && getLocaleString(intlField, locale, defaultLocale) || fieldValue;
|
||||
return (intlField && getLocaleString(intlField, locale, defaultLocale)) || fieldValue;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -66,41 +65,42 @@ addGraphQLSchema('directive @intl on FIELD_DEFINITION');
|
|||
Migration function
|
||||
|
||||
*/
|
||||
const migrateIntlFields = async (defaultLocale) => {
|
||||
|
||||
const migrateIntlFields = async defaultLocale => {
|
||||
if (!defaultLocale) {
|
||||
throw new Error('Please pass the id of the locale to which to migrate your current content (e.g. migrateIntlFields(\'en\'))');
|
||||
throw new Error(
|
||||
'Please pass the id of the locale to which to migrate your current content (e.g. migrateIntlFields(\'en\'))'
|
||||
);
|
||||
}
|
||||
|
||||
Collections.forEach(async collection => {
|
||||
|
||||
Collections.forEach(async collection => {
|
||||
const schema = collection.simpleSchema()._schema;
|
||||
const intlFields = pickBy(schema, isIntlField);
|
||||
const intlFieldsNames = Object.keys(intlFields);
|
||||
|
||||
if (intlFieldsNames.length) {
|
||||
console.log(`### Found ${intlFieldsNames.length} field to migrate for collection ${collection.options.collectionName}: ${intlFieldsNames.join(', ')} ###\n`); // eslint-disable-line no-console
|
||||
console.log(
|
||||
`### Found ${intlFieldsNames.length} field to migrate for collection ${
|
||||
collection.options.collectionName
|
||||
}: ${intlFieldsNames.join(', ')} ###\n`
|
||||
); // eslint-disable-line no-console
|
||||
|
||||
// const intlFieldsWithLocale = intlFieldsNames.map(f => `${f}_intl`);
|
||||
|
||||
// find all documents with one or more unmigrated intl fields
|
||||
const selector = {
|
||||
const selector = {
|
||||
$or: intlFieldsNames.map(f => {
|
||||
return {$and: [
|
||||
{[`${f}`]: { $exists: true }},
|
||||
{[`${f}_intl`]: { $exists: false }}
|
||||
]};
|
||||
return {
|
||||
$and: [{ [`${f}`]: { $exists: true } }, { [`${f}_intl`]: { $exists: false } }]
|
||||
};
|
||||
})
|
||||
};
|
||||
const documentsToMigrate = await Connectors.find(collection, selector);
|
||||
|
||||
if (documentsToMigrate.length) {
|
||||
|
||||
console.log(`-> found ${documentsToMigrate.length} documents to migrate \n`); // eslint-disable-line no-console
|
||||
for (const doc of documentsToMigrate) {
|
||||
|
||||
console.log(`// Migrating document ${doc._id}`); // eslint-disable-line no-console
|
||||
const modifier = { $push: {}};
|
||||
const modifier = { $push: {} };
|
||||
|
||||
intlFieldsNames.forEach(f => {
|
||||
if (doc[f] && !doc[`${f}_intl`]) {
|
||||
|
@ -113,18 +113,16 @@ const migrateIntlFields = async (defaultLocale) => {
|
|||
if (!_.isEmpty(modifier.$push)) {
|
||||
// update document
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const n = await Connectors.update(collection, {_id: doc._id}, modifier);
|
||||
const n = await Connectors.update(collection, { _id: doc._id }, modifier);
|
||||
console.log(`-> migrated ${n} documents \n`); // eslint-disable-line no-console
|
||||
}
|
||||
console.log('\n'); // eslint-disable-line no-console
|
||||
|
||||
}
|
||||
} else {
|
||||
console.log ('-> found no documents to migrate.'); // eslint-disable-line no-console
|
||||
console.log('-> found no documents to migrate.'); // eslint-disable-line no-console
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Vulcan.migrateIntlFields = migrateIntlFields;
|
||||
|
|
Loading…
Add table
Reference in a new issue