我可以在没有查询字段的情况下执行 GraphQL 变更吗?

Can I perform a GraphQL mutation without query fields?

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

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

我必须定义查询字段和变量吗?我希望只传递 Shopify returns 的整个 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"
                    }
                },
                {

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

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

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

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