Get data of node's translation using route query field

Created on 10 October 2023, about 1 year ago
Updated 25 January 2024, 11 months ago

Problem/Motivation

On a multilingual site with English(Default), French languages and language detection using path prefix
It is possible to get node data in default language using below query

query getData {
  route(path: '/node-alias') {
    ... on EntityCanonicalUrl {
      entity {
        id
        langcode
      }
    }
  }
}

but same does not work for other language, that is querying with path '/fr/node-alias' and with available types - DefaultEntityUrl, EntityCanonicalUrl or the interface EntityUrl.

What is the correct way to query for a translation using route query field?
If this is not supported by the routing Extension currently, is there any alternative approach to query a translation by alias directly.

🐛 Bug report
Status

Closed: won't fix

Version

1.0

Component

Code

Created by

🇮🇳India sumit_saini

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @sumit_saini
  • 🇮🇳India sumit_saini

    Further debugging took me to src/Plugin/GraphQL/DataProducer/Route::resolve() where language option is not added to $url object just after determining the correct language. The language is being set in access check condition (after comparing the passed path $value with the generated path $target_url which is causing this issue) which seemed out of place. Moving it before generating $target_url seems to work correctly.

    --- a/src/Plugin/GraphQL/DataProducer/Route.php
    +++ b/src/Plugin/GraphQL/DataProducer/Route.php
    @@ -247,7 +247,9 @@ class Route extends DataProducerPluginBase implements ContainerFactoryPluginInte
               }
             }
    
    -        // Check URL access.
    +        $negotiatedLanguage = $this->languageManager->getLanguage($negotiatedLangcode);
    +        $url->setOption('language', $negotiatedLanguage);
    +        $field->setContextValue('language', $negotiatedLangcode);
             $target_url = $url->toString(TRUE)->getGeneratedUrl();
    
             // if language detection is domain based, remove domain from $target_url
    @@ -272,10 +274,8 @@ class Route extends DataProducerPluginBase implements ContainerFactoryPluginInte
             $access = $url->access(NULL, TRUE);
             $field->addCacheableDependency($access);
    
    +        // Check URL access.
             if ($access->isAllowed()) {
    -          $negotiatedLanguage = $this->languageManager->getLanguage($negotiatedLangcode);
    -          $url->setOption('language', $negotiatedLanguage);
    -          $field->setContextValue('language', $negotiatedLangcode);
               return $url;
             }
             else {
    

    After above fix, EntityUrl and EntityCanonicalUrl are returning correct language data.

  • Status changed to Needs review about 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    run-tests.sh fatal error
  • 🇮🇳India sumit_saini

    Updated issue meta as we have a patch.

  • 🇨🇭Switzerland ayalon

    This is not the way to go. To get the translated contest, you have to provide the translation language when doing the GraphQL request.
    This ensures, that also field label translations and all other dependencies are working correct.

    In short:

    Do the above query with the alias '/fr/node-alias' against your API with http:/my-backend/fr/graphql as where 'graphql' is the endpoint you defined in your graphql server configuration.

    We will not incorporate the proposed patch. Please adapt your frontend application to provide the correct language when querying the API. Thanks for understanding.

  • Status changed to Closed: won't fix 11 months ago
Production build 0.71.5 2024