【问题标题】:Can I perform a GraphQL mutation without query fields?我可以在没有查询字段的情况下执行 GraphQL 突变吗?
【发布时间】:2021-10-04 14:48:20
【问题描述】:

我必须将产品从一个 Shopify 商店转移到另一个。为此,我使用 Shopify GraphQL 管理 API。我在移动数据之前对其进行了转换,因此我无法让 Shopify 帮助我。

无论如何,我从 Shopify 的 API 中获取产品,并将它们存储在 JSON 文件中。每个产品大约有 3,000 行长,包含所有变体类型。

我必须定义查询字段和变量吗?我希望只传递 Shopify 返回的整个 JSON 对象并将其提供给 GraphQL。目前我必须手动定义字段(NodeJS):

const addProduct = (product) => axios.post(apiPath, {
    query: `
        mutation productCreate($input: ProductInput!) {
            productCreate(input: $input) {
                userErrors {
                    field
                    message
                }
                product {
                    title
                    handle
                    descriptionHtml
                    ...
                }
            }
        }
    `,
    variables: {
        input: {...product}
    }
})

但是这样定义查询字段是一个很长的过程,有没有什么办法可以不用定义查询呢?

这是来自 Shopify 的产品对象的示例。我不得不限制行数:

{
    "body_html": " The Air Jordan 3 Retro 'UNC' arrives in March 2020 as an homage to Michael Jordan's alma mater. The special edition presents university-inspired colors on its dual-textured leather upper and eyestays, with elephant print panels granting an iconic touch. Valor Blue hits both Jumpman logos on the tongue and heel while the visible Air unit in the midsole and rubber outsole wrap up the styling. ",
    "price_min_usd": 0,
    "compare_at_price_max_usd": null,
    "available": false,
    "review_count": 0,
    "percent_sale_min_aud": 0,
    "variants": [
        {
            "merged_options": [
                "size:3 UK"
            ],
            "price_cad": "0.00",
            "price_usd": "0.00",
            "presentment_prices": [
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "AED"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "AUD"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "CAD"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "DKK"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "EUR"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "GBP"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "ILS"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0",
                        "currency_code": "JPY"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "QAR"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "SAR"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "USD"
                    }
                }
            ],
            "inventory_management": "shopify",
            "available": false,
            "title": "UK 3 | EU 35.5 | US 3.5",
            "price": "0.00",
            "price_aed": "0.00",
            "id": 40807884062909,
            "sku": "CT8532-104.3UK",
            "price_eur": "0.00",
            "barcode": null,
            "price_dkk": "0.00",
            "inventory_quantity": 0,
            "image": null,
            "compare_at_price": null,
            "fulfillment_service": "manual",
            "price_aud": "0.00",
            "price_ils": "0.00",
            "price_jpy": "0",
            "inventory_policy": "deny",
            "price_gbp": "0.00",
            "price_qar": "0.00",
            "price_sar": "0.00"
        },
        {
            "merged_options": [
                "size:3.5 UK"
            ],
            "price_cad": "0.00",
            "price_usd": "0.00",
            "presentment_prices": [
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "AED"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "AUD"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "CAD"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "DKK"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "EUR"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "GBP"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "ILS"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0",
                        "currency_code": "JPY"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "QAR"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "SAR"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "USD"
                    }
                }
            ],
            "inventory_management": "shopify",
            "available": false,
            "title": "UK 3.5 | EU 36 | US 4",
            "price": "0.00",
            "price_aed": "0.00",
            "id": 40807884095677,
            "sku": "CT8532-104.3.5UK",
            "price_eur": "0.00",
            "barcode": null,
            "price_dkk": "0.00",
            "inventory_quantity": 0,
            "image": null,
            "compare_at_price": null,
            "fulfillment_service": "manual",
            "price_aud": "0.00",
            "price_ils": "0.00",
            "price_jpy": "0",
            "inventory_policy": "deny",
            "price_gbp": "0.00",
            "price_qar": "0.00",
            "price_sar": "0.00"
        },
        {
            "merged_options": [
                "size:4 UK"
            ],
            "price_cad": "0.00",
            "price_usd": "0.00",
            "presentment_prices": [
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "AED"
                    }
                },
                {
                    "compare_at_price": null,
                    "price": {
                        "amount": "0.00",
                        "currency_code": "AUD"
                    }
                },
                {

【问题讨论】:

    标签: shopify shopify-api


    【解决方案1】:

    一个简单的方法是下载一个产品,您可以在其中获得有效的 Shopify 产品 JSON。现在删除所有 id 键。将该对象发送到新商店中的 Product Create。循环重复。

    您还可以批量下载产品,为您提供 JSON,然后对其进行迭代,然后以这种方式构建产品,然后调用 create。

    无论哪种方式,完成此任务的代码都很短、甜且不难。

    【讨论】:

      【解决方案2】:

      @BugHunterUK - 据我了解,您正在使用 JSON 文件将产品数据从一家商店移动到另一家商店,但您没有这样做。您可以要求 Shopify 支持人员这样做。他们会将您的所有产品从商店 A 转移到 B。

      【讨论】:

      • 遗憾的是,他们帮不上忙,因为我在重新上传数据之前正在对其进行转换。这样做完全有可能,但这只是意味着必须手动定义所有字段。
      猜你喜欢
      • 2019-12-02
      • 1970-01-01
      • 2020-11-13
      • 2013-12-01
      • 2015-01-08
      • 2020-05-11
      • 2018-10-31
      • 2021-05-29
      • 1970-01-01
      相关资源
      最近更新 更多