wp-graphql / wp-graphql

:rocket: GraphQL API for WordPress
https://www.wpgraphql.com
GNU General Public License v3.0
3.66k stars 443 forks source link

Wrong data being returned from `EnqueuedAsset.extra` resolve function #2938

Closed lubiah closed 11 months ago

lubiah commented 1 year ago

Description

If you are trying to query about the extra information of an enqueued asset whether it being a style or a script, you get the wrong results.

Here are the reasons for the error.

Here's proof.

image Over here, you can see it's returning null for the data.

image By using GraphQL debug, we can verify that many properties exist on the extra field but it's being forced to return a string

Steps to reproduce

1) Use this query on any random posts

{
  post(idType:DATABASE_ID, id:43){
    slug,
    enqueuedStylesheets {
      edges{
        node{
          dependencies {
            id
          },
            extra  
        }
      }
    }
  }
}

Additional context

No response

WPGraphQL Version

Version 1.16.0

WordPress Version

6.3.1

PHP Version

PHP version 8.2.4

Additional enviornment details

No response

Please confirm that you have searched existing issues in the repo.

Please confirm that you have disabled ALL plugins except for WPGraphQL.

justlevine commented 1 year ago

Source: https://github.com/wp-graphql/wp-graphql/blob/2937585d4f73756535302b0ebe78592e51e23c94/src/Type/InterfaceType/EnqueuedAsset.php#L72

The problem is that $extra is array<string, mixed>, not sure if we can just encode the value as type String 🤔. What sort of data usually gets stored here?

lubiah commented 1 year ago

Source:

https://github.com/wp-graphql/wp-graphql/blob/2937585d4f73756535302b0ebe78592e51e23c94/src/Type/InterfaceType/EnqueuedAsset.php#L72

The problem is that $extra is array<string, mixed>, not sure if we can just encode the value as type String 🤔. What sort of data usually gets stored here?

It stores data about about the styles or scripts.

For scripts,

For styles

lubiah commented 1 year ago

@justlevine , I suggest we create two new ObjectTypes for the extra found in an enqueuedScript and for that found in an enqueuedStyle

justlevine commented 1 year ago

Source: https://github.com/wp-graphql/wp-graphql/blob/2937585d4f73756535302b0ebe78592e51e23c94/src/Type/InterfaceType/EnqueuedAsset.php#L72

The problem is that $extra is array<string, mixed>, not sure if we can just encode the value as type String 🤔. What sort of data usually gets stored here?

It stores data about about the styles or scripts.

For scripts,

  • data
  • group
  • strategy

For styles

  • rlt
  • suffix
  • path
  • conditional

@lubiah any chance you could point me to some documentation or a source of truth in the codebase for any of these? I need to understand the possible types and shapes in order to get this into core. (I tried searching myself, and the best I could come up with is a script _WP_Dependency also using the $extra['conditional'] var over here. )

lubiah commented 1 year ago

@justlevine , there is no documentation though I can provide you with information where you can find them. If you have the Wordpress codebase downloaded on your computer, a simple search for wp_style_add_data or wp_script_add_data or add_data in your code editor can help you to find where it's being used.

The WP_Scripts class and WP_Styles class both inherit the WP_Dependencies class which has a method called add_data. The method is responsible for adding data to the extra so wherever it's being called can give you a hint of the possible types. Would you need any more info??