ashhitch / wp-graphql-yoast-seo

This is an extension to the WPGraphQL plugin for Yoast SEO
GNU General Public License v3.0
216 stars 49 forks source link

Internal server error when querying SEO fields for a user that has no posts #150

Closed marcusforsberg closed 1 year ago

marcusforsberg commented 1 year ago

Running a query such as:

query GET_USER {
  user(id: 2, idType: DATABASE_ID) {
    name
    seo {
      title
    }
  }
}

This triggers an internal server error. Debug mode shows this specific error message: Call to a member function get_head() on bool

The cause seems to be in get_post_type_graphql_fields():

'fullHead' => is_string(
    YoastSEO()
        ->meta->for_author($user->userId)
        ->get_head()
)
    ? YoastSEO()
        ->meta->for_author($user->userId)
        ->get_head()
    : YoastSEO()
        ->meta->for_author($user->userId)
        ->get_head()->html,

for_author() will return false if the user has no post since it is considered to not be indexable by Yoast.

Can we add a check so that get_head() is not run if for_author() returns false? 😊

pascalroget commented 1 year ago

Same issue here:

Query:

  user(idType: SLUG, id: $slug) {
    id
    slug
    name
    avatar {
      url
    }
    seo {
      canonical
      fullHead
      metaDesc
    }
  }
}

Errors:

    {
      "debugMessage": "Call to a member function get_head() on bool",
      "message": "Internal server error",
      "extensions": {
        "category": "internal"
      },
      "locations": [
        {
          "line": 9,
          "column": 5
        }
      ],
      "path": [
        "user",
        "seo"
      ],