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

Set to null instead of false #154

Closed izzygld closed 1 year ago

izzygld commented 1 year ago

Cannot return null for non-nullable field "MediaItem.id"

Betting it has to do with this, where wpcom_vip_attachment_url_to_postid is returning false causing a 0 to be passed to the loader instead of bailing with null. $id = wpcom_vip_attachment_url_to_postid($twitter_image);

This fixes - https://github.com/ashhitch/wp-graphql-yoast-seo/issues/132


My Solution:

It's possible that the error message "Cannot return null for non-nullable field 'MediaItem.id'" is related to the fact that the wpcom_vip_attachment_url_to_postid()function is returning false when it fails to find a post ID for the given URL. This can cause the $id variable to be assigned a value of false instead of a valid post ID.

When the $idvariable is later used in a GraphQL query/mutation as the ID of a MediaItem object, it's expected to be a non-null value. Since false is not a valid ID, the GraphQL server is throwing an error saying that it cannot return null for a non-nullable field.

To fix this issue, you could modify the wpcom_vip_attachment_url_to_postid() function to return null instead of false when it fails to find a post ID. Then, when$id is used in a GraphQL query/mutation, it will be null instead of false, which is an acceptable value for a nullable field.

By setting $id to null instead of false when a post ID cannot be found, you'll avoid the error message about returning null for a non-nullable field.

izzygld commented 1 year ago

@ashhitch any updates?

ashhitch commented 1 year ago

@izzygld Sorry for the delay, seems logical to me, I will get this merged in and released