GraphQL Api Inventory Bulk Quantities Adjustments in C#

GraphQL Api Inventory Bulk Quantities Adjustments in C#

AdvancedModel
Shopify Partner
2 0 0

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/<correct>"") {
                            inventoryLevels {
                                 id
                                  available
                                  incoming
                                  item {
                                    id
                                    sku
                                  }
                                  location {
                                    id
                                    name
                                  }
                              
                              
                            }
userErrors {
field
message
}
                          }
                        }",
                    Variables = new
                    {
                        invItemAdjustments = batch 
                    }
                };
                
                var response = await graphQLClient.SendMutationAsync<InventoryBulkAdjustResponse>(request);

                if (response.Errors != null)
                {
                    // Handle errors for this batch

                    _logger.LogError ("Error from GraphQL API: {0}", response.Errors.ToString ());
                }
                else
                {
                    InventoryBulkAdjustResponse inventoryLevels = response.Data;


               
                }

 

 

 

Replies 2 (2)

Liam
Community Manager
3108 340 871

Hi AdvancedModel,

 

From a quick look, the code you have posts seems okay. Here are a couple of things you can check:

  1. Permissions: Make sure the access token you are using has the right permissions to update the inventory.

  2. Data: Ensure the data you are sending in the invItemAdjustments variable is correctly formatted and contains valid inventory item IDs and location IDs.

  3. 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.

  4. 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.

 

Hope this helps,

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

AdvancedModel
Shopify Partner
2 0 0

1. Permissions are correct

2. Data is correct

3. has been converted to a class that populates from the json

4. yes, it was redacted before posting

5, I am now trying to use another API which allows me to set Quantities as absolute Values, inventorySetOnHandQuantities

 

 

Now the inventoryAdjustmentGroup response is null, but userErrors contains a response with two nulls for field and message