Use variables in Shopify Bulk Operations graphql mutation

Viewed 416

I am trying to use graphql to query a store's orders in a bulk operation. I am using a tags variable in the graphql request to query orders with specific tags. I have been following Shopify's documentation in order to do this.

Here is what I've been trying:

const variables = {
  "tags": "tag:foo AND tag:bar"
};

const query = gql`
  mutation getOrders($tags: String!) {  
    bulkOperationRunQuery(
      query:"""
        query {
          orders(query: $tags) {
            edges {
              node {
                id
                email
              }
            }
          }
        }
      """
    ) {
      bulkOperation {
        id
        url
        status
      }
      userErrors {
        field
        message
      }
    }
  }
`;

const bulkOperation = await graphQLClient.request(query, variables);

However, I always get this error back from the api:

Variable $tags is declared by getOrders but not used

Does anyone know if there is a way to use a graphql variable in that bulk operation? Thanks!

1 Answers

""" is multiline string based on docs, which would means its just a string and no vars are allowed. You can use js variable substitution like ${tags} inside templated string and bypass this all together or try if concatenation works like """ 1st part """" + $tags + """ 2nd part """

Related