node.jsgraphqlshopifyshopify-api

Variable $id of type ID! was provided invalid value | Graphql API | Shopify | Node.js


Currently I am working on a feature that on button click a metafield gets updated. To verify that the metafield isn't full, I send a GraphQL query to look if the metafield is blank or full.

But now comes the error: It says "Variable id of type ID! was provided invalid value". I don't know what to do, to fix this error.

That is the GraphQL query:

export const VIEW_PRODUCT_METAFIELD = `
query VIEW_METAFIELD($id: ID!){
  myProduct: product(id: $id) {
    metafield(namespace:"custom", key:"myfield"){
      namespace
      key
      value
      updatedAt
    }
  }
}
`

My code is:

var shopifyId = "gid://shopify/Product/" + product_id;

      var isFull = await client.query({
        data: {
          query: VIEW_PRODUCT_METAFIELD,
          variables: {
            input: {
              id: shopifyId <=== Here occurs the error
            }
          }
        }
      });

I made a second version but neither of both work:

      var isFull = await client.query({
        data: {
          query: VIEW_PRODUCT_METAFIELD,
          variables: {
            input: {
              id: `gid://shopify/Product/${product_id}` <=== Here occurs the error
            }
          }
        }
      });

It would very help me if anyone has a solution for this.


Solution

  • I just inserted the query directly into the client.query({

    It look like this now:

    var isFull = await client.query({
            
            data: `{
              product(id: "${shopifyId}") {
                 id
                 title
                 metafield(namespace:"custom", key:"myMetafield") {
                   value
                 }
                }
              }`
          });