Confusion about missing query fields

Created on 20 February 2023, over 1 year ago
Updated 25 January 2024, 8 months ago

GraphQL 3 delivered Query fields like 'menuByName' and 'NodeById' that facilitated queries like:

query ($node: String!) {
  node: nodeById(id: $node) {
    type:__typename
    entityId
    langcode {
      value
    }
    ...News
    ...Announcement
  }
  mainMenu: menuByName(name: "main") {
    ... Menu
  }
  selectedPath: currentUrl {
    path
  }
}

I cannot find those anymore in the explorer. Did I make a mistake in the configuration? Or are they just not part of the solution by this module?

💬 Support request
Status

Closed: won't fix

Version

1.0

Component

Miscellaneous

Created by

🇳🇱Netherlands Ronald van Belzen

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

Comments & Activities

  • Issue created by @Ronald van Belzen
  • 🇳🇱Netherlands Ronald van Belzen

    I am able to use:

    query ($id: ID!, $langcode: Langcode) {
     node: entityById(entityType: NODE, id: $id, langcode: $langcode) {
        type:__typename
        entityTypeId
        langcode
    		...FlexPage
      }
    
      mainMenu: entityById(entityType: MENU, id: "main", langcode: EN) {
        ... Menu
      }
    }
    

    The NODE query works fine, but I run into a problem with the MENU query. In contrast to the NODE query, the langcode parameter is being ignored. The default language of the site is NL and I only get a query response for that language; no matter the langcode I use.

    Seems to be a bug.

  • First commit to issue fork.
  • 🇨🇭Switzerland dulnan

    You're correct, there are only two generic fields for entity queries. The reason is that this module supports all entity types out of the box and deriving two fields for each entity type adds a lot of duplication to the schema.

    However, currently all enabled entity types are also automatically available to be queried directly. This is not ideal and I'm planning to add entity type specific query fields again, but so that one can select which ones should be generated.

    Regarding the entityById for menus: The menu entity is not an instance of TranslatableInterface, which is why the langcode argument is ignored because no translation can be loaded. This also means all subsequent fields will resolve based on the default langcode of the menu entity. However, if you perform the GraphQL request already in the desired language (e.g. by using /en/graphql as the URL) you will get both the menu and the links in EN.

    Alternatively, if you want to fetch both NL and EN in the same query, you can instead get the translation of the MenuLinkContent entity:

    query {
      entityById(entityType: MENU, id: "main") {
        ... on Menu {
          links {
            link {
              content {
                ... on MenuLinkContentMain {
                  en: translation(langcode: EN) {
                    label
                  }
                  nl: translation(langcode: NL) {
                    label
                  }
                }
              }
            }
          }
        }
      }
    }
    

    Please note that there is a bug (actually it's by design) in the drupal/graphql module when requesting a translation that does not exist: https://github.com/drupal-graphql/graphql/pull/1176
    I plan to add a workaround by returning NULL in this case, in order to not produce internal server errors for such trivial actions.

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