gpt4 book ai didi

javascript - 使用 graphql-request 的 Shopify GraphQL Admin API 产品元字段突变

转载 作者:行者123 更新时间:2023-11-30 19:33:22 28 4
gpt4 key购买 nike

我正在关注 Retrieving metafields with the Storefront APIgraphql-request 记录库,但我无法将变量传递给 GraphQL 查询。

我已经尝试使用 curl 构造相同的请求,但收到相同的错误消息。

import { GraphQLClient } from 'graphql-request'

async function main() {
const endpoint = `https://${apikey}:${password}@${hostname}/admin/api/2019-04/graphql.json`

const graphQLClient = new GraphQLClient(endpoint)

const updateMetafieldStorefrontVisibility = `
mutation($input: MetafieldStorefrontVisibilityInput!) {
metafieldStorefrontVisibilityCreate(input: $input) {
metafieldStorefrontVisibility {
id
}
userErrors {
field
message
}
}
}
`
const updateMeta = await graphQLClient.request(updateMetafieldStorefrontVisibility, {
"input": {
"namespace": "global",
"key": "featured",
"ownerType": "PRODUCT"
}
})
console.log('updateMeta:', JSON.stringify(updateMeta));
}

main().catch(error => console.error(error))

我希望得到一个成功的响应,我得到:

{ Error: MetafieldStorefrontVisibilityInput isn't a defined input type (on $input): {"response":{"errors":[{"message":"MetafieldStorefrontVisibilityInput isn't a defined input type (on $input)","locations":[{"line":2,"column":14}],"path":["mutation"],"extensions":{"code":"variableRequiresValidType","typeName":"MetafieldStorefrontVisibilityInput","variableName":"input"}},{"message":"Field 'metafieldStorefrontVisibilityCreate' doesn't exist on type 'Mutation'","locations":[{"line":3,"column":7}],"path":["mutation","metafieldStorefrontVisibilityCreate"],"extensions":{"code":"undefinedField","typeName":"Mutation","fieldName":"metafieldStorefrontVisibilityCreate"}},{"message":"Variable $input is declared by  but not used","locations":[{"line":2,"column":5}],"path":["mutation"],"extensions":{"code":"variableNotUsed","variableName":"input"}}],"status":200},"request":{"query":"\n    mutation($input: MetafieldStorefrontVisibilityInput!) {\n      metafieldStorefrontVisibilityCreate(input: $input) {\n        metafieldStorefrontVisibility {\n          id\n        }\n        userErrors {\n          field\n          message\n        }\n      }\n    }\n  ","variables":{"input":{"namespace":"global","key":"featured","ownerType":"PRODUCT"}}}}
at GraphQLClient.<anonymous> (/Users/skok/dev/bva-gatsby-shopify-starter-2/plugins/gatsby-source-shopify-metafields/node_modules/graphql-request/dist/src/index.js:116:35)
at step (/Users/skok/dev/bva-gatsby-shopify-starter-2/plugins/gatsby-source-shopify-metafields/node_modules/graphql-request/dist/src/index.js:40:23)
at Object.next (/Users/skok/dev/bva-gatsby-shopify-starter-2/plugins/gatsby-source-shopify-metafields/node_modules/graphql-request/dist/src/index.js:21:53)
at fulfilled (/Users/skok/dev/bva-gatsby-shopify-starter-2/plugins/gatsby-source-shopify-metafields/node_modules/graphql-request/dist/src/index.js:12:58)
at process.internalTickCallback (internal/process/next_tick.js:77:7)
response: { errors: [ [Object], [Object], [Object] ], status: 200 },
request:
{ query:
'\n mutation($input: MetafieldStorefrontVisibilityInput!) {\n metafieldStorefrontVisibilityCreate(input: $input) {\n metafieldStorefrontVisibility {\n id\n }\n userErrors {\n field\n message\n }\n }\n }\n ',
variables: { input: [Object] } } }

最佳答案

您使用的是他们的 GraphQL API 的旧版本。

/admin/api/2019-04/graphql.json

对比

/admin/api/2019-07/graphql.json

您尝试访问的突变已定义为 2019-07 ( https://help.shopify.com/en/api/versioning/migration-guides/2019-07 )

关于javascript - 使用 graphql-request 的 Shopify GraphQL Admin API 产品元字段突变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56211047/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com