diff --git a/src/hooks/sourceNodes/createTypes/item/index.js b/src/hooks/sourceNodes/createTypes/item/index.js index 5348fdf..9ac76b8 100644 --- a/src/hooks/sourceNodes/createTypes/item/index.js +++ b/src/hooks/sourceNodes/createTypes/item/index.js @@ -27,20 +27,6 @@ function getI18n(args, context, info, mainLocale) { }; } -function getAllLocalesI18n(node, args, context, info) { - context.sourceDatocms - .getQueryContext(context) - .localeState.set(info, node.locale); - context.sourceDatocms - .getQueryContext(context) - .fallbackLocalesState.set(info, []); - - return { - locale: node.locale, - fallbacks: {}, - }; -} - module.exports = ({ entitiesRepo, fallbackLocales, @@ -179,7 +165,12 @@ module.exports = ({ value: { type, resolve: (node, args, context, info) => { - const i18n = getAllLocalesI18n(node, args, context, info); + const i18n = getI18n( + { locale: node.locale }, + context, + info, + node.locale, + ); const value = localizedRead( node.entityPayload.attributes, @@ -187,6 +178,7 @@ module.exports = ({ field.localized, i18n, ); + return resolveForSimpleField( value, context, @@ -201,11 +193,11 @@ module.exports = ({ valueNode: { type: nodeType, resolve: (node, args, context, info) => { - const i18n = getAllLocalesI18n( - node, - args, + const i18n = getI18n( + { locale: node.locale }, context, info, + node.locale, ); const value = localizedRead( @@ -232,7 +224,21 @@ module.exports = ({ objectAssign(acc, { [`_all${pascalize(field.apiKey)}Locales`]: { type: `[${allLocalesTypeName}]`, - resolve: node => { + args: { + fallbackLocales: '[String!]', + }, + resolve: (node, args, context, info) => { + const queryContext = context.sourceDatocms.getQueryContext( + context, + ); + + if (args.fallbackLocales) { + queryContext.fallbackLocalesState.set( + info, + args.fallbackLocales, + ); + } + const locales = Object.keys( node.entityPayload.attributes[field.apiKey] || {}, ); diff --git a/test/__snapshots__/graphql.test.js.snap b/test/__snapshots__/graphql.test.js.snap index 4258cde..eb12c53 100644 --- a/test/__snapshots__/graphql.test.js.snap +++ b/test/__snapshots__/graphql.test.js.snap @@ -1,5 +1,84 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`_allXXXLocales with fallback 1`] = ` +Object { + "data": Object { + "fallbackAllLocales": Object { + "nodes": Array [ + Object { + "_allTitleLocales": Array [ + Object { + "locale": "en", + "value": "English title", + }, + ], + }, + Object { + "_allTitleLocales": Array [ + Object { + "locale": "en", + "value": "English title", + }, + Object { + "locale": "it", + "value": "English title", + }, + ], + }, + ], + }, + "fallbackToplevel": Object { + "nodes": Array [ + Object { + "_allTitleLocales": Array [ + Object { + "locale": "en", + "value": "English title", + }, + ], + }, + Object { + "_allTitleLocales": Array [ + Object { + "locale": "en", + "value": "English title", + }, + Object { + "locale": "it", + "value": "English title", + }, + ], + }, + ], + }, + "noFallback": Object { + "nodes": Array [ + Object { + "_allTitleLocales": Array [ + Object { + "locale": "en", + "value": "English title", + }, + ], + }, + Object { + "_allTitleLocales": Array [ + Object { + "locale": "en", + "value": "English title", + }, + Object { + "locale": "it", + "value": null, + }, + ], + }, + ], + }, + }, +} +`; + exports[`assets 1`] = ` Object { "data": Object { diff --git a/test/graphql.test.js b/test/graphql.test.js index 4d83307..e4d9f4a 100644 --- a/test/graphql.test.js +++ b/test/graphql.test.js @@ -847,6 +847,43 @@ test('items', async () => { expect(result).toMatchSnapshot(); }); +test('_allXXXLocales with fallback', async () => { + const query = /* GraphQL */ ` + { + noFallback: allDatoCmsOptionalLocalesModel { + nodes { + _allTitleLocales { + locale + value + } + } + } + fallbackToplevel: allDatoCmsOptionalLocalesModel( + fallbackLocales: ["en"] + ) { + nodes { + _allTitleLocales { + locale + value + } + } + } + fallbackAllLocales: allDatoCmsOptionalLocalesModel { + nodes { + _allTitleLocales(fallbackLocales: ["en"]) { + locale + value + } + } + } + } + `; + + const result = await executeQuery(query); + + expect(result).toMatchSnapshot(); +}); + test('items (parallel)', async () => { const getQuery = locale => /* GraphQL */ ` {