datocms / gatsby-source-datocms

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

Getting an item by other than default locale slug #231

Open GreenberryServiceAccount opened 1 month ago

GreenberryServiceAccount commented 1 month ago

I'm currently migrating from gatsby-source-datocms v4 to v5, but I'm having some trouble getting it to work. My default locale in DatoCMS is "en" and the other locale I'm using is "nl". In the previous version I could fetch an item based on the "nl" slug, but that doesn't seem to work in version 5 anymore. This should really be possible, because if, for example, I'm navigating to /nl/nieuws/mijn-eerste-nieuws I only know that my Dutch slug is mijn-eerste-nieuws. I don't have the "original" English slug at my disposal. Some items don't even have an English version.

I've verified that this issue exists by using the graphql explorer locally at http://localhost:8000/__graphql, by first getting an article in Dutch by using the English slug, eg:

query getArticle {
  data: datoCmsArticle(
    slug: { eq: "my-first-article" }
    locale: "nl"
  ) {
    title
    slug
    intro
    date
  }
}

This gives me the correct article in Dutch, so the "locale" argument is working.

I don't get any response when I try to use the Dutch slug however, eg:

query getArticle {
  data: datoCmsArticle(
    slug: { eq: "mijn-eerste-artikel" }
    locale: "nl"
  ) {
    title
    slug
    intro
    date
  }
}

Am I missing something? There must be a way to get a specific article based on the slug in any translation, right?

GreenberryServiceAccount commented 1 month ago

Update: I've now changed everywhere in my project to include the item's id in the object that's passed as context argument into the createPage function from Gastby. I've updated all queries to fetch the CMS item based on the id instead of the slug now and it works.

For me now this problem is solved, but it still seems like something you should be able to do: fetch a CMS item based on their slug in another language than English...