How to set default inventory quantity for Shopify product variants using GraphQL?

I’m working on a Shopify project and need some help with the GraphQL API. I want to create product variants with a default inventory quantity of 999. Here’s what I’ve got so far:

mutation createVariants($productId: ID!, $variantData: [ProductVariantInput!]!) {
  productVariantsCreate(productId: $productId, variants: $variantData) {
    productVariants {
      id
    }
    userErrors {
      field
      message
    }
  }
}

My variant input looks like this:

const variantData = {
  price: '19.99',
  sku: 'EXAMPLE-SKU',
  inventoryItem: {
    tracked: true,
    // How do I set quantity here?
  }
}

I used to do this easily with the REST API, but I’m not sure how to set the quantity in GraphQL. Do I need to use inventory locations? If so, how can I get those without extra API permissions? Any help would be great!

my two cents: once u create the variant, grab its inventory item id and then run inventoryAdjustQuantity to set qty. you’ll likely have to do this as a separate step. hope this helps!

hey, i experienced this too. after creatin the variant, u gotta call inventoryAdjustQuantity with inventoryItem.id and your primary location id to set qty.

yeah, it’s extra step but graphql don’t have direct qty setting. hope it helps!

I’ve dealt with this exact issue before. Here’s what worked for me:

You’re right that you can’t set the quantity directly when creating variants. Instead, you need to use a two-step process.

First, create the variant as you’re doing. Then, use the inventoryAdjustQuantity mutation to set the quantity. You’ll need the inventoryItem.id from the newly created variant and the locationId of your inventory location.

To get the locationId without extra permissions, you can usually use the shop’s primary location. Try querying the shop’s locations and grab the first one:

query {
  locations(first: 1) {
    edges {
      node {
        id
      }
    }
  }
}

This approach has always worked for me without needing additional API access. It’s a bit more involved than the REST API, but it gives you more flexibility in the long run.

You’re on the right track with your GraphQL mutation. To set the default inventory quantity, you’ll need to use the inventoryAdjustQuantity mutation after creating the variant. Here’s a simplified workflow:

  1. Create the variant using productVariantsCreate
  2. Retrieve the inventoryItem.id from the newly created variant
  3. Use inventoryAdjustQuantity to set the quantity to 999

For step 3, your mutation would look something like this:

mutation adjustInventory($inventoryItemId: ID!, $locationId: ID!, $quantity: Int!) {
  inventoryAdjustQuantity(input: {
    inventoryItemId: $inventoryItemId,
    availableDelta: $quantity,
    locationId: $locationId
  }) {
    inventoryLevel {
      available
    }
    userErrors {
      field
      message
    }
  }
}

You’ll need to fetch the location ID separately, usually from the shop’s primary location. This approach should work without requiring additional API permissions.