Updating Shop Metafields using GraphQL

This is a fairly short one. I have been building an app that needs to pass data from my server to the Shopify front end in order for liquid to be able to use the data to render the shop templates.

This data does not change rapidly, it’s a setting that affects the visual layout for a snippet. There are two options I see available to me.

App proxy: On the face of it, this seems like the best choice. App proxies enable data to be fetched from an app server and used on the front end, Liquid support is included. However, this means that every time this app data needs to be used in a rendering, a call needs to be made back to the app server. Even with a caching scheme in place, this seems like an expensive way to do things. I also believe in minimising the JavaScript that is necessary to get a Shopify app rendering.

Metafields: Metafields can be used to store app data against the store as a namespace,key,value triplet. They come in two flavours, private and public. Public metafields can be accessed by any app and by liquid in the templates. Private metafields are only available to the app itself and are not available to liquid.

Public metafields probably fit the task best, I can update them though the app whenever the customer changes them in the admin system, they are available directly to Liquid in the templates through the metafields object.

Metafields can be stored against a number of different resources such as product, order and shop. In this case, storing against the shop makes most sense as this data is unique per shop, not customer, collection of product.

Here’s where I encountered a problem …. Using GraphQL, I can set public metafields for any resource except a shop. There are mutations for all private metafields (which are not any use to me) and metafields can be updated on all other resources as part of their mutation functions but there is no mutation for the shop resource.

They seem to be east to read by GraphQL but not write:

{
  shop
  {
    metafields
    {
      edges {
        node {
          id
        }
      }
      
    }
  }
}

In the end I had to use the REST API to write the shop metafields. So much for Shopify having gone ‘all in’ on GraphQL (to quote from one of their showcase videos). This particular omission was noted a few years ago now, I hope it finally gets plugged soon!

Leave a Reply

Your email address will not be published. Required fields are marked *