skip to Main Content

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.

3

Answers


  1. Chosen as BEST ANSWER

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

  2. 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"
    }
    
    Login or Signup to reply.
  3. 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}`
        }
      }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search