如何使用GraphQL突变中的变量进行批量调整库存?

时间:2019-05-15 23:22:30

标签: ruby-on-rails ruby graphql shopify

我尝试按照本文中的说明批量调整Shopify产品变体的库存项目:deleting a database

我尝试对查询中的变体ID进行硬编码,效果很好:

<<-'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

现在,我尝试将产品变体ID设置为如下变量:

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

当我尝试运行查询时,出现以下错误:

Unknown action

The action 'bulk_update_inventory' could not be found for HomeController

有人知道为什么我会出现此错误吗?

2 个答案:

答案 0 :(得分:0)

终于得到了答案! 正确的查询是:

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

“ mutation”关键字后面的“ inventoryBulkAdjustQuantityAtLocation”一词必须删除。

答案 1 :(得分:0)

检查您的路线文件,并确保为该特殊路径设置了一个文件。