wp-graphql-content-blocks icon indicating copy to clipboard operation
wp-graphql-content-blocks copied to clipboard

Can't retrieve Core Navigation data

Open studiohic opened this issue 1 year ago • 1 comments

Thanks for your hardwork on this. I'm struggling to get data from CoreNavigation, CoreNavigationLink, CoreNavigationSubmenu. Please see code:

query NewQuery {
  pages {
    nodes {
      editorBlocks(flat: true) {
        __typename
        name
        ... on CoreNavigation {
          attributes {
            ref
          }
        }
        ... on CoreNavigationLink {
          attributes {
            label
            url
            title
          }
        }
        ... on CoreNavigationSubmenu {
          attributes {
            title
            url
            label
          }
        }
      }
    }
  }
}

This returns the following:

{
  "data": {
    "pages": {
      "nodes": [
        {
          "editorBlocks": []
        },
        {
          "editorBlocks": []
        },
        {
          "editorBlocks": [
            {
              "__typename": "CoreParagraph",
              "name": "core/paragraph"
            }
          ]
        },
        {
          "editorBlocks": []
        },
        {
          "editorBlocks": []
        },
        {
          "editorBlocks": []
        },
        {
          "editorBlocks": []
        },
        {
          "editorBlocks": [
            {
              "__typename": "CoreParagraph",
              "name": "core/paragraph"
            }
          ]
        },
        {
          "editorBlocks": [
            {
              "__typename": "CoreParagraph",
              "name": "core/paragraph"
            },
            {
              "__typename": "CoreParagraph",
              "name": "core/paragraph"
            },
            {
              "__typename": "CoreColumns",
              "name": "core/columns"
            },
            {
              "__typename": "CoreColumn",
              "name": "core/column"
            },
            {
              "__typename": "CoreParagraph",
              "name": "core/paragraph"
            },
            {
              "__typename": "CoreColumn",
              "name": "core/column"
            },
            {
              "__typename": "CoreParagraph",
              "name": "core/paragraph"
            },
            {
              "__typename": "CoreGroup",
              "name": "core/group"
            },
            {
              "__typename": "CoreImage",
              "name": "core/image"
            }
          ]
        }
      ]
    }
  },
  "extensions": {
    "debug": [
      {
        "type": "DEBUG_LOGS_INACTIVE",
        "message": "GraphQL Debug logging is not active. To see debug logs, GRAPHQL_DEBUG must be enabled."
      }
    ]
  }
}

studiohic avatar Feb 19 '24 15:02 studiohic

Hello @studiohic. Thank you for the report.

After looking into this, I can see that this block is not a standard block that has queryable attributes. Its part of the Full-stack editing blocks that are generated dynamically per page.

If you look at the contents of a saved CoreNavigation block you will see that it contains only a reference:

<!-- wp:navigation {"ref":45} /-->

This means that by default it cannot be parsed using the parse_blocks function. It won't do anything.

One could try to fix this by retrieving the navigation model from the ref id (using https://developer.wordpress.org/reference/functions/wp_get_nav_menu_items/) and with adding Query Attributes that extract the menu items from the HTML or return the menu node so that users can query it.

Here is an example of how we do this with the MediaDetails attribute of the Image block

https://github.com/wpengine/wp-graphql-content-blocks/blob/main/includes/Blocks/CoreImage.php#L44-L69

Here is the block code that does this work maybe you can make a sense out of it:

https://github.com/WordPress/WordPress/blob/a4aeb7160325117db7eed9fb1277df95c1681273/wp-includes/blocks/navigation.php#L206-L231

theodesp avatar Feb 20 '24 11:02 theodesp