Ad

How To Use Variables In GraphQL Mutation For Bulk Adjust Inventory?

- 1 answer

I trying to bulk adjust inventory item of my Shopify product variants as explained in this article: https://www.shopify.com/partners/blog/multi-location_and_graphql

I tried hardcoding the variants ID in the query and it worked great :

<<-'GRAPHQL'
      mutation {
      inventoryBulkAdjustQuantityAtLocation(
        locationId: "gid://shopify/Location/5537988719",
        inventoryItemAdjustments: [
          {inventoryItemId: "gid://shopify/InventoryItem/21112836292719", availableDelta: 1},
          {inventoryItemId: "gid://shopify/InventoryItem/21112836325487", availableDelta: 10}
          ]) {
        inventoryLevels {
          available
        }
      }
    }
  GRAPHQL

Now I am trying to set the product variants ID as variables like follow:

require "graphql/client"
require "graphql/client/http"

class HomeController < ApplicationController
  API_KEY       = 'XXXXXX'.freeze
  PASSWORD      = 'XXXXXX'.freeze
  SHARED_SECRET = 'XXXXXX'.freeze
  SHOP_NAME     = 'xxxxxx'.freeze
  API_VERSION   = '2019-04'.freeze

  shop_url                      = "https://#{API_KEY}:#{PASSWORD}@#{SHOP_NAME}.myshopify.com/admin"
  ShopifyAPI::Base.site         = shop_url
  ShopifyAPI::Base.api_version  = API_VERSION
  CLIENT                        = ShopifyAPI::GraphQL.new

  BULK_ADJUST = CLIENT.parse <<-'GRAPHQL'
      mutation inventoryBulkAdjustQuantityAtLocation($inventoryItemAdjustments: [InventoryAdjustItemInput!]!, $locationId: ID!) {
        inventoryBulkAdjustQuantityAtLocation(inventoryItemAdjustments: $inventoryItemAdjustments, locationId: $locationId) {
          inventoryLevels {
            id
          }
          userErrors {
            field
            message
          }
        }
      }
  GRAPHQL

  def bulk_update_inventory
    inventoryItemAdjustments = [
        { "inventoryItemId" => "gid://shopify/InventoryItem/1234", "availableDelta" => 1 },
        { "inventoryItemId" => "gid://shopify/InventoryItem/5678", "availableDelta" => 10 }
    ]

    variables = {
        "inventoryItemAdjustments" => inventoryItemAdjustments,
        "locationId" => "gid://shopify/Location/9012"
    }

      result = CLIENT.query(BULK_ADJUST,
                          variables: variables)
    render :json => { :result => result }
  end

end

When I try to run the query I reach the following error:

Unknown action

The action 'bulk_update_inventory' could not be found for HomeController

There is anybody knows why do I have this error?

Ad

Answer

Finally got the answer! The correct query was:

BULK_ADJUST = CLIENT.parse <<-'GRAPHQL'
      mutation($inventoryItemAdjustments: [InventoryAdjustItemInput!]!, $locationId: ID!) {
        inventoryBulkAdjustQuantityAtLocation(inventoryItemAdjustments: $inventoryItemAdjustments, locationId: $locationId) {
          inventoryLevels {
            id
          }
          userErrors {
            field
            message
          }
        }
      }
  GRAPHQL

The word "inventoryBulkAdjustQuantityAtLocation" after the "mutation" keyword had to be removed.

Ad
source: stackoverflow.com
Ad