带graphql请求的Shopify GraphQL Admin API产品元字段突变

时间:2019-05-19 18:58:54

标签: javascript graphql shopify

我正在使用Retrieving metafields with the Storefront API库跟踪graphql-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] } } }

1 个答案:

答案 0 :(得分:1)

您正在使用其GraphQL API的旧版本。

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

vs

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

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