Solved

Adding Fragments to BulkOperation

birica
Shopify Partner
13 1 5

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

 

20 years+ programming experience (Ruby, Javascript, SCSS/SASS/CSS)
Accepted Solution (1)

Liam
Shopify Staff
2731 302 783

This is an accepted solution.

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!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 2 (2)

Liam
Shopify Staff
2731 302 783

This is an accepted solution.

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!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

birica
Shopify Partner
13 1 5

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.  

20 years+ programming experience (Ruby, Javascript, SCSS/SASS/CSS)