Skip to content

Commit

Permalink
Add fallbackLocales to _allXXXLocales
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed May 9, 2023
1 parent 44079aa commit c42c234
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/hooks/sourceNodes/createTypes/item/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -179,14 +165,20 @@ 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,
field.apiKey,
field.localized,
i18n,
);

return resolveForSimpleField(
value,
context,
Expand All @@ -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(
Expand All @@ -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] || {},
);
Expand Down
79 changes: 79 additions & 0 deletions test/__snapshots__/graphql.test.js.snap
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions test/graphql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */ `
{
Expand Down

0 comments on commit c42c234

Please sign in to comment.