Dedicated to the Hydrogen framework, headless commerce, and building custom storefronts using the Storefront API.
I'm testing gql cart mutations and queries with storefront api.
When I tried fetching 'cost' field on type 'CartLine', I got error message for the query.
It said
"Field 'cost' doesn't exist on type 'CartLine'"
Not only the cost field, there are other fields that make same error such as 'discountAllocations' field on 'Cart.'
Why does this error occur? And how can I fix this problem?
Below is the code I executed:
const variables = {id: cartId, numCartLines: 10}; const input = {query: queries.CART_CREATE_MUTATION, variables}; const response = await fetch("https://thisisneverthat.com/api/graphql", { method: "post", headers: { "Content-Type": "application/json", "Accept": "application/json", // https://shopify.dev/api/usage/versioning "X-Shopify-API-Version": "2023-01", "X-Shopify-Storefront-Access-Token": STOREFRONT_ACCESS_TOKEN }, body: JSON.stringify(input) }); const data = await response.json();
Cart Query:
const CART_QUERY = ` query CartInfo($id: ID!, $numCartLines: Int=250) { cart(id: $id) { id
totalQuantity discountAllocations { discountedAmount { amount currencyCode } } discountCodes { applicable code } lines(first: $numCartLines) { edges { node { id quantity cost { totalAmount { amount currencyCode } } } } } } } `;
Error message:
{ errors: [ { message: "Field 'totalQuantity' doesn't exist on type 'Cart'", locations: [Array], path: [Array], extensions: [Object] }, { message: "Field 'discountAllocations' doesn't exist on type 'Cart'", locations: [Array], path: [Array], extensions: [Object] }, { message: "Field 'cost' doesn't exist on type 'CartLine'", locations: [Array], path: [Array], extensions: [Object] } ] }
Hi @YeojinJung 👋
The type and value of the `numCartLines` variable needs to be distinct, similar to the below:
query ($id: ID!, $numCartLines: Int!){
cart(id: $id) {
id
totalQuantity
lines (first: $numCartLines) {
...
With variables:
{
"id": "gid://shopify/Cart/1234",
"numCartLines": 3
}
I'd recommend trying the below CURL request that uses this format:
curl -L -X POST 'https://YOUR_STORE.myshopify.com/api/2023-01/graphql.json' \
-H 'X-Shopify-Storefront-Access-Token: YOUR_TOKEN' \
-H 'Content-Type: application/json' \
--data-raw '{"query":"query ($id: ID!, $numCartLines: Int!){ cart(id: $id) { id totalQuantity lines (first: $numCartLines) { nodes { id cost { totalAmount { amount } } } } discountCodes { code } discountAllocations { discountedAmount { amount } } } }","variables":{"id":"gid://shopify/Cart/776ec9458c9ae3401fe467fdec9239c1","numCartLines":3}}'
Hope that helps!
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
Glad that worked for you!
As for the discounts at the `CartLine` level, we can fragment on the `CartDiscountAllocation` interface to surface the discount titles as shown below.
{
cart(id:"gid://shopify/Cart/1234") {
id
lines (first: 10){
nodes {
id
discountAllocations {
discountedAmount {
amount
}
... Discounts
}
}
}
}
}
fragment Discounts on CartDiscountAllocation {
... on CartCodeDiscountAllocation{
code
}
... on CartAutomaticDiscountAllocation {
title
}
... on CartCustomDiscountAllocation {
title
}
}
Hope that helps!
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