GraphQL Admin API product metafield mutation with graphql-request

Highlighted
New Member
5 0 0

I'm following "Retrieving metafields with the Storefront API" doc with graphql-request library, but it's not clear how to
pass arguments(variables) in the query. It would be nice if the documentation will have more real-world examples.

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

 

And the error 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] } } }
```
0 Likes
Shopify Staff
Shopify Staff
1555 79 241

It looks like that feature is set to be added to a future version (2019-07). Change your request URL to 

https://${apikey}:${password}@${hostname}/admin/api/2019-07/graphql.json

and you should see success. Keep in mind though, this version is currently unstable. Use that version only for what you need to (in this case, this specific mutation) and even then, it's still pre-release.

 

We missed documenting this on the doc you referenced, so we're going to add a disclaimer there too. Thanks for the report.

 

Cheers. 

0 Likes