From 6c96171f22be4795078ba6be828e28a6ed1cf180 Mon Sep 17 00:00:00 2001 From: pieh Date: Mon, 9 Jan 2023 12:15:14 +0100 Subject: [PATCH 1/2] add failing test --- test/graphql.test.js | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/test/graphql.test.js b/test/graphql.test.js index 10f8b4c..1d1cdbe 100644 --- a/test/graphql.test.js +++ b/test/graphql.test.js @@ -798,6 +798,53 @@ test('items', async () => { expect(result).toMatchSnapshot(); }); +test('items (parallel)', async () => { + const getQuery = locale => /* GraphQL */ ` + { + article: datoCmsArticle(originalId: {eq: "7364344"}, locale: "${locale}") { + __typename + originalId + id + locales + singleLineString + singleLink { + id + singleLineString + } + multipleLinks { + id + singleLineString + } + advancedMultipleLinks { + __typename + ... on DatoCmsArticle { + singleLineString + } + ... on DatoCmsSecondaryModel { + title + } + } + } + allDatoCmsOptionalLocalesModel(locale: "it", fallbackLocales: ["en"]) { + nodes { + originalId + title + boolean + } + } + } + `; + + const [result1, result2] = await Promise.all([ + executeQuery(getQuery(`en`)), + executeQuery(getQuery(`it`)), + ]); + + // we compare results of queries running in parallel to results of queries running one by one + expect(await executeQuery(getQuery(`en`))).toEqual(result1); + expect(await executeQuery(getQuery(`it`))).toEqual(result2); +}); + test('structured text', async () => { const query = /* GraphQL */ ` { From 2462344ae4d074aaa0c28cc922565ef1cf2afafa Mon Sep 17 00:00:00 2001 From: pieh Date: Mon, 9 Jan 2023 12:27:54 +0100 Subject: [PATCH 2/2] separate locale and fallbackLocale for each query --- src/hooks/createResolvers/index.js | 18 +++++-------- src/hooks/createSchemaCustomization/index.js | 18 ++++++++++--- .../sourceNodes/createTypes/item/index.js | 25 ++++++++++++++----- .../createTypes/site/DatoCmsSite.js | 17 ++++++++++--- 4 files changed, 52 insertions(+), 26 deletions(-) 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) || [], }, }; }