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.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

GraphQL API: Get Metafields translations

Solved

GraphQL API: Get Metafields translations

egiuliani
Shopify Partner
7 0 4

Hello,

 

I'm able to get the products translation via GraphQL API but I can't figure out how and if it's possible to get translated metafields as well. More specifically I need to recover the variant metafields.

This is my current call with untranslated metafield

 

query getProduct {
  product(id: "gid://shopify/Product/6550868590685") {
    en: translations(locale: "en") {
      key
      locale
      value
    }
    de: translations(locale: "de") {
      key
      locale
      value
    }
    fr: translations(locale: "fr") {
      key
      locale
      value
    }
    variants(first: 1) {
      edges {
        node {
          metafield1: metafield(namespace: "custom", key: "main_color") {
            value
          }
        }
      }
    }
  }
}

 

 

Could someone help me?

 

Thanks so much

Accepted Solution (1)

ShopifyDevSup
Shopify Staff
1453 239 535

This is an accepted solution.

Hi @egiuliani & @Kalen_Jordan 👋

 

To query translatable resources for metafields, you can specify the `resourceType` to be `METAFIELD`, or query them by the metafield ID:

{
    translatableResources(first: 3, resourceType: METAFIELD) {
        nodes {
            resourceId
            translatableContent {
                key
                value
            }
        }
    }
    translatableResourcesByIds(first: 5, resourceIds: [
        "gid://shopify/Metafield/123", 
        "gid://shopify/Metafield/456"
    ]) {
        nodes {
            resourceId
            translatableContent {
                key
                value
            }
        }
    }
}

 

For variant metafields, it would involve a 2 step process: 

 

1. fetch all the metafield IDs for the variants:

{
     productVariants(first:5) {
        nodes {
            metafield(namespace:"custom", key:"main_color"){
                 id
            }
        }
    }
}

 

2. then use the returned metafields in the `translatableResourcesByIds` query:

{
    translatableResourcesByIds(first: 5, resourceIds: [
        "gid://shopify/Metafield/123", ...
    ]) {
        nodes {
            resourceId
            translatableContent {
                key
                value
            }
        }
    }
}

Hope that helps!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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 4 (4)

egiuliani
Shopify Partner
7 0 4

I still have the same problem, does anyone have a solution? I need a way to extract metafield translations via API, translations are entered via Translate & Adapt

Thanks

Asela123
Visitor
3 0 0

did you found solution?

Kalen_Jordan
Shopify Partner
803 39 148

Also looking for help with this.

ShopifyDevSup
Shopify Staff
1453 239 535

This is an accepted solution.

Hi @egiuliani & @Kalen_Jordan 👋

 

To query translatable resources for metafields, you can specify the `resourceType` to be `METAFIELD`, or query them by the metafield ID:

{
    translatableResources(first: 3, resourceType: METAFIELD) {
        nodes {
            resourceId
            translatableContent {
                key
                value
            }
        }
    }
    translatableResourcesByIds(first: 5, resourceIds: [
        "gid://shopify/Metafield/123", 
        "gid://shopify/Metafield/456"
    ]) {
        nodes {
            resourceId
            translatableContent {
                key
                value
            }
        }
    }
}

 

For variant metafields, it would involve a 2 step process: 

 

1. fetch all the metafield IDs for the variants:

{
     productVariants(first:5) {
        nodes {
            metafield(namespace:"custom", key:"main_color"){
                 id
            }
        }
    }
}

 

2. then use the returned metafields in the `translatableResourcesByIds` query:

{
    translatableResourcesByIds(first: 5, resourceIds: [
        "gid://shopify/Metafield/123", ...
    ]) {
        nodes {
            resourceId
            translatableContent {
                key
                value
            }
        }
    }
}

Hope that helps!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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