Backup language

Is there a way to retrieve content for a backup / fallback language with GraphQL?

For example, my default language Prismic language is "en-us", but we will have the data in many languages.

If a language exists in our Prismic CMS, 'de-de' for example, but a certain page has only been created in English, when a user requests this page in 'de-de' via GraphQL a null object is returned. e.g.

Example Query

query () {
  locale: article_category(uid: "about", lang: 'de-de') {
     etc ...
  }
}

GraphQL Results

{
  "data": {
    "locale": null,
   }
 }

How can we make it return the default language content (en-us) if no data exists for the language we are asking for?

Hi @tim, thanks for reaching out about this. Unfortunately there isn’t a way to have the GraphQL API (or the REST API for anyone else curious) return a backup document if the language you’re looking for doesn’t exist. This will have to be done in your website application. That is, if you don’t find a document in the language you’re looking for (it return null), then run a second query for the document in the default language (“en-us” in your case).

Let me know if you have any questions about this. Thanks!

Thanks @Levi,

No worries, that makes sense, and I’m already building in this logic into the app.
e.g. if it returns null then I make a second request with the default language.

Thanks!

Hi @tim, just an alternative practice. Instead of to call the default language (your backup, fallback language) only if the language you’re looking for doesn’t exist, you can check this scenario every time for your content. If use nodejs/javascript and REST API, for example, you can use a Promise.all to get the content in current language and in the fallback language. If the first response does not contains results, return the second response:

let predicates = [  Prismic.Predicates.at('document.type', '<something>')  ]
let options = { orderings: `[document.last_publication_date desc]`, lang: 'en-us'}

// Check for de-de
const query1 = api.query(predicates, { ...options, ...{ lang: 'de-de' } })

// Check for en-us (backup lang)  
const query2 = api.query(predicates, options)

const [response1, response2] = await Promise.all([query1, query2])
return (response1.results.length > 0) ? response1 : response2

Thanks @alessandro, thats a neat way of doing it :stuck_out_tongue_winking_eye:

I am currently doing something similar, but a little bit longer as I’m using Apollo. I like the idea of utilising promises in this way though, it seems much cleaner.

I’ll see what I can do with Apollo and post my solution back here when I’m successful!

1 Like

This issue has been closed due to inactivity.