diff --git a/src/hooks/createResolvers/index.js b/src/hooks/createResolvers/index.js index 8f03408..67efe39 100644 --- a/src/hooks/createResolvers/index.js +++ b/src/hooks/createResolvers/index.js @@ -35,22 +35,16 @@ module.exports = ( fallbackLocales: { type: '[String!]' }, }, resolve: async (parent, args, context, info) => { - if (context.sourceDatocms.localeState) { - context.sourceDatocms.localeState.clear(info); - } - if (context.sourceDatocms.fallbackLocales) { - context.sourceDatocms.fallbackLocales.clear(info); - } - if (args.locale) { - context.sourceDatocms.localeState.set(info, args.locale); + sourceDatocms + .getQueryContext(context) + .localeState.set(info, args.locale); } if (args.fallbackLocales) { - context.sourceDatocms.fallbackLocalesState.set( - info, - args.fallbackLocales, - ); + context.sourceDatocms + .getQueryContext(context) + .fallbackLocalesState.set(info, args.fallbackLocales); } const { locale, fallbackLocales, ...argsWithoutLocale } = args; diff --git a/src/hooks/createSchemaCustomization/index.js b/src/hooks/createSchemaCustomization/index.js index af47e0f..6e04ab9 100644 --- a/src/hooks/createSchemaCustomization/index.js +++ b/src/hooks/createSchemaCustomization/index.js @@ -26,10 +26,20 @@ module.exports = async ( logApiCalls, }, ) => { - const localeState = new CascadedContext({ reporter }); - const fallbackLocalesState = new CascadedContext({ reporter }); - - actions.createResolverContext({ localeState, fallbackLocalesState }); + const statePerQuery = new WeakMap(); + actions.createResolverContext({ + getQueryContext: key => { + let queryState = statePerQuery.get(key); + if (!queryState) { + queryState = { + localeState: new CascadedContext({ reporter }), + fallbackLocalesState: new CascadedContext({ reporter }), + }; + statePerQuery.set(key, queryState); + } + return queryState; + }, + }); if (!apiToken) { const errorText = `API token must be provided!`; diff --git a/src/hooks/sourceNodes/createTypes/item/index.js b/src/hooks/sourceNodes/createTypes/item/index.js index 417c688..867069e 100644 --- a/src/hooks/sourceNodes/createTypes/item/index.js +++ b/src/hooks/sourceNodes/createTypes/item/index.js @@ -8,26 +8,39 @@ const { seoTagsBuilder, JsonApiEntity } = require('datocms-client'); function getI18n(args, context, info, mainLocale) { if (args.locale) { - context.sourceDatocms.localeState.set(info, args.locale); + context.sourceDatocms + .getQueryContext(context) + .localeState.set(info, args.locale); } if (args.fallbackLocales) { - context.sourceDatocms.fallbackLocalesState.set(info, args.fallbackLocales); + context.sourceDatocms + .getQueryContext(context) + .fallbackLocalesState.set(info, args.fallbackLocales); } - const locale = context.sourceDatocms.localeState.get(info) || mainLocale; + const locale = + context.sourceDatocms.getQueryContext(context).localeState.get(info) || + mainLocale; return { locale, fallbacks: { - [locale]: context.sourceDatocms.fallbackLocalesState.get(info) || [], + [locale]: + context.sourceDatocms + .getQueryContext(context) + .fallbackLocalesState.get(info) || [], }, }; } function getAllLocalesI18n(node, args, context, info) { - context.sourceDatocms.localeState.set(info, node.locale); - context.sourceDatocms.fallbackLocalesState.set(info, []); + context.sourceDatocms + .getQueryContext(context) + .localeState.set(info, node.locale); + context.sourceDatocms + .getQueryContext(context) + .fallbackLocalesState.set(info, []); return { locale: node.locale, diff --git a/src/hooks/sourceNodes/createTypes/site/DatoCmsSite.js b/src/hooks/sourceNodes/createTypes/site/DatoCmsSite.js index 48f2690..336435d 100644 --- a/src/hooks/sourceNodes/createTypes/site/DatoCmsSite.js +++ b/src/hooks/sourceNodes/createTypes/site/DatoCmsSite.js @@ -2,19 +2,28 @@ const { localizedRead } = require('datocms-client'); function getI18n(args, context, info, mainLocale) { if (args.locale) { - context.sourceDatocms.localeState.set(info, args.locale); + context.sourceDatocms + .getQueryContext(context) + .localeState.set(info, args.locale); } if (args.fallbackLocales) { - context.sourceDatocms.fallbackLocalesState.set(info, args.fallbackLocales); + context.sourceDatocms + .getQueryContext(context) + .fallbackLocalesState.set(info, args.fallbackLocales); } - const locale = context.sourceDatocms.localeState.get(info) || mainLocale; + const locale = + context.sourceDatocms.getQueryContext(context).localeState.get(info) || + mainLocale; return { locale, fallbacks: { - [locale]: context.sourceDatocms.fallbackLocalesState.get(info) || [], + [locale]: + context.sourceDatocms + .getQueryContext(context) + .fallbackLocalesState.get(info) || [], }, }; }