GraphQL: only return data for one language

I’m pretty new to GraphQL as well as squidex, so please bear with me if this is obvious:

I have two language defined in squidex. And I have defined a field as localizable and provided the two languages to my content instances.

With GraphQL: how can I retrieve the content for a given language without writing different GraphQL queries for all my languages?

E.g. for a query like the following:

query Posts {
  queryPostContents(filter: "data/tags/iv eq 'forDev'") {
    data {
      text {
        en
        de
      }
    }
  }
}

If I had to rewrite this query for every language I would end up with something like:

...
text {
  en
}
...

and

...
text {
  de
}
...

Which makes the code unmaintainable and also poses problems when parsing the data (I am using apollo-android) as I would have to use reflection to get the right variable name from the generated type. I recognize that flatData would solve the latter problem, but then I could not define the language in the query anymore (I like would have to put it somewhere in the filter?).

I’m pretty sure I’m just overlooking some simple solution here as this seems to be a pretty common use-case, but I neither could find it here in the forum or on the internets.

It is not possible right now. The only option is to use headers for that:

Thanks for the quick reply! :pray:

I saw the localization page, but thought there was maybe some GraphQL language feature I’m overlooking. Is this a GraphQL or a squidex shortcoming? And if it’s the latter: are there already plans to make it work in the future?

What exactly do you want to make work? To define the language in the query?

Yes, without resorting to the REST API if possible. Sorry for not being clear.

You can use the same headers for graphql as well

Oooooh, really? That’s perfect!

Because in the documentation you’ve linked it says:

NOTE: The headers above are not supported by the graphql endpoint, because in graphql the output should be defined the query only

Oh, sorry. But this was probably before flatData has been introduced.

1 Like

No worries, I should have just tried it out! :joy: Thanks a lot!

Just to ensure I’m getting this correctly: I can use flatData within the GraphQL query and don’t need to add the X-flatten header, right? And for the language I’ll set the the header.

Yes, this should work.

1 Like

That worked, many thanks! :pray:

1 Like