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.

Field 'cost' doesn't exist on type 'CartLine'

Field 'cost' doesn't exist on type 'CartLine'

YeojinJung
Shopify Partner
2 0 0

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]
    }
  ]
}

 

Replies 3 (3)

ShopifyDevSup
Shopify Staff
1453 238 518

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

YeojinJung
Shopify Partner
2 0 0
Hello. Thank you for your email.

Thanks to your help, I could resolve the problem.

By the way, I have another question.

I want to fetch a discount title field when fetching type cart line. I want
to figure out what script discounts are applied to line items.

I know that storefront api doesn't provide a discount title for cart line
items in cart query but for checkout objects.

But is there any solution (like proxy) to fetch discount allocation title
for cart lines?

Thank you. Regards

Yeojin.
ShopifyDevSup
Shopify Staff
1453 238 518

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