Graphql 2024-4 get the id of the default-variant after productCreate

Hello,

i use graphql 2024-4 to create a simple product without any variants.

to set the price i need to know the id of the default variant.

for 2024-4 i dont know how to do this.

or in gerneral : how can i get all the Variants of product , usind the Id of the product withe graphql 2024-4

best Regards

ok, i have find the solution now

use now this Query to get the id of default_variant

query {
productVariants(first: 10, , query: "product_id:123456789" )) {
edges {
node {
id
}
}
}
}

Hey @peakpack

You have reached the German community here but we can chat in English too, that’s no problem!

It looks like you found the solution to your problem using the Shopify GraphQL API. To get the default variant ID of a product, you can use the productVariants query with a filter on the product ID. Here’s the corrected version of your query that you found:

query {
  productVariants(first: 10, query: "product_id:123456789") {
    edges {
      node {
        id
      }
    }
  }
}

This query will return the first 10 variants of the product with the specified product ID (123456789) and the response will contain the IDs of these variants, from which you can extract the default variant ID. Here how you can generally structure your query to get all variants of a product using its ID:

query getProductVariants($productId: ID!) {
  product(id: $productId) {
    variants(first: 100) {
      edges {
        node {
          id
          title
          sku
          price
        }
      }
    }
  }
}

Query Variables:

{
  "productId": "gid://shopify/Product/123456789"
}
  • Replace "gid://shopify/Product/123456789" with the actual product ID in Shopify’s Global ID format.
  • The variants(first: 100) retrieves up to 100 variants of the product. Adjust the number as needed.

Example response:

{
  "data": {
    "product": {
      "variants": {
        "edges": [
          {
            "node": {
              "id": "gid://shopify/ProductVariant/1234567890",
              "title": "Default Title",
              "sku": "SKU12345",
              "price": "19.99"
            }
          },
          // Other variants...
        ]
      }
    }
  }
}

This will return the list of variants for the product, including the default variant. You can then use the ID of the default variant to set the price or perform other operations.

Hope that helps anybody else trying to solve the same issue! :wink: