Adding Fragments to BulkOperation

Topic summary

Issue: A user is attempting to add GraphQL fragments to a BulkOperationRunQuery mutation but encounters errors when trying to use fragments defined elsewhere in their codebase (e.g., Shopify::CustomerBasicFragment).

Solution Provided:

  • Fragments must be defined within the query argument string of bulkOperationRunQuery, not externally
  • The bulk operation runs independently and cannot access fragments or variables defined outside its scope
  • The fragment must be defined on the correct GraphQL type (e.g., CustomerFields fragment on Customer type)

Example Structure:

fragment CustomerFields on Customer { ... }
{ orders { ... customer { ...CustomerFields } } }

Resolution: The original poster identified that their GraphQL library doesn’t properly support fragment inclusion when fragments are namespaced (e.g., Namespaced::Scoped). They will need to either fix the library or use an alternative solution.

Summarized with AI on November 16. AI used: claude-sonnet-4-5-20250929.

Hi.

I have a basic bulk operation running , but I can’t figure out how to properly add fragments to BulkOperationRunQuery

I have other operations that for example do Customer search that use fragments – no problem, but not inside BulkOperationRunQuery.

Here is my currently working basic graphql request:

mutation {
  bulkOperationRunQuery(
    query: """
      {
           orders(query: "(created_at:>=2023-08-23 AND created_at:<2023-08-24) OR (updated_at:>=2023-08-23 AND updated_at:<2023-08-24)", reverse: true) {
               edges {
                   node {
                       id
                       createdAt
                       customer {
                           id
                           firstName
                           lastName
                           email
                       }
                       
                   }
               }
           }
      }
    """
  ) {
    bulkOperation {
      id
      status
      query
    }
    userErrors {
      field
      message
    }
  }
}

But when I stry to use my Shopify::CustomerBasicFragment - defined elsewhere and used in other operations I run into all kinds of issues (I tried to construct my query many different ways)

How do I properly provide my Customer Fragment to The bullkoperation?

Thank you

Hi Birica,

If you want to use fragments in your bulk operation query, you would specify it as part of the query argument of the bulkOperationRunQuery mutation. This is similar to how you might use a fragment in a non-bulk operation query.

The key thing to note is that the fragment should be defined within the query argument itself. This is because the bulk operation runs independently and doesn’t have access to any fragments or variables defined outside of the query argument.

Here’s an example of how you might use a fragment within a bulk operation:

mutation {
  bulkOperationRunQuery(
    query: """
      fragment CustomerFields on Customer {
        id
        firstName
        lastName
        email
      }

      {
        orders(query: "(created_at:>=2023-08-23 AND created_at:<2023-08-24) OR (updated_at:>=2023-08-23 AND updated_at:<2023-08-24)", reverse: true) {
          edges {
            node {
              id
              createdAt
              customer {
                ...CustomerFields
              }
            }
          }
        }
      }
    """
  ) {
    bulkOperation {
      id
      status
      query
    }
    userErrors {
      field
      message
    }
  }
}

In this example, the CustomerFields fragment is used to select fields from the customer field of each order. The fragment is defined within the query argument, and then used within the same query argument.

Remember that the fragment must be defined on the correct type. In this case, the CustomerFields fragment is defined on the Customer type because it’s used to select fields from the customer field, which is of type Customer.

Also, ensure that the fragment is used on a field of the correct type. If the fragment is defined on the Customer type, then it can only be used on fields of the Customer type. If you still run into issues, please provide the error messages you’re seeing.

Hope this helps!

I see. Thank you for the explanation. It helped me figure out what is going on. It seems that the library I’m using for GraphQL communication doesn’t support fragment inclusion properly if fragments are Named::Spaced - like so. I will need to either fix it or use another one.

Thank you for a detailed answer.