A developer is experiencing issues with Shopify’s GraphQL API for bulk inventory quantity adjustments in C#. Despite receiving HTTP 200 responses, inventory updates aren’t reflecting on the Shopify site, and API responses return null.
I have a function in C# which takes an array of validly formatted objects and passes them to the GraphQL API, and I get status 200 as a response… However, nothing ever updates on the Shopify Site. And the response is always null.
Here is the code
int batchSize = 100;
string ShopifyGraphURL = _myShopifyUrl + "/admin/api/2023-07/graphql.json";
var graphQLClient = new GraphQLHttpClient(ShopifyGraphURL, new NewtonsoftJsonSerializer());
graphQLClient.HttpClient.DefaultRequestHeaders.Add("X-Shopify-Access-Token", this._accessToken);
var batches = vplItems
.Select((item, index) => new { Item = item, Index = index })
.GroupBy(x => x.Index / batchSize)
.Select(group => group.Select(x => x.Item).ToList())
.ToList();
foreach (var batch in batches)
{
var request = new GraphQLRequest
{
Query = @"
mutation InventoryBulkAdjustQuantitiesAtLocationMutation($invItemAdjustments: [InventoryAdjustItemInput!]!) {
inventoryBulkAdjustQuantityAtLocation(inventoryItemAdjustments: $invItemAdjustments, locationId: ""gid://shopify/Location/
From a quick look, the code you have posts seems okay. Here are a couple of things you can check:
Permissions: Make sure the access token you are using has the right permissions to update the inventory.
Data: Ensure the data you are sending in the invItemAdjustments variable is correctly formatted and contains valid inventory item IDs and location IDs.
Error Handling: The response.Errors only contains GraphQL syntax errors. Shopify also returns user errors in the userErrors field in the response body which describes the application-specific errors. You are querying it, but not checking it. These are errors like missing permissions or invalid data.
Location ID: You’ve mentioned <correct> in the locationId. Ensure you’re replacing this with a valid location ID.
Here’s how you can modify your code to handle userErrors:
if (response.Data?.InventoryBulkAdjustQuantitiesAtLocationMutation?.UserErrors?.Count > 0)
{
// Handle user errors for this batch
foreach (var error in response.Data.InventoryBulkAdjustQuantitiesAtLocationMutation.UserErrors)
{
_logger.LogError("User error from GraphQL API in field {0}: {1}", error.Field, error.Message);
}
}
else
{
InventoryBulkAdjustResponse inventoryLevels = response.Data;
// Rest of your code
}
Note: The InventoryBulkAdjustQuantitiesAtLocationMutation should match the name you provide in your deserialized response class. If you’re still encountering issues, try running the same mutation query manually in the Shopify GraphiQL app to see if you get any additional errors there.