1
votes

I am using drupal 8 to build a headless page with gatsby and graphql.

How to query a translation of a page?

query MyQuery {
  allNodePage {
    nodes {
      body {
        value
      }
    }
  }
}

I want to get this allNodePage.nodes.body.value in a specific language.

Edit: Here is the the graphiql allNodePage entity.

enter image description here

Edit_1:

According to this issue you can only query translations by modifying the api host-route (/fr/jsonapi/node/page) in drupal 8.

But in gatsby-source-drupal-plugin the api host route is not changeable in runtime.

Edit_2:

I try to translate a page node. Default language is english. I want to request the german translation which exists in drupal.

2
Does the lang code appear at any point in the page node? - ksav
I updated that question with a screenshot of the allPageNode entity. There is a langcode but its not possible to set the langcode. - tomole
I'm working on something similar, I will try get the node translation in the graphql query. How are you translating your node? Is it through the UI primary tabs menu? If you already found a solution please post it. Thanks - Rick
Hi @Rick. I switched to strapi to maintain data. As mentioned above you only can translate content with /fr/jsonapi/node/page oder /en/jsonapi/node/page in drupal. Another possibility is to add values for each language in your model. For example title__en, title__fr or content__en, content__fr. - tomole
Hi @Tomole. There is another way (incase you or anyone else is interested), I will post as an answer - Rick

2 Answers

3
votes

After some experimentation I see it's not working yet with Drupal JSON API, but I got it working with the Drupal GraphQL module. This can be used with Gatsby and gatsby-source-graphql instead of gatsby-source-drupal. The query would look something like this:

{
  nodeQuery(
    filter: {
      conditions:[
        {operator: IN, field: "type", value: ["my_node_type"]},
        {operator: EQUAL, field: "langcode", value: "de"},
      ]
    }
  ) {
    entities {
      ...on NodeMyNodeType {
        entityTranslation(language: DE) {
          entityLabel
        }
      }
    }
  }
}
-1
votes

If you want to filter allNodePage by langcode you can do it like this:

    query MyQuery {
      allNodePage(filter: {langcode: {eq: "fr_FR"}}) {
        nodes {
          body {
            value
          }
        }
      }
    }

Of course, you can use a variable in place of "fr_FR" instead of hardcoding it

query MyQuery($lang: String!) {
      allNodePage(filter: {link: {eq: $lang:}}) {
        nodes {
          body {
            value
          }
        }
      }
    }