首页 > 解决方案 > Shopify GraphQL Checkout 创建突变

问题描述

我无法使用 Shopify 的 Graphql API 创建结帐

我实际上是从Shopify 的结帐指南中的此页面复制示例并将其粘贴到我尝试创建结帐的商店中安装的 Shopify 的 GraphiQL 应用程序中。

这是我的突变,我唯一改变的是,variantId所以它与我商店中的一个匹配:

mutation {
  checkoutCreate(input: {
    lineItems: [{ variantId: "gid://shopify/ProductVariant/46037988422", quantity: 1 }]
  }) {
    checkout {
       id
       webUrl
       lineItems(first: 5) {
         edges {
           node {
             title
             quantity
           }
         }
       }
    }
  }
}

这是我从 Shopify 得到的回复:

{
  "errors": [
    {
      "message": "Field 'checkoutCreate' doesn't exist on type 'Mutation'",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "mutation",
        "checkoutCreate"
      ],
      "extensions": {
        "code": "undefinedField",
        "typeName": "Mutation",
        "fieldName": "checkoutCreate"
      }
    }

checkoutCreate据 Shopify称,奇怪的是,这显然是一种突变。请参阅此处的页面链接

然后我注意到,该页面上的突变是不同的。所以我正在尝试那个版本,没有variable这样的:

mutation checkoutCreate(input: {
    lineItems: [{ variantId: "gid://shopify/ProductVariant/46037988422", quantity: 1 }]
  }) {
    checkout {
      id
    }
    checkoutUserErrors {
      code
      field
      message
    }
}

现在我回来的错误是:

{
  "errors": [
    {
      "message": "Parse error on \"input\" (INPUT) at [1, 25]",
      "locations": [
        {
          "line": 1,
          "column": 25
        }
      ]
    }
  ]
}

最后我用一个变量尝试了这个版本,它也失败了:

mutation checkoutCreate($input: CheckoutCreateInput!) {
  checkoutCreate(input: $input) {
    checkout {
      id
    }
    checkoutUserErrors {
      code
      field
      message
    }
  }
}

{
  "input": {
    lineItems: [{ variantId: "gid://shopify/ProductVariant/46037988422", quantity: 1 }]
  }
}

这里的错误是:

{
  "errors": [
    {
      "message": "Parse error on \"input\" (STRING) at [15, 3]",
      "locations": [
        {
          "line": 15,
          "column": 3
        }
      ]
    }
  ]
}

最重要的是,Shopify 在他们的 GraphiQL 应用程序中有交互式文档。它没有将 checkoutCreate 列为可用的突变。请参阅此屏幕截图:https ://nimb.ws/af4iHx

标签: graphqlshopify

解决方案


完成结账的突变仅适用于销售渠道。这些应用程序必须是公开的。因此,如果您正在创建私有应用程序,它可能无法正常工作。

https://shopify.dev/tutorials/create-a-checkout-with-storefront-api https://shopify.dev/tutorials/authenticate-a-public-app-with-oauth#turn-an-app-into -销售渠道


推荐阅读