-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Localization should enable passing a default/fallback language #164
Comments
@irenama Thanks for trying out the library. Unfortunately the behaviour you're seeing is indeed expected. The nature of Gatsby's GraphQL layer makes it difficult for the library to implement the fallback pattern you see with your GraphCMS API. However there are ways to achieve what you're looking to do using Gatsby APIs. You can add a custom resolver for the exports.createResolvers = ({ createResolvers }) => {
const resolvers = {
GraphCMS_CrossLink: {
images: {
args: {
locale: {
type: 'GraphCMS_Locale!',
defaultValue: 'en',
},
},
resolve: async ({ remoteTypeName, remoteId }, args, context, _) => {
const toGatsbyId = (remoteId) => Object.values(remoteId).join(`:`)
// Find the `en` node for this `CrossLink` entry
const enCrossLinkNode = context.nodeModel.getNodeById({
id: toGatsbyId({ remoteTypeName, remoteId, locale: args.locale }),
})
// Map over the related images on the `en` node to build an array of ids
const imageGatsbyIds = enCrossLinkNode.images.map(
(remoteImageIdFields) => toGatsbyId(remoteImageIdFields)
)
// Lookup localised nodes for `images`
return context.nodeModel.getNodesByIds({ ids: imageGatsbyIds })
},
},
},
}
createResolvers(resolvers)
} This can be then used in queries: {
allGraphCmsPage(filter: { locale: { eq: de } }) {
nodes {
id
locale
sections {
... on GraphCMS_ServicesSection {
id
crossLink {
images(locale: en) {
id
}
}
}
}
}
}
} This example works where Hope this helps! |
Thanks for the detailed answer! Unfortunately, introducing custom resolvers would be an overhead for this project having a lot of nested 'sections' with images and the like. |
There are both trade offs and benefits to using this library over There might be some future API/product improvements which making working with localised assets better. Right now it's not a great experience to have to re-upload the actual asset for any localised versions. I'll relay your feedback. Will close this issue, but feel free to re-open if you need 👍 |
Hello @irenama , i came across a similar challenge and i was wondering how did grasby-source-graphql resolve the null assets issue? |
Hi @TarekMowafy ! The project, that I tested page(locales: [$locale, en]) {
...PageFields
} So that's convenient/straightforward: No missing content (though maybe mixed language). :) |
I ran into this problem when querying images for my localized project, though I think it is a general issue with how
gatsby-source-graphcms
queries localized content.On GraphCMS images are localized per default, although it doesn't often make sense to actually upload two images (en/de). This brings me to my current issue:
As English is the default language I would like to fall back to the English content, if there is no translated content for
de
. As far as I can see, it's not possible to pass an array oflocales: [de, en]
like in the general GraphCMS documentation (and like I did before) but only filter by one distinct language. Through this, I am missing a lot of e.g images on the queries for the German translation.Am I missing something here or is this not possible to fallback/query the default language with the package yet? If so is there a way around it? Thanks in advance for clarifying! 🙂
The text was updated successfully, but these errors were encountered: