JEverhart383 / astro-wordpress-starter

151 stars 31 forks source link

Cannot destructure property 'menus' of '(intermediate value)' as it is undefined. #17

Closed sangrepura closed 8 months ago

sangrepura commented 8 months ago

I'm opening a new issue because I'm encountering the same issue as https://github.com/JEverhart383/astro-wordpress-starter/issues/11

I tried with the "Twenty Twenty-Three "and "Twenty Twenty-Four" WP themes with and without Faust installed

I also ensured that the menu items were added to PRIMARY and the GraphQL query words fine in the IDE

Using the default query in this code base:

query: `{
                menus(where: {location: PRIMARY}) {
                  nodes {
                    name
                    menuItems {
                        nodes {
                            uri
                            url
                            order
                            label
                        }
                    }
                  }
                }
                generalSettings {
                    title
                    url
                    description
                }
            }
            `

Any help would be appreciated!

sangrepura commented 8 months ago

Note: Here;s teh query in the IDE:

{
  menus(where: {location: PRIMARY}) {
    nodes {
      name
      menuItems {
        nodes {
          uri
          url
          order
          label
        }
      }
    }
  }
  generalSettings {
    title
    url
    description
  }
}

And the result:

{
  "data": {
    "menus": {
      "nodes": [
        {
          "name": "Default",
          "menuItems": {
            "nodes": [
              {
                "uri": "/my-account/",
                "url": "REDACTED/my-account/",
                "order": 1,
                "label": "My account"
              },
              ....REDACTED
           }
      }
    }
}
sangrepura commented 8 months ago

OK, I found the issue! The field "RootQuery.menus" cannot be accessed without authentication see: https://www.wpgraphql.com/docs/menus#access-control

Just add an Application password and set it in your headers:

        headers: {
            'Content-Type':'application/json',
            Authorization: `Basic ${hashedCredentials()}`
        },