`useAppMetafields` returns stringified booleans

Reposting this issue here as requested in [IMPORTANT] Stop creating issues here, use the Shopify Community forum instead · Issue #2521 · Shopify/ui-extensions · GitHub


Please list the package(s) involved in the issue, and include the version you are using

@shopify/ui-extensions-react@2024.1.1

Describe the bug

useAppMetafields returns the values "true" and "false" for boolean metafields instead of true and false. This is surprising because AppMetafield.value’s type definition includes boolean.

  // https://github.com/Shopify/ui-extensions/blob/c22cbdaf0b3bcc887aa554c30bfdbcdeb4d96750/packages/ui-extensions/src/surfaces/checkout/api/standard/standard.ts#L178-L196
 /** 
  * Represents a custom metadata attached to a resource. 
  */ 
 export interface AppMetafield { 
   /** The key name of a metafield. */ 
   key: string; 
  
   /** The namespace for a metafield. */ 
   namespace: string; 
  
   /** The value of a metafield. */ 
   value: string | number | boolean; 
  
   /** The metafield’s information type. */ 
   valueType: 'boolean' | 'float' | 'integer' | 'json_string' | 'string'; 
  
   /** The metafield's type name. */ 
   type: string; 
 } 

Steps to reproduce the behavior:

  1. The metafield can have a definition or be unstructured. I tested with a product variant metafield. It’s type must be boolean.
  2. Add a value of true or false to a variant’s metafield.
  3. console.log(useAppMetafields({ id: yourMetafieldId })) produces a value similar to the screenshot below.

Expected behavior

AppMetafield.value should be a boolean if the type of the metafield is boolean, not "true" or "false".

Screenshots

useappmetafield

Hi masonmcelvain,

Thanks for the detailed description of this issue - I’ve connected with the product team in this area and will report back when I learn more.

Hi again masonmcelvain,

Just wanted to update that our team are looking into this as a possible bug on our side or an issue with the documentation.

2 Likes

@Liam-Shopify Thanks for looking this issue, I am also facing the same issue. Could you please help us resolve it?
This the same issue - Help with useAppMetafields in Checkout UI Extension

Hi team, thanks for reporting this issue. It is expected that metafield values are always returned as strings, despite their defined types. We understand that this creates more work on the developer’s end, but for the time being we are keeping this as-is for consistency with other Shopify APIs.

The documentation types are incorrect and we are looking into updating this.

2 Likes

It is expected that metafield values are always returned as strings, despite their defined types.

Why?

This seems odd. The API returns json, and json is of course perfectly capable of returning an actual boolean?

I really don’t understand why always returning strings is the logical thing to do?