iamskok
iamskok

Reputation: 630

Shopify GraphQL Admin API product metafield mutation with graphql-request

I'm following Retrieving metafields with the Storefront API doc with graphql-request library, but I'm unable to pass variables to GraphQL queries.

I have already tried constructing the same request using curl, but was getting the same error message.

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

I expect to get a successful response instead, I get:

{ 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] } } }

Upvotes: 1

Views: 1180

Answers (1)

puelo
puelo

Reputation: 6047

You are using an old version of their GraphQL API.

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

vs

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

The mutation you are trying to access got defined for 2019-07 (https://help.shopify.com/en/api/versioning/migration-guides/2019-07)

Upvotes: 1

Related Questions