datocms / gatsby-source-datocms

Official GatsbyJS source plugin to pull content from DatoCMS
MIT License
140 stars 50 forks source link

Locale at query level returns wrong fields #209

Closed jgdemattos closed 1 year ago

jgdemattos commented 1 year ago

Issue when querying a document with the locale argument at document level, like the the query below:

query BlogPostByOriginalId($language: String!, $originalId: String!) {
  datoCmsBlogpost(originalId: {eq: $originalId}, locale: $language) {
    originalId
    title
  }
}

This query will, sometimes, ramdomly return fields in the wrong language when building the site. This happens mostly in production.

As an example, if the $language is PT, portuguese, I will get fields in EN, english.

The safe way to ensure the correct field language, is to use the locale argument at field level, like below:

query BlogPostByOriginalId($language: String!, $originalId: String!) {
  datoCmsBlogpost(originalId: {eq: $originalId}, locale: $language) {
    originalId
    title(locale: $postLang)
  }
}
vanska commented 1 year ago

I'm facing the same issue.

If a locale is not defined on a field level the secondary locale is returned even if a fallback locale is set to the primary locale.

Doesn't affect development.

Faxxiz commented 1 year ago

Facing the same issue, not on a development but on every build.

export const pageQuery = graphql`
  query HomePage($locale: String!) {
    datoCmsGlobalConfiguration(locale: $locale) {
      id
      websiteName
      ...CookiesBannerFields
    }
  }
`

here is my gatsby-node just to show that I'm creating the same page on different languages :

async function generateHomePage() {
  const index = path.resolve(`./src/templates/index.tsx`)

  allLocales.data.datoCmsSite.locales.forEach(locale => {
    createPage({
      path: locale === defaultLocale ? "/" : `/${locale}`,
      component: index,
      context: {
        locale,
        defaultLocale,
        slugs: allMainSlugs,
      },
    })
  })
}

and as you can see in my context the locale is "fr" and the data returned is in english

Capture d’écran du 2022-12-29 10-04-14

pieh commented 1 year ago

Hey folks, I opened PR with potential fix for those issues in https://github.com/datocms/gatsby-source-datocms/pull/211