我无法使用 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"
}
}
奇怪的是,根据 Shopify 的说法,
checkoutCreate
显然是一个突变。 在此处查看页面链接
然后我注意到,那个页面上的突变是不同的。所以我正在尝试那个版本,没有像这样的
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
我相信您的输入被解析为 JSON,因此在测试时尝试在突变变量的嵌套属性周围加上引号。
{
"input": {
"lineItems": [{ "variantId": "gid://shopify/ProductVariant/46037988422",
"quantity": 1 }]
}
}
完成结帐的突变仅适用于销售渠道。这些应用程序必须是公开的。因此,如果您正在创建私人应用程序,它可能无法正常工作。
https://shopify.dev/tutorials/create-a-checkout-with-storefront-api https://shopify.dev/tutorials/authenticate-a-public-app-with-oauth#turn-an-app-into-a-sales-channel