Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: Checking if a metafield without a definition exists/retrieving its value

Solved

Checking if a metafield without a definition exists/retrieving its value

jomei1
Shopify Partner
6 0 0

Hey all, 

I'm writing an app but am having some trouble checking if a metafield (without a definition) already exists. I have a previous function that, using a GraphQL mutation, creates a metafield without a definition (with namespace: "warranty_plan" and key: "frontend"). Now, I'm trying to write a function that can later check if there already exists a metafield under this namespace/key, and if so, retrieve that metafield value. I'm having some trouble working out the syntax of it, however. My current iteration involves writing a graphql query (using the below code) that returns the metafield id so that I could then pass that into a "shopify.rest.Metafield.find" command and pull that. I'm not sure how to actually return the result of this query, however, so help with the syntax would be greatly appreciated.

 

Screenshot 2023-07-10 at 4.12.18 PM.png

 

Overall, however, I feel like this is such a roundabout way to do an otherwise incredibly simple action. Do you guys know any tools that I may be able to use that may work better? Thanks so much. 

 

Accepted Solution (1)

Liam
Community Manager
3108 344 911

This is an accepted solution.

Hi Jomei1,

 

To check if a metafield already exists, you'll want to use a GraphQL query that filters metafields by the namespace and key. Here's an example of how to write such a query for a product:

 

 

{
  product(id: "gid://shopify/Product/1234567890") {
    metafield(namespace: "warranty_plan", key: "frontend") {
      id
      value
    }
  }
}

 

 

In the above query, you'd replace `"gid://shopify/Product/1234567890"` with the actual GraphQL ID of the resource for which you're checking the metafield, and also replace `"warranty_plan"` and `"frontend"` with the namespace and key you're checking for.

 

To execute this query from your app and handle the result, you'll typically use the library or SDK that you're using for GraphQL requests. The exact code to do this will depend on the library or SDK.

Here's a rough example using Node.js and axios:

 

 

const axios = require('axios');

const data = {
  query: `
    {
      product(id: "gid://shopify/Product/1234567890") {
        metafield(namespace: "warranty_plan", key: "frontend") {
          id
          value
        }
      }
    }
  `
};

axios.post('https://your-shop-name.myshopify.com/api/2023-07/graphql.json', data, {
  headers: {
    'X-Shopify-Storefront-Access-Token': 'your-access-token',
    'Content-Type': 'application/json'
  }
})
.then(response => {
  // check the response data
  const productMetafield = response.data.data.product.metafield;
  if (productMetafield) {
    console.log('Metafield exists, ID:', productMetafield.id, 'Value:', productMetafield.value);
  } else {
    console.log('Metafield does not exist');
  }
})
.catch(error => {
  console.error('Error:', error);
});

 

 

Please replace `"gid://shopify/Product/1234567890"`, `"warranty_plan"`, `"frontend"`, `https://your-shop-name.myshopify.com/api/2023-07/graphql.json`, and `your-access-token` with your actual values.

 

In this example, if the metafield exists, it logs the metafield's ID and value. If it doesn't exist, the product's `metafield` field in the response will be `null`, and it logs that the metafield doesn't exist.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 3 (3)

Liam
Community Manager
3108 344 911

This is an accepted solution.

Hi Jomei1,

 

To check if a metafield already exists, you'll want to use a GraphQL query that filters metafields by the namespace and key. Here's an example of how to write such a query for a product:

 

 

{
  product(id: "gid://shopify/Product/1234567890") {
    metafield(namespace: "warranty_plan", key: "frontend") {
      id
      value
    }
  }
}

 

 

In the above query, you'd replace `"gid://shopify/Product/1234567890"` with the actual GraphQL ID of the resource for which you're checking the metafield, and also replace `"warranty_plan"` and `"frontend"` with the namespace and key you're checking for.

 

To execute this query from your app and handle the result, you'll typically use the library or SDK that you're using for GraphQL requests. The exact code to do this will depend on the library or SDK.

Here's a rough example using Node.js and axios:

 

 

const axios = require('axios');

const data = {
  query: `
    {
      product(id: "gid://shopify/Product/1234567890") {
        metafield(namespace: "warranty_plan", key: "frontend") {
          id
          value
        }
      }
    }
  `
};

axios.post('https://your-shop-name.myshopify.com/api/2023-07/graphql.json', data, {
  headers: {
    'X-Shopify-Storefront-Access-Token': 'your-access-token',
    'Content-Type': 'application/json'
  }
})
.then(response => {
  // check the response data
  const productMetafield = response.data.data.product.metafield;
  if (productMetafield) {
    console.log('Metafield exists, ID:', productMetafield.id, 'Value:', productMetafield.value);
  } else {
    console.log('Metafield does not exist');
  }
})
.catch(error => {
  console.error('Error:', error);
});

 

 

Please replace `"gid://shopify/Product/1234567890"`, `"warranty_plan"`, `"frontend"`, `https://your-shop-name.myshopify.com/api/2023-07/graphql.json`, and `your-access-token` with your actual values.

 

In this example, if the metafield exists, it logs the metafield's ID and value. If it doesn't exist, the product's `metafield` field in the response will be `null`, and it logs that the metafield doesn't exist.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

jomei1
Shopify Partner
6 0 0

Gotcha, thank you so much Liam! This was very helpful. 

 

logangelzer
Shopify Partner
21 5 10

Hey Liam, what about doing this for a Customer Metafield?

Logan Gelzer
Founder & CEO
Logo Media
logan@logo.media