GraphQL Admin API Metafields

jt274
Shopify Partner
15 1 8

Looking at the API documentation, in the REST Admin API you can send a POST or PUT request to /admin/metafields.json to create metafields in the Shop resource. Is there an equivalent for the GraphQL Admin API?

 

I am writing an app and trying to store some simple data within Shopify itself (for app settings) and metafields seem like the way to go. But I can't find documentation anywhere for the GraphQL Admin API on how to do this. I've found info on queries and mutations, but don't see any equivalent mutations for writing Shop metafields. Any help is greatly appreciated!

Replies 3 (3)

KarlOffenberger
Shopify Partner
1873 184 900

Hi,

 

When using the GraphQL Admin API, metafields are part of the resource's input when creating or modifying it. So for instance, if you wanted to create a product and have some new metafield associated with it, you could:

 

mutation {
  productCreate(input: {
    title: "Carrot",
    descriptionHtml: "Yummy carrot"
    metafields: {
      description: "meta sample"
      namespace: "prod",
      key: "roots",
      valueType: STRING,
      value: "carrot"
    }
  }) {
    userErrors {
      field
      message
    }
    product {
      id
      metafields(first: 10) {
        edges {
          node {
            namespace
            key
            value
          }
        }
      }
    }
    shop {
      id
    }
  }
}

The metafields input field is an array of MetafieldInput and you'll find which resource types support metafields here.

 

Hope this helps!

jt274
Shopify Partner
15 1 8

In your example you use the productCreate mutation and create some metafields. How would I use a mutation to modify the "Shop" resource metafields (/admin/metafields.json endpoint in your link)? I can't find any relevant mutation. I want to create a space to store a few app settings somewhere in the Shopify admin - nothing to do with specific products, customers, pages, etc.

KarlOffenberger
Shopify Partner
1873 184 900

Ha!

 

Good catch! That one, I suppose, is laid to REST until GraphQL catches up 😉