Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Understanding Order Cancellation Workflow via GraphQL API: Clarification on Confusion in Process

Solved

Understanding Order Cancellation Workflow via GraphQL API: Clarification on Confusion in Process

mansikumari
Shopify Partner
5 0 1

I am using the GraphQL API to cancel orders on Shopify. The first time I call the API with all valid mutations, it returns a job field with done as false and a job ID. However, when I make a second call to the same API, it returns an error stating "Cannot cancel a paid and fulfilled order." The first call successfully canceled the order, so I'm confused about why this error occurred on the second call. Additionally, is it not possible to retrieve the data on the same call when a job is returned, as the subsequent query for the job ID returns null in the query field because of which I am bound to make another call for the job id to fetch the data of order? Any clarification on these issues would be greatly appreciated.

Accepted Solution (1)
ShopifyDevSup
Shopify Staff
1453 238 519

This is an accepted solution.

Hey @mansikumari

 

Thanks for sharing that! I can see how that error message would be confusing and would be better suited for an open order than a cancelled order. I'm able to replicate that as well so I'll pass that on that feedback to the product teams. 

 

As for getting data on the same call, the query field does only return when the job is done. 

 

One option is a second query for the order details. Alternatively (and possibly more efficient) the orders/cancelled webhook will return once it's complete and the payload will have the order details: https://shopify.dev/docs/api/admin-rest/2024-01/resources/webhook#event-topics-orders-cancelled 

 

Hopefully that helps: 

 

- Kyle G. 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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 4 (4)

ShopifyDevSup
Shopify Staff
1453 238 519

Hey @mansikumari

 

For context, the mutations that return a job ID are typically jobs that are completed Async. 

 

The expected behaviour here would be to poll the job ID for when it has been successfully completed. https://shopify.dev/docs/api/admin-graphql/2024-04/queries/job 

 

What's likely happening here is that the job is completing before the second cancellation mutation resulting in the error you're seeing as the cancelled order is already cancelled. 

 

Hope that helps, 

 

- Kyle G.

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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

mansikumari
Shopify Partner
5 0 1

Hey Kyle G.,

Thank you for your response and the clarification provided.

Based on your explanation, it seems that the job for canceling the order is completing before the second cancellation mutation is processed. This results in the error "Cannot cancel a paid and fulfilled order" because the order has already been canceled by the first mutation when the second call is made.

However, I am still unclear about why this error occurs when hitting the same call again with the same order ID. My understanding was that if the order was already canceled, the second call should not result in an error but rather return a confirmation that the order is already canceled. Could you please provide further clarification on this aspect?

Additionally, I would like to know if there is a way to retrieve the data on the same call when a job is returned, as the subsequent query for the job ID returns null in the query field. Is there a method to fetch the data of the order without having to make another call for the job ID?

Your insights on these points would be greatly appreciated. Thank you.

- Mansi Kumari

ShopifyDevSup
Shopify Staff
1453 238 519

This is an accepted solution.

Hey @mansikumari

 

Thanks for sharing that! I can see how that error message would be confusing and would be better suited for an open order than a cancelled order. I'm able to replicate that as well so I'll pass that on that feedback to the product teams. 

 

As for getting data on the same call, the query field does only return when the job is done. 

 

One option is a second query for the order details. Alternatively (and possibly more efficient) the orders/cancelled webhook will return once it's complete and the payload will have the order details: https://shopify.dev/docs/api/admin-rest/2024-01/resources/webhook#event-topics-orders-cancelled 

 

Hopefully that helps: 

 

- Kyle G. 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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

mansikumari
Shopify Partner
5 0 1

Thanks @ShopifyDevSup  Kyle G. for considering this concern and providing me with a helpful response.
- Mansi Kumari