TheDeveloperFrontend
TheDeveloperFrontend

Reputation: 57

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.

Upvotes: 0

Views: 7513

Answers (3)

Jordan Quartermain
Jordan Quartermain

Reputation: 652

Anyone else coming here for the solution. Here's the correct code. OP just used the variable input incorrectly.

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

Upvotes: 1

TheDeveloperFrontend
TheDeveloperFrontend

Reputation: 57

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
             }
            }
          }`
      });

Upvotes: 1

David Lazar
David Lazar

Reputation: 11427

Can't speak for your code but the code that works 100% of the time for me looks like this:

query($id: ID!) {
  product(id: $id) {
   id
   title
   metafield(namespace:"custom", key:"fizzbuzz") {
     value
   }
  }
}

with data:

{
  "id": "gid://shopify/Product/6698599021234"
}

Upvotes: 3

Related Questions