System Information
So I just updated to the latest Strapi version, 3.6.0, and followed the guide to create localization for my content. I also use a slug system from the official documentation. I created a slug based on a title field. So, for example, I have a collection type ‘Category’, with title ‘Animals’, I got a slug ‘animas’. And I used this GraphQL query to fetch a category by slug:
query Categories($slug:String!) {
categories(where: {slug: $slug}) {
_id
title
description
}
}
Now I added Italian locale, and for Italian, I changed the title to ‘Animali’, which also created a slug ‘animali’ for that Category.
However, when I run the query above with this slug, I’m getting no results. If I manually change it to ‘animals’ - I do receive the Category.
Is it a bug or am I doing something wrong?
OK, so apparently I have to pass also a locale to the query in order for that to work:
query Categories($slug:String!, $locale:String! = "all") {
categories(where: {slug: $slug}, locale: $locale) {
...
}
}
But now I ran into another 2 problems:
-
in my Category I have a relation field, Bars, with the following rule: “Categories has and belongs to many Bars”. Obviously, all Bars belonged to a Category with the default EN locale. Now that I’ve added locales to Ctegories, the relations weren’t copied also to these locales. So is there a way to a) copy all the relations to the new locales and b) make sure that once I add a new Bar to the relation, it will be added to all the locales?
-
I tried to add ‘localizations’ to the query
query Categories($slug:String!, $locale:String! = “all”) {
categories(where: {slug: $slug}, locale: $locale) {
…
localizations: {
id
title
}
}
}
And I received the following error:
“Cast to ObjectId failed for value “[object Object]” at path “_id” for model “category””
In fact, I received this error on every query where I tried it. Why is this error happening?
If you replace locale: $locale
with _locale: "all"
then you’ll get your main page, plus an array of the localized pages in localizations
. Not sure if that’s what you were looking for, but thought it might be helpful. The documentation doesn’t make this clear at all.
And I just found out that a query like this will return just the page for the language you request without having to deal with the localizations array when your slug matches across languages.
query{
pages (
where: {
path: "/jars",
locale: "es-MX",
_locale: "all" },
limit: 1 ) {
id,
title,
path,
locale,
content
}
}