A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
Im using graph ql and the storefront api, currently not able to fetch my cart:
Here is my mutation to create a cart:
mutation CreateCheckout { checkoutCreate(input: {}) { checkoutUserErrors { message } checkout { webUrl id } } }
Here is my response:
{ "data": { "checkoutCreate": { "checkoutUserErrors": [], "checkout": { "webUrl": "https://exampledemo9.myshopify.com/79379857727/checkouts/95fc07e10e53218fdba96451e45f4eeb?key=58af8673d32769d7e82e3f5ec8fa6dee", "id": "gid://shopify/Checkout/95fc07e10e53218fdba96451e45f4eeb?key=58af8673d32769d7e82e3f5ec8fa6dee" } } } }
Here is my query to get my cart:
query GetCart { cart( id: "gid://shopify/Checkout/95fc07e10e53218fdba96451e45f4eeb?key=58af8673d32769d7e82e3f5ec8fa6dee" ) { checkoutUrl id } }
And here is the response i get (this is using the shopify graphql explorer)
{
"data": {
"cart": null
},
"errors": [
{
"message": "invalid id",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"cart"
]
}
]
}
I am not understanding why my checkout Id is invalid. Its the same id that i get back in the response when i created the cart and added an item to the cart. For whatever reason I just cant seem to fetch the cart,
Hi Artsycoder,
The issue you're experiencing could be due to a misunderstanding of the differences between a 'Checkout' and a 'Cart' in Shopify's API. In Shopify, a 'Checkout' and a 'Cart' are two separate concepts. The ID you're getting back from the checkoutCreate
mutation is a Checkout ID, not a Cart ID.
So, when you're trying to use that Checkout ID in the cart
query, it's returning an error because it's expecting a Cart ID instead. If you want to fetch the Checkout you just created, you should use the checkout
query instead of the cart
query, like this:
query GetCheckout {
checkout(
id: "gid://shopify/Checkout/95fc07e10e53218fdba96451e45f4eeb?key=58af8673d32769d7e82e3f5ec8fa6dee"
) {
webUrl
id
}
}
This should return the checkout information you're looking for. Let me know if this helps or if you have any other questions!
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
Hello, thank you for your reply. The reason I used `cart` instead of `checkout` is that it didnt appear that `checkout` was an option here (i get a red squiggly underlining checkout telling me so) but i saw `cart` was an option so i went with that. Note I am using the storefront API, but i dont see this as an option for Admin either anyway.
Using this query:
query GetCheckout {
checkout(
id: "gid://shopify/Checkout/95fc07e10e53218fdba96451e45f4eeb?key=58af8673d32769d7e82e3f5ec8fa6dee"
) {
webUrl
id
}
}
Using `checkout` this is the response i get:
{
"errors": [
{
"message": "Field 'checkout' doesn't exist on type 'QueryRoot'",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"query GetCheckout",
"checkout"
],
"extensions": {
"code": "undefinedField",
"typeName": "QueryRoot",
"fieldName": "checkout"
}
}
]
}
Let me get this straight, First I have to create a checkout or am i creating a new cart? Then take this id so that I can add line items to it. If I want to then display the cart contents from shopify to see quantity, subtotal etc. i'm supposed to get the checkout to do this or the cart? The naming is confusing, even more so that checkout isnt an option to use in the query, so im confused as to what im supposed to do here? I currently have a custom front end im working on thats basically useless because i cant query the cart/checkout whichever one im supposed to be using.
Hi @artsycoder,
We have a guide on how to create and manage carts with the storefront API in our developer documentation here.
For most use cases it's recommended to create and manage a cart, then redirect a customer to the Shopify checkout at the cart's checkoutUrl. There is also a Hydrogen, Headless, and Storefront APIs board in the forums for more specific questions about how to manage your headless storefront.
The cart query on the Storefront API's query root is what you are using above, and can only retrieve cart objects. You are correct that there is no checkout query on the Storefront API, but a checkout object implements the node interface so it can be queried directly like this if necessary:
query checkoutNode($id: ID!){
node(id:$id){
...on Checkout{
id
webUrl
}
}
}
variables:
{
"id":"gid://shopify/Checkout/95fc07e10e53218fdba96451e45f4eeb?key=58af8673d32769d7e82e3f5ec8fa6dee"
}
Hope that helps!
- James
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