Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CoreImageAttributes.width is type String, but should be a Float #136

Open
justlevine opened this issue Jul 24, 2023 · 11 comments
Open

CoreImageAttributes.width is type String, but should be a Float #136

justlevine opened this issue Jul 24, 2023 · 11 comments
Labels
needs: discussion Requires a discussion to proceed needs: reproduction This issue needs to be reproduced independently type: bug Issue that causes incorrect or unexpected behavior

Comments

@justlevine
Copy link
Contributor

Noticing that the width field on CoreImageAttributes is type String when it should be of type Float:

image

I'm not 💯sure how this plugin is automating the {BlockType}Attributes field registration, otherwise I'd open a PR myself.

@theodesp
Copy link
Member

theodesp commented Jul 24, 2023

Actually this causes an issue if you turn this into a Float when you combine this with CoreColumns width attribute which is of type =String.

252650454-4bfd991b-edd6-4650-b0c7-59221e37ece5

Take a look at this testcase:

https://github.com/wpengine/wp-graphql-content-blocks/blob/main/tests/unit/blocks/CoreImageTest.php#L38

I think my fix was premature. Any ideas or thoughts on how to support this better is welcome.

@justlevine
Copy link
Contributor Author

I think my fix was premature. Any ideas or thoughts on how to support this better is welcome.

From a search of WordPress's codebase, it seems, the Column and Spacer blocks are the only one that use a string width, while the rest use a number. As such it should be treated as outlier behavior.

As mentioned, I'm unclear where this is being handled in the code and I don't see a particular PR that made this fix, but my suggestion would be to parse the width into a width: Float, widthUnit: CSSUnitEnum. This is the pattern used by the Search block, and will likely be adopted by the Column and Spacer blocks in a future release, so not just would this be a semantically ideal pattern, it's forward compatible.

@theodesp
Copy link
Member

Hey @justlevine. After a conversation with @jasonbahl, we found that this is indeed inconsistent. Could you open a PR for this? There is a test case that needs to addressed here when you revert this back to Float type. So in that case the test case is expected to fail with the error mentioned above.

If you haven't got availability, I will create a ticket and follow up. Thanks.

@justlevine
Copy link
Contributor Author

I have some availability over the weekend but I'm not entirely sure I understand this plugin's lifecycle. If you link me to the parts that are currently responsible for fetching/parsing the block attributes, I'd be happy to take a stab at it 😁

@theodesp
Copy link
Member

theodesp commented Aug 10, 2023

Sure. It's this line need to be removed:

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

It will default back to Float

@justlevine
Copy link
Contributor Author

justlevine commented Aug 10, 2023

It's the attribute fetching/parsing_ that I'm looking for. I need that in order to take the original string $attributes['width'] and split it into (float) width and (enum) cssUnit. It's either in the abstract block class or the BlocksResolver, I think, but that's where I start to get a bit lost.

@theodesp
Copy link
Member

You can still specify both explicitly:

'width'        => array( 'type' => 'float' ),
'widthUnit' =>  array( 'type' => 'string' ),

The type parsing is located here:
https://github.com/wpengine/wp-graphql-content-blocks/blob/main/includes/Blocks/Block.php#L152-L168

However we don't handle enum types yet.

https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/#enum-validation
https://www.apollographql.com/tutorials/side-quest-intermediate-schema-design/02-the-enum-type

Implementing this would be outside the scope of this ticket.

@LarsEjaas
Copy link

This is a major pain when working with something like GraphQL Codegen for TypeScript types.

I see this problem in several places throughout the attributes for the Core blocks:

CoreAudioAttributes on CoreAudio:

  • id

CoreColumnAttributes on CoreColumn

  • width

CoreCoverAttributes on CoreCover

  • id

CoreFileAttributes on CoreFile

  • id

CoreImageAttributes on CoreImage

  • id
  • width

CoreNavigationLinkAttributes on CoreNavigationLink

  • id

CoreNavigationSubmenuAttributes on CoreNavigationSubmenu

  • id

CorePageListItemAttributes on CorePageListItem

  • id

CorePostFeaturedImageAttributes on CorePostFeaturedImage

  • width
  • height

CoreSiteLogoAttributes on CoreSiteLogo

  • width

CoreSocialLinksAttributes onCoreSocialLinks

  • size

CoreSpacerAttributes on CoreSpacer

  • height
  • width

CoreTextColumnsAttributes on CoreTextColumns

  • width
  • content

CoreVideoAttributes on CoreVideo

  • id

@justlevine
Copy link
Contributor Author

I see this problem in several places throughout the attributes for the Core blocks:

@LarsEjaas the root cause /solution for all these is likely similar. Apart from witdh, what are the types currently being returned and what should they be?

@LarsEjaas
Copy link

I see this problem in several places throughout the attributes for the Core blocks:

@LarsEjaas the root cause /solution for all these is likely similar. Apart from witdh, what are the types currently being returned and what should they be?

I am not sure what they should be actually. But for me, the main issue is that the types are different depending on where I make a query for the blocks.
I can make a query for the EditorBlock on Post or Page. So my issue here is not so much WHAT the types should be - I just want them to be the same so I can reuse GraphQL fragments for the different Core blocks everywhere.

Example:
Right now for CoreSpacer I would need to have a different CoreSpacerAttributes fragment for pages and posts - that is a pain - I just want the types to be identical. In the headless frontend I would also have to account for the difference in types in the components.

@mindctrl mindctrl added type: bug Issue that causes incorrect or unexpected behavior has_jira labels Aug 22, 2023
@theodesp
Copy link
Member

theodesp commented Sep 12, 2023

I noticed that this would happen on each fragment that contains the same attribute name but different type. For example:

fragment CorePostFeaturedImageFragment on CorePostFeaturedImage {
  attributes {
    width
  }
}
fragment CoreSiteLogoFragment on CoreSiteLogo {
  attributes {
    width
  }
}
{
  posts {
    nodes {
      editorBlocks {
        name
      	__typename
        ...CorePostFeaturedImageFragment
        ...CoreSiteLogoFragment
      }
    }
  }
}
 "message": "Fields \"attributes\" conflict because subfields \"width\" conflict because they return conflicting types String and Float. Use different aliases on the fields to fetch both if this was intentional.", "message": "Fields \"attributes\" conflict because subfields \"width\" conflict because they return conflicting types String and Float. Use different aliases on the fields to fetch both if this was intentional.",

One potential solution is to use unique aliases for each attributes field. This would avoid the conflicts.

fragment CorePostFeaturedImageFragment on CorePostFeaturedImage {
  corePostAttributes: attributes {
    width
  }
}
fragment CoreSiteLogoFragment on CoreSiteLogo {
  coreSiteLogoAttributes: attributes {
    width
  }
}
{
  posts {
    nodes {
      editorBlocks {
        name
      	__typename
        ...CorePostFeaturedImageFragment
        ...CoreSiteLogoFragment
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: discussion Requires a discussion to proceed needs: reproduction This issue needs to be reproduced independently type: bug Issue that causes incorrect or unexpected behavior
Projects
None yet
Development

No branches or pull requests

5 participants