GraphQL Bulk Operation with variables

Is it possible to use variables in a bulk operation? Let’s say, I have a bulk operation like so

mutation
{
    bulkOperationRunQuery(query:
    """
    query o($query)
    {
  orders( query: $query)
    {
        edges{
            node{
                id
                name
            }
        }

  }
}
    """
    ){
        bulkOperation{
            id
            status
        }
        userErrors{
            field
            message
        }
    }
}

Where can I set the value of the query?

Yes, you can use variables with Bulk Operations.

I’m not sure which language & graphql client you’re using to perform the operation, and that dictates how you pass variables to the query.

If you’re using plain text, you’ll need to interpolate these variables by hand. Whereas if you’re using a GraphQL client, typically you can pass variables by reference.

I am interpolating them by hand now. However I want to start using directives, and it will become a pain in the neck.

If it can’t be done by hand, how can a GraphQL client do it?

Depends on the GraphQL client and the language you’re using.

Each GraphQL client has a slightly different API. So you’ll need to share more about your current set up for better help.

I am using .NET with StrawberryShake, however I am considering migrating to GraphQL.Client

Not familiar with DotNet myself, but this gist should help:

Basically you define interpolation variables in your query string. Then you can pass variables to the query.

This is for a regular queries, I know how to do this by hand as well (without interpolation). I am talking specifically about bulk queries.

Take a look at this cURL command.

 curl --location 'https://{SHOPNAME}.myshopify.com/admin/api/{Version}/graphql.json' 
--header 'X-Shopify-Access-Token: {TOKEN}'
 --header 'Content-Type: application/json'
 --data '{
        "query": "query o($q : String!){orders(first: 2, query: $q){edges {node { id name } } } } ",
        "variables":{"q":"created_at:>=2025-04-01 AND created_at:<2025-04-30"}
}'

I can make the query directly without using any interpolation, I send the query with the variable, and then I send the variables in a different object.

I think I understand what you’re asking now.

You want some way of passing variables to the embedded query in the outermost bulk query. Is that right?

There might be some kind of lower level utility in your GraphQL library to compile the query string, then you can interpolate that by hand into to the bulk operation query.

Yes that’s exactly what I want, I want to use variables in my embedded query. I am currently getting the string and interpolating by hand. But I want to add directives, which would add a lot of variables to my query.

I don’t think it’s supported by Shopify at the moment, but it would be nice if they added support for it. Perhaps they can add another argument, where the variables can be set from.

OK, it’s definitely not supported by shopify. I just tried this

mutation
{
    bulkOperationRunQuery(query:
    """
 query o($query : String = "created_at:>=2025-04-01 AND created_at:<2025-04-02"){
  orders(query: $query)
    {
        edges{
            node{
                id name 
            }
        }

  }
}
    """
    ){
        bulkOperation{
            id
            status
        }
        userErrors{
            field
            message
        }
    }
}

The bulk operation was successfully created, however after running a few minutes the status changed to FAILED with an errorCode of INTERNAL_SERVER_ERROR.