Skip to content

Instantly share code, notes, and snippets.

@danyn
Last active December 12, 2024 15:46
Show Gist options
  • Save danyn/0a9b842129e76394f54113b4990f0014 to your computer and use it in GitHub Desktop.
Save danyn/0a9b842129e76394f54113b4990f0014 to your computer and use it in GitHub Desktop.
SHOPIFY GQL FOR META OBJECTS

Get a meta object definition by id

query

query getMetaobjectDefinitionById($id: ID!) {
  
  metaobjectDefinition(id: $id) {
    type,
    metaobjectsCount,
  }

}

vars

{"id":"gid://shopify/MetaobjectDefinition/125403448"}

response

{
  "data": {
    "metaobjectDefinition": {
      "type": "app--2315872--swatchify",
      "metaobjectsCount": 2
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 1,
      "actualQueryCost": 1,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 999,
        "restoreRate": 50
      }
    }
  }
}

empty response

scenorios

  • object that is not accessible by the app
  • object that does that has not been created by the app yet.
{
  "data": {
    "metaobjectDefinition": null
  },

Get a metaobject definition by type

query getMetaobjectDefinitionByType($type:
String!){
  
  metaobjectDefinitionByType(type:$type) {
    id
    fieldDefinitions{
      name
      key
      type {
        name
        category
      }
      description
    }
  
}
}

vars

{"type":"app--2315872--swatchify"}

Response

{
  "data": {
    "metaobjectDefinitionByType": {
      "id": "gid://shopify/MetaobjectDefinition/125403448",
      "fieldDefinitions": [
        {
          "name": "Title",
          "key": "title",
          "type": {
            "name": "single_line_text_field",
            "category": "TEXT"
          },
          "description": null
        },
        {
          "name": "Swatch Data",
          "key": "swatch_data",
          "type": {
            "name": "json",
            "category": "JSON"
          },
          "description": null
        }
      ]
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 3,
      "actualQueryCost": 3,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 997,
        "restoreRate": 50
      }
    }
  }
}

empty response

  "data": {
    "metaobjectDefinitionByType": null
  },

1. Create a Definition for Metaobject

mutation CreateMetaobjectDefinitionWithAccessAndPublish($definition: MetaobjectDefinitionCreateInput!) {
  metaobjectDefinitionCreate(definition: $definition) {
    metaobjectDefinition {
      name
      type
      access {
        admin
        storefront
      }
      capabilities {
        publishable {
          enabled
        }
      }
      fieldDefinitions {
        name
        key
      }
    }
    userErrors {
      field
      message
      code
    }
  }
}

variables

{
  "definition": {
    "name": "CustomSwatchify",
    "type": "$app:swatchify",
    "access": {
      "admin": "PUBLIC_READ",
      "storefront": "PUBLIC_READ"
    },
    "fieldDefinitions": [
      {
        "name": "Title",
        "key": "title",
        "type": "single_line_text_field",
        "validations": [
          {
            "name": "max",
            "value": "100"
          }
        ]
      },
      {
        "name": "Swatch Data",
        "key": "swatch_data",
        "type": "json"
      }
    ]
  }
}

response

{
  "data": {
    "metaobjectDefinitionCreate": {
      "metaobjectDefinition": {
        "name": "CustomSwatchify",
        "type": "app--2315872--swatchify",
        "access": {
          "admin": "PUBLIC_READ",
          "storefront": "PUBLIC_READ"
        },
        "capabilities": {
          "publishable": {
            "enabled": false
          }
        },
        "fieldDefinitions": [
          {
            "name": "Title",
            "key": "title"
          },
          {
            "name": "Swatch Data",
            "key": "swatch_data"
          }
        ]
      },
      "userErrors": []
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 14,
      "actualQueryCost": 14,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 986,
        "restoreRate": 50
      }
    }
  }
}

Populate A metaobject of the above type

mutation UpsertMetaobject($handle: MetaobjectHandleInput!, $metaobject: MetaobjectUpsertInput!) {
  metaobjectUpsert(handle: $handle, metaobject: $metaobject) {
    metaobject {
      handle
      title: field(key: "title") {
        value
      }
      swatch_data: field(key: "swatch_data"){
        value
      }
    }
    userErrors {
      field
      message
      code
    }
  }
}

variables

{
  "handle": {
    "type": "app--2315872--swatchify",
    "handle": "abc2"
  },
  "metaobject": {
    "fields": [
      {
        "key": "title",
        "value": "swatch abc 2"
      },
      {
        "key": "swatch_data",
        "value": "{\"moHandle\":\"abc2\",\"name\":\"swatch abc 1\",\"palettes\":{\"battery\":{\"isActive\":true,\"palette\":{\"1100ma\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/1100-ma-battery.png?v=1659625612\",\"1800ma\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/1800-ma-battery.png?v=1659625612\"},\"shape\":{\"width\":\"60\",\"height\":\"60\",\"border\":false,\"borderRadius\":\"4\",\"borderWidth\":\"1\",\"borderColor\":\"#a6a4a4\",\"borderColorChecked\":\"#ff0000\"},\"usingSharedPalette\":false},\"coil type\":{\"isActive\":true,\"palette\":{\"quartz\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/quartz.jpg?v=1659720216\",\"ceramic\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/ceramic.jpg?v=1659720216\"},\"shape\":{\"width\":\"120\",\"height\":\"120\",\"border\":false,\"borderRadius\":\"4\",\"borderWidth\":\"1\",\"borderColor\":\"#a6a4a4\",\"borderColorChecked\":\"#ff0000\"},\"usingSharedPalette\":false},\"color\":{\"isActive\":true,\"palette\":{\"silver\":\"#101211\",\"black\":\"#000000\",\"pakura pink\":\"#5f4172\",\"apple green\":\"#2c2b46\",\"azure green\":\"#62749b\"},\"shape\":{\"width\":\"120\",\"height\":\"120\",\"border\":false,\"borderRadius\":\"4\",\"borderWidth\":\"1\",\"borderColor\":\"#a6a4a4\",\"borderColorChecked\":\"#ff0000\"},\"usingSharedPalette\":false}},\"scope\":[\"gid://shopify/Product/7923552420069\",\"gid://shopify/Product/7923221692645\",\"gid://shopify/Product/7923288637669\"]}"
      }
    ]
  }
}

response

{
  "data": {
    "metaobjectUpsert": {
      "metaobject": {
        "handle": "abc2",
        "title": {
          "value": "swatch abc 2"
        },
        "swatch_data": {
          "value": "{\"moHandle\":\"abc2\",\"name\":\"swatch abc 1\",\"palettes\":{\"battery\":{\"isActive\":true,\"palette\":{\"1100ma\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/1100-ma-battery.png?v=1659625612\",\"1800ma\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/1800-ma-battery.png?v=1659625612\"},\"shape\":{\"width\":\"60\",\"height\":\"60\",\"border\":false,\"borderRadius\":\"4\",\"borderWidth\":\"1\",\"borderColor\":\"#a6a4a4\",\"borderColorChecked\":\"#ff0000\"},\"usingSharedPalette\":false},\"coil type\":{\"isActive\":true,\"palette\":{\"quartz\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/quartz.jpg?v=1659720216\",\"ceramic\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/ceramic.jpg?v=1659720216\"},\"shape\":{\"width\":\"120\",\"height\":\"120\",\"border\":false,\"borderRadius\":\"4\",\"borderWidth\":\"1\",\"borderColor\":\"#a6a4a4\",\"borderColorChecked\":\"#ff0000\"},\"usingSharedPalette\":false},\"color\":{\"isActive\":true,\"palette\":{\"silver\":\"#101211\",\"black\":\"#000000\",\"pakura pink\":\"#5f4172\",\"apple green\":\"#2c2b46\",\"azure green\":\"#62749b\"},\"shape\":{\"width\":\"120\",\"height\":\"120\",\"border\":false,\"borderRadius\":\"4\",\"borderWidth\":\"1\",\"borderColor\":\"#a6a4a4\",\"borderColorChecked\":\"#ff0000\"},\"usingSharedPalette\":false}},\"scope\":[\"gid://shopify/Product/7923552420069\",\"gid://shopify/Product/7923221692645\",\"gid://shopify/Product/7923288637669\"]}"
        }
      },
      "userErrors": []
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 12,
      "actualQueryCost": 12,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 988,
        "restoreRate": 50
      }
    }
  }
}

Query the Type for its nodes

query getMetaobjects($type: String!){
  metaobjects(type: $type, first:100) {
    
    nodes {
      handle,
      capabilities{
        publishable{
          status
        }
      }
      fields {
        key
        value
      }
    }
  }

}

variables

json {"type": "app--2315872--swatchify"}

response

{
  "data": {
    "metaobjects": {
      "nodes": [
        {
          "handle": "abc1",
          "capabilities": {
            "publishable": null
          },
          "fields": [
            {
              "key": "title",
              "value": "swatch abc 1"
            },
            {
              "key": "swatch_data",
              "value": "{\"moHandle\":\"abc1\",\"name\":\"swatch abc 1\",\"palettes\":{\"battery\":{\"isActive\":true,\"palette\":{\"1100ma\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/1100-ma-battery.png?v=1659625612\",\"1800ma\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/1800-ma-battery.png?v=1659625612\"},\"shape\":{\"width\":\"60\",\"height\":\"60\",\"border\":false,\"borderRadius\":\"4\",\"borderWidth\":\"1\",\"borderColor\":\"#a6a4a4\",\"borderColorChecked\":\"#ff0000\"},\"usingSharedPalette\":false},\"coil type\":{\"isActive\":true,\"palette\":{\"quartz\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/quartz.jpg?v=1659720216\",\"ceramic\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/ceramic.jpg?v=1659720216\"},\"shape\":{\"width\":\"120\",\"height\":\"120\",\"border\":false,\"borderRadius\":\"4\",\"borderWidth\":\"1\",\"borderColor\":\"#a6a4a4\",\"borderColorChecked\":\"#ff0000\"},\"usingSharedPalette\":false},\"color\":{\"isActive\":true,\"palette\":{\"silver\":\"#101211\",\"black\":\"#000000\",\"pakura pink\":\"#5f4172\",\"apple green\":\"#2c2b46\",\"azure green\":\"#62749b\"},\"shape\":{\"width\":\"120\",\"height\":\"120\",\"border\":false,\"borderRadius\":\"4\",\"borderWidth\":\"1\",\"borderColor\":\"#a6a4a4\",\"borderColorChecked\":\"#ff0000\"},\"usingSharedPalette\":false}},\"scope\":[\"gid://shopify/Product/7923552420069\",\"gid://shopify/Product/7923221692645\",\"gid://shopify/Product/7923288637669\"]}"
            }
          ]
        },
        {
          "handle": "abc2",
          "capabilities": {
            "publishable": null
          },
          "fields": [
            {
              "key": "title",
              "value": "swatch abc 2"
            },
            {
              "key": "swatch_data",
              "value": "{\"moHandle\":\"abc2\",\"name\":\"swatch abc 1\",\"palettes\":{\"battery\":{\"isActive\":true,\"palette\":{\"1100ma\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/1100-ma-battery.png?v=1659625612\",\"1800ma\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/1800-ma-battery.png?v=1659625612\"},\"shape\":{\"width\":\"60\",\"height\":\"60\",\"border\":false,\"borderRadius\":\"4\",\"borderWidth\":\"1\",\"borderColor\":\"#a6a4a4\",\"borderColorChecked\":\"#ff0000\"},\"usingSharedPalette\":false},\"coil type\":{\"isActive\":true,\"palette\":{\"quartz\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/quartz.jpg?v=1659720216\",\"ceramic\":\"https://cdn.shopify.com/s/files/1/0654/5198/3077/files/ceramic.jpg?v=1659720216\"},\"shape\":{\"width\":\"120\",\"height\":\"120\",\"border\":false,\"borderRadius\":\"4\",\"borderWidth\":\"1\",\"borderColor\":\"#a6a4a4\",\"borderColorChecked\":\"#ff0000\"},\"usingSharedPalette\":false},\"color\":{\"isActive\":true,\"palette\":{\"silver\":\"#101211\",\"black\":\"#000000\",\"pakura pink\":\"#5f4172\",\"apple green\":\"#2c2b46\",\"azure green\":\"#62749b\"},\"shape\":{\"width\":\"120\",\"height\":\"120\",\"border\":false,\"borderRadius\":\"4\",\"borderWidth\":\"1\",\"borderColor\":\"#a6a4a4\",\"borderColorChecked\":\"#ff0000\"},\"usingSharedPalette\":false}},\"scope\":[\"gid://shopify/Product/7923552420069\",\"gid://shopify/Product/7923221692645\",\"gid://shopify/Product/7923288637669\"]}"
            }
          ]
        }
      ]
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 402,
      "actualQueryCost": 9,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 991,
        "restoreRate": 50
      }
    }
  }
}

A meta object can be referenced in a metafield

  • the metafield could be attached to a product resource
  • that product could be queried from the top nodes and still get the metafield by its type and key
query productsByGid($ids:[ID!]!, $transform:ImageTransformInput, $namespace: String, $key: String!) {
  nodes(ids: $ids) {
    ...on Product {
      id
      title
      featuredImage{
        url(transform:$transform)
      }
      options {
        name 
        values
      }
      metafield(namespace:$namespace, key:$key) {
        value
        id
      }
    }
  }
}

vars

{
  "ids": ["gid://shopify/Product/8131839328568"], 
  "namespace": "custom", 
  "key": "swatchify",
  "transform": {"maxWidth": 200, "scale": 2}
}

response

{
  "data": {
    "nodes": [
      {
        "id": "gid://shopify/Product/8131839328568",
        "title": "The 3p Fulfilled Snowboard",
        "featuredImage": {
          "url": "https://cdn.shopify.com/s/files/1/0725/7718/8152/products/[email protected]?v=1675888407"
        },
        "options": [
          {
            "name": "Title",
            "values": [
              "Default Title"
            ]
          }
        ],
        "metafield": {
          "value": "gid://shopify/Metaobject/6914360",
          "id": "gid://shopify/Metafield/28515140141368"
        }
      }
    ]
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 4,
      "actualQueryCost": 4,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 996,
        "restoreRate": 50
      }
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment