Field 'cost' doesn't exist on type 'InventoryItem'

I followed the GraphQL Admin API docs but I can’t figure this out

mutation call($input: ProductInput!) {
  productCreate(input: $input) {
    product {
      handle
      status
      title
      descriptionHtml
      vendor
      productType
      customProductType
      tags
      seo {
        title
        description
      }
      images(first: 1) {
        edges {
            node {
                altText
                src
            }
        }
      }
      variants(first: 1) {
        edges {
            node {
                inventoryItem {
                    cost
                    tracked
                }
                inventoryPolicy
                inventoryQuantities {
                    avalaibleQuantity
                    locationId
                }
                price
                requiresShipping
                sku
                taxCode
                taxable
                weight
                weightUnit
            }
        }
      }
    }
    userErrors {
      message
      field
    }
  }
}

With payload

{
  "input": {
    "handle": "iphone-9-64gb-feher",
    "status": "ACTIVE",
    "title": "iPhone 9 64 GB feher",
    "descriptionHtml": "ez a legjobb telo",
    "vendor": "ZZ",
    "customProductType": "tokok",
    "collectionsToJoin": "gid://shopify/Collection/26506562745",
    "tags": "type:Hátlapi szilikon,material:Szilikon",
    "seo": {
      "title": "iPhone 9 64 GB feher - Show Me",
      "description": "ez egy naonjao meta description"
    },
  "images": {
    "altText": "a-termek-neve-elvalasztva-kotojellel",
    "src":
      "http://image-link"
  },
  "variants": [
      {
          "inventoryItem": {
        "cost": "140990.456",
        "tracked": true
      },
      "inventoryPolicy": "DENY",
      "inventoryQuantities": {
        "avalaibleQuantity": 5,
        "locationId": "gid://shopify/Location/61586669543"
      },
      "price": "129990",
      "requiresShipping": true,
      "sku": "SM-023456",
      "taxCode": "ÁFA",
      "taxable": true,
      "weight": 841.36,
      "weightUnit": "GRAMS"
      }
  ]
}
}

This mutation gives me the error below

{
    "errors": [
        {
            "message": "Field 'cost' doesn't exist on type 'InventoryItem'",
            "locations": [
                {
                    "line": 28,
                    "column": 21
                }
            ],
            "path": [
                "mutation call",
                "productCreate",
                "product",
                "variants",
                "edges",
                "node",
                "inventoryItem",
                "cost"
            ],
            "extensions": {
                "code": "undefinedField",
                "typeName": "InventoryItem",
                "fieldName": "cost"
            }
        },
        {
            "message": "Field 'inventoryQuantities' doesn't exist on type 'ProductVariant'",
            "locations": [
                {
                    "line": 32,
                    "column": 17
                }
            ],
            "path": [
                "mutation call",
                "productCreate",
                "product",
                "variants",
                "edges",
                "node",
                "inventoryQuantities"
            ],
            "extensions": {
                "code": "undefinedField",
                "typeName": "ProductVariant",
                "fieldName": "inventoryQuantities"
            }
        }
    ]
}

Please help me figure out where I might be going wrong, but these fields seem to exist in the docs but not on the API

Hey @Chadyka

I took a closer look and can confirm in our docs that the InventoryItemInput (click here) does accept the field “cost” as an input. However to return this data via the Product.variants connection, you will need to use InventoryItem (object) and unitCost (object). Below is an example mutation using those fields to write and return the data in the API response.

Mutation:

mutation productCreate($input: ProductInput!) {
	productCreate(input: $input) {
		product {
			title
			id
			variants(first: 1) {
				edges {
					node {
						id
						inventoryItem {
							id
							tracked
							unitCost {
								amount
							}
						}
					}
				}
			}
		}

		userErrors {
			field
			message
		}
	}
}

Variables:

{
	"input": {
		"title":"New Test Product",
		"variants": {
      "inventoryItem": {
        "cost": 19.99,
        "tracked": true
      }
    }
  }
}

Hope that helps clarify a bit more - Cheers

1 Like

Hey @awwdam ,

Huge thanks for your answer! I actually managed to figure this out on my own but I was just messing around with this and got the solution by mistake actually.

Can I ask why it works like this? It seems completely backwards for me but if it’s an architectural decision that brought this about than knowing the details would be infinitely useful!

1 Like

You’re confusing input and output. GraphQL lets you mutate and query at the same time, and the parameters you send in are InventoryItemInput but the ones you’re asking for in the query are InventoryItem. If you just want to mutate you can remove the whole product {} and everything, and it will return nothing.

1 Like