GraphQL examples for products

Example GraphQL queries and mutations for working with products.

Querying products

For more information, see the product object reference.

Get a product's title, description, and online store URL

{
  product(id: "gid://shopify/Product/1925886804024") {
    title
    description
    onlineStoreUrl
  }
}

View response

{
  "data": {
    "product": {
      "title": "Classic crew neck",
      "description": "This t-shirt comes in a range of colors. It's made with super-soft combed cotton and a slim fit. Cotton-blend thread in the seams guarantees an even dye.",
      "onlineStoreUrl": "https://test-shop.myshopify.com/products/classic-crew-neck"
    }
  }
}


Get the titles of a product's first five variants

{
  product(id:"gid://shopify/Product/10521578626") {
    variants(first: 5) {
     edges {
       node {
         title
        }
      }
    }
  }
}

View response

{
  "data": {
    "product": {
      "variants": {
        "edges": [
          {
            "node": {
              "title": "Small / Blue"
            }
          },
          {
            "node": {
              "title": "Small / Green"
            }
          },
          {
            "node": {
              "title": "Medium / Blue"
            }
          },
          {
            "node": {
              "title": "Medium / Green"
            }
          },
          {
            "node": {
              "title": "Large / Blue"
            }
          }
        ]
      }
    }
  }
}


Get two specific products by their ID using aliases

{
  glasses: product(id: "gid://shopify/Product/10521584002") {
    title
    description
  }
  shoes: product(id: "gid://shopify/Product/10521582402") {
    title
    description
  }
}

View response

{
  "data": {
    "glasses": {
      "title": "Black Ban Glasses",
      "description": "Shady!"
    },
    "shoes": {
      "title": "Blue French Shoes",
      "description": "These shoes are so bleu"
    }
  }
}


Get the first five products created before a specific year

{
  products(query: "created_at:<2019", first: 5) {
    edges {
      node {
        title
        description
      }
    }
  }
}

View response

{
  "data": {
    "products": {
      "edges": [
        {
          "node": {
            "title": "Personal shopping",
            "description": "Pay what you want"
          }
        },
        {
          "node": {
            "title": "Digital product",
            "description": "This is a digital product. View a sample."
          }
        },
        {
          "node": {
            "title": "Free product",
            "description": "Requires shipping"
          }
        },
        {
          "node": {
            "title": "Fancy product",
            "description": "A very fancy product."
          }
        },
        {
          "node": {
            "title": "Wood product",
            "description": "A product made of wood."
          }
        }
      ]
    }
  }
}


Get the total inventory and price range of three products using a fragment

{
  Glasses: product(id: "gid://shopify/Product/10521584002") {
    ...productInformation
  }
  Shoes: product(id: "gid://shopify/Product/10521582402") {
    ...productInformation
  }
  Bracelet: product(id: "gid://shopify/Product/10521581186") {
    ...productInformation
  }
}

fragment productInformation on Product {
  totalInventory
  priceRange {
    minVariantPrice {
      amount
    }
    maxVariantPrice {
      amount
    }
  }
}

View response

{
  "data": {
    "Glasses": {
      "totalInventory": 55,
      "priceRange": {
        "minVariantPrice": {
          "amount": "1900.0"
        },
        "maxVariantPrice": {
          "amount": "1900.0"
        }
      }
    },
    "Shoes": {
      "totalInventory": 133,
      "priceRange": {
        "minVariantPrice": {
          "amount": "30000.0"
        },
        "maxVariantPrice": {
          "amount": "30000.0"
        }
      }
    },
    "Bracelet": {
      "totalInventory": 71,
      "priceRange": {
        "minVariantPrice": {
          "amount": "900.0"
        },
        "maxVariantPrice": {
          "amount": "1900.0"
        }
      }
    }
  }
}


Get the first 5 products in a specific collection

{
  products(first:5, query:"collection_type:t-shirt"){
    edges{
      node{
        id
      }
    }
  }
}

View response

{
  "data": {
    "products": {
      "edges": [
        {
          "node": {
            "id": "gid://shopify/Product/10521577986"
          }
        },
        {
          "node": {
            "id": "gid://shopify/Product/10521578050"
          }
        },
        {
          "node": {
            "id": "gid://shopify/Product/10521578178"
          }
        },
        {
          "node": {
            "id": "gid://shopify/Product/10521578306"
          }
        },
        {
          "node": {
            "id": "gid://shopify/Product/10521578370"
          }
        }
      ]
    }
  }
}


Get all a product's fields and connections

{
  product(id: "gid://shopify/Product/10521578626") {
    availablePublicationCount
    collections(first: 5) {
      edges {
        node {
          handle
        }
      }
    }
    createdAt
    defaultCursor
    description
    descriptionHtml
    featuredImage {
      id
    }
    feedback {
      details {
        messages {
          message
        }
      }
    }
    giftCardTemplateSuffix
    handle
    hasOnlyDefaultVariant
    hasOutOfStockVariants
    id
    images(first: 5) {
      edges {
        node {
          id
        }
      }
    }
    inCollection(id: "gid://shopify/Collection/425038914")
    isGiftCard
    legacyResourceId
    metafield(key: "app_key", namespace: "affiliates") {
      description
    }
    metafields(first: 5) {
      edges {
        node {
          description
        }
      }
    }
    onlineStorePreviewUrl
    onlineStoreUrl
    options {
      name
    }
    priceRange {
      maxVariantPrice {
        amount
      }
      minVariantPrice {
        amount
      }
    }
    productType
    publicationCount
    publishedAt
    resourcePublications(first: 5) {
      edges {
        node {
          isPublished
        }
      }
    }
    seo {
      title
    }
    storefrontId
    tags
    templateSuffix
    title
    totalInventory
    totalVariants
    tracksInventory
    unpublishedPublications(first: 5) {
      edges {
        node {
          name
        }
      }
    }
    updatedAt
    variants(first: 5) {
      edges {
        node {
          displayName
        }
      }
    }
    vendor
  }
}

View response

{
  "data": {
    "product": {
      "availablePublicationCount": 3,
      "collections": {
        "edges": [
          {
            "node": {
              "handle": "great-gifts"
            }
          }
        ]
      },
      "createdAt": "2017-09-18T14:05:28Z",
      "defaultCursor": "eyJsaW1pdCI6MSwib3JkZXIiOiJpZCBhc2MiLCJsYXN0X2lkIjoxMDUyMTU3ODYyNiwibGFzdF92YWx1ZSI6MTA1MjE1Nzg2MjYsImRpcmVjdGlvbiI6Im5leHQifQ==",
      "description": "This t-shirt comes in a range of colors. It's made with super-soft combed cotton and a slim fit. Cotton-blend thread in the seams guarantees an even dye.",
      "descriptionHtml": "<meta charset=\"utf-8\"><span>This t-shirt comes in a range of colors.  It's made with super-soft combed cotton and a slim fit.  Cotton-blend thread in the seams guarantees an even dye.</span>",
      "featuredImage": {
        "id": "gid://shopify/ProductImage/23213976514"
      },
      "feedback": {
        "details": []
      },
      "giftCardTemplateSuffix": null,
      "handle": "classic-crew-neck",
      "hasOnlyDefaultVariant": false,
      "hasOutOfStockVariants": false,
      "id": "gid://shopify/Product/10521578626",
      "images": {
        "edges": [
          {
            "node": {
              "id": "gid://shopify/ProductImage/23213976514"
            }
          },
          {
            "node": {
              "id": "gid://shopify/ProductImage/23213976578"
            }
          },
          {
            "node": {
              "id": "gid://shopify/ProductImage/23213976642"
            }
          }
        ]
      },
      "inCollection": false,
      "isGiftCard": false,
      "legacyResourceId": "10521578626",
      "metafield": null,
      "metafields": {
        "edges": []
      },
      "onlineStorePreviewUrl": "https://test-shop.myshopify.com/products/classic-crew-neck",
      "onlineStoreUrl": "https://test-shop.myshopify.com/products/classic-crew-neck",
      "options": [
        {
          "name": "Size"
        },
        {
          "name": "Color"
        }
      ],
      "priceRange": {
        "maxVariantPrice": {
          "amount": "4200.0"
        },
        "minVariantPrice": {
          "amount": "4200.0"
        }
      },
      "productType": "",
      "publicationCount": 4,
      "publishedAt": "2017-09-18T14:05:17Z",
      "resourcePublications": {
        "edges": [
          {
            "node": {
              "isPublished": true
            }
          },
          {
            "node": {
              "isPublished": true
            }
          },
          {
            "node": {
              "isPublished": true
            }
          }
        ]
      },
      "seo": {
        "title": null
      },
      "storefrontId": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzEwNTIxNTc4NjI2",
      "tags": [],
      "templateSuffix": null,
      "title": "Classic crew neck",
      "totalInventory": 12,
      "totalVariants": 6,
      "tracksInventory": true,
      "unpublishedPublications": {
        "edges": [
          {
            "node": {
              "name": "Google Shopping"
            }
          }
        ]
      },
      "updatedAt": "2019-01-23T01:35:15Z",
      "variants": {
        "edges": [
          {
            "node": {
              "displayName": "Classic crew neck - Small / Blue"
            }
          },
          {
            "node": {
              "displayName": "Classic crew neck - Small / Green"
            }
          },
          {
            "node": {
              "displayName": "Classic crew neck - Medium / Blue"
            }
          }
        ]
      },
      "vendor": "John's Apparel"
    }
  }
}

Creating products

For more information, see the productCreate mutation reference.

Create a product and return the product ID

mutation {
  productCreate(input: {title: "Sweet new product", productType: "Snowboard", vendor: "JadedPixel"}) {
    product {
      id
    }
  }
}

View response

{
  "data": {
    "productCreate": {
      "product": {
        "id": "gid://shopify/Product/1925886804024"
      }
    }
  }
}


Create a product with a metafield

mutation($input: ProductInput!) {
  productCreate(input: $input) {
    product {
      id
    }
  }
}

Variables

{
  "input": {
    "title": "Sweet new product",
    "productType": "Snowboard",
    "vendor": "JadedPixel",
    "metafields": {
      "namespace": "supply chain",
      "key": "manufacturer",
      "value": "Board Corp.",
      "valueType": "STRING"
    }
  }
}

View response

{
  "data": {
    "productCreate": {
      "product": {
        "id": "gid://shopify/Product/1926606422072"
      }
    }
  }
}


Duplicate a product

mutation {
  productDuplicate(productId: "gid://shopify/Product/10521584002", newTitle: "Duplicate product title") {
    newProduct {
      id
      title
    }
  }
}

View response

{
  "data": {
    "productDuplicate": {
      "newProduct": {
        "id": "gid://shopify/Product/1927129890872",
        "title": "Duplicate product title"
      }
    }
  }
}

Updating products

For more information, see the productUpdate mutation reference.

Update a product's title and return the product ID

mutation {
  productUpdate(input: {id: "gid://shopify/Product/1925886804024", title: "Sweet new product - GraphQL Edition"} ) {
    product {
      id
    }
  }
}

View response

{
  "data": {
    "productUpdate": {
      "product": {
        "id": "gid://shopify/Product/1925886804024"
      }
    }
  }
}


Update a product's tags

mutation {
  productUpdate(input: {id: "gid://shopify/Product/1925886804024", tags: ["Jacket, Warm"]}) {
    product {
      id
      tags
    }
  }
}

View response

{
  "data": {
    "productUpdate": {
      "product": {
        "id": "gid://shopify/Product/1925886804024",
        "tags": [
          "Jacket",
          "Warm"
        ]
      }
    }
  }
}


Reorder a product's images

mutation productReorderImages($id: ID!, $moves: [MoveInput!]!) {
  productReorderImages(id: $id, moves: $moves) {
    job {
      id
    }
  }
}

Variables

{
  "id": "gid://shopify/Product/10521578626",
  "moves": [
    {
      "id": "gid://shopify/ProductImage/23213981506",
      "newPosition": "2"
    }
  ]
}

View response

{
  "data": {
    "productReorderImages": {
      "job": {
        "id": "gid://shopify/Job/e2193067-88ae-4e08-9768-6cc746bdb473"
      }
    }
  }
}

Deleting products

For more information, see the productDelete mutation reference.

Delete a product and return its ID

mutation {
  productDelete(input:{id: "gid://shopify/Product/1925886804024"})
  {
    deletedProductId
  }
}

View response

{
  "data": {
    "productDelete": {
      "deletedProductId": "gid://shopify/Product/1925886804024"
    }
  }
}

Sign up for a Partner account to get started.

Sign up