Shopify / js-buy-sdk

The JS Buy SDK is a lightweight library that allows you to build ecommerce into any website. It is based on Shopify's API and provides the ability to retrieve products and collections from your shop, add products to a cart, and checkout.
https://shopify.github.io/js-buy-sdk
MIT License
987 stars 262 forks source link

Missing MetafieldConnection in Product schema #910

Closed czarekowyxjs closed 2 years ago

czarekowyxjs commented 2 years ago

Please complete the checklist before filing an issue:

None of the above? Create an issue. Be sure to include all the necessary information for us to understand and reproduce the problem:

Bug details

Describe the bug Product schema has fields metafield and metafields. In version 2.15.1, metafield was type Metafield and metafields was type MetafieldConnection. Now both fields have same type: metafield. It makes impossible to fetch list of metafields for product.

To Reproduce Try to add metafields connection to your request.

Example of request which works for version 2.15.1, but doesn't for 2.16.1:

export const collectionsWithProductsQuery = client.graphQLClient.query((root: any) => {
  root.addConnection('collections', { args: { first: 100 } }, (collection: any) => {
    collection.add('title')

    collection.addConnection('products', { args: { first: 100 } }, (product: any) => {
      product.add('title')

      product.addConnection('metafields', { args: { first: 100 } }, (metafield: any) => {
        metafield.add('namespace')
        metafield.add('key')
        metafield.add('value')

        metafield.add('reference', (reference: any) => {
          reference.addInlineFragmentOn('MediaImage', (mediaImage: any) => {
            mediaImage.add('id')
            mediaImage.add('image', (image: any) => {
              image.add('id')
              image.add('altText')
              image.add('height')
              image.add('width')
              image.add('url')
              image.add('transformedSrc')
            })
          })
        })
      })
    })
  })
})

Expected behavior It should return list of metafields. Exactly as it was in version 2.15.1

Environment (please complete the following information):

Additional context Add any other context about the problem here.

Bug Report Checklist

riccardolardi commented 1 year ago

@czarekowyxjs any update on how to query metafields using js-buy-sdk? Why was this issue closed?

czarekowyxjs commented 1 year ago

@riccardolardi I closed this issue, because the reason of issue was new version of shopify api where shopify changed the way of querying metafields. I realized it after creating this issue. Now if you want to query any metafield, you have to define it in query args. It works for me:

const metafields = [{
  key: "metafield_key",
  namespace: 'metafield_namespace',
}, {
  key: "metafield_key_2",
  namespace: 'metafield_namespace',
}]

export const collectionsWithProductsQuery = client.graphQLClient.query((root: any) => {
  root.addConnection('collections', { args: { first: 100 } }, (collection: any) => {
    collection.add('title')

    collection.addConnection('products', { args: { first: 100 } }, (product: any) => {
      product.add('title')

      product.add(
        'metafields',
        { args: { identifiers: metafields } },
        (metafield: any) => {
          metafield.add('namespace')
          metafield.add('key')
          metafield.add('value')

          metafield.add('reference', (reference: any) => {
            reference.addInlineFragmentOn('MediaImage', (mediaImage: any) => {
              mediaImage.add('id')
              mediaImage.add('image', (image: any) => {
                image.add('id')
                image.add('altText')
                image.add('height')
                image.add('width')
                image.add('url')
                image.add('transformedSrc')
              })
            })
          })
        }
      )
    })
  })
})
riccardolardi commented 1 year ago

@czarekowyxjs thanks a lot, should this work in latest SFAPI 2022-10 ?