Мутация метаполя продукта Shopify GraphQL Admin API с помощью graphql-запроса - PullRequest
1 голос
/ 19 мая 2019

Я слежу за Извлечение метаполей с помощью API Storefront Документ с библиотекой 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 Ответ

1 голос
/ 19 мая 2019

Вы используете старую версию API GraphQL.

/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)

...