首页 > 解决方案 > 我可以在没有查询字段的情况下执行 GraphQL 突变吗?

问题描述

我必须将产品从一个 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"
                    }
                },
                {

标签: shopifyshopify-api

解决方案


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

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

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


推荐阅读