Why is the cart API not working in my public app?

rushikesh93
Excursionist
78 1 6

Screenshot-from-2023-06-07-20-10-39.pngHello all,
I'm develping on App using Shopify PHP App Template,

in-app I'm trying top get cart data, but its gives the error, below is my code, that I have tried,

 

MycartComponent.jsx

 

const shopDomain = 'myshop.myshopify.com';
  const accessToken = 'shpa0dfe2de8c1c791a9a8e2c7a2de9a5901';
  const apiUrl = `https://${shopDomain}/admin/api/2023-07/graphql.json`;
  //const corsAnywhereUrl = 'https://cors-anywhere.herokuapp.com/';
  const corsAnywhereUrl = 'http://localhost:8080/';
  const cartId =  "gid://shopify/Cart/1";
  const query = `
    query {
      cart(
        id: "gid://shopify/Cart/1"
      ) {
        id
        createdAt
        updatedAt
        lines(first: 10) {
          edges {
            node {
              id
              quantity          
              attributes {
                key
                value
              }
            }
          }
        }
        attributes {
          key
          value
        }
        cost {
          totalAmount {
            amount
            currencyCode
          }
          subtotalAmount {
            amount
            currencyCode
          }      
        }    
      }
    }
  `;

  axios.post(corsAnywhereUrl + apiUrl, {
      query: query
    }, {
      headers: {
        'Content-Type': 'application/json',        
        'X-Shopify-Access-Token': accessToken,
      }
    })
    .then(response => {
      console.log('Response:', response.data.data);
    })
    .catch(error => {
      console.error('Error:', error);
    });

 

below is the error.

 

{
    "errors": [
        {
            "message": "Field 'cart' doesn't exist on type 'QueryRoot'",
            "locations": [
                {
                    "line": 2,
                    "column": 7
                }
            ],
            "path": [
                "query",
                "cart"
            ],
            "extensions": {
                "code": "undefinedField",
                "typeName": "QueryRoot",
                "fieldName": "cart"
            }
        }
    ]
}

 

now, how can I fix that error, I don't understand this.
please help me with this, 

Thanks.

Shopify Beginner.
Reply 1 (1)

MrRobot7
Shopify Partner
1 0 0

Hi @rushikesh93 , did you got any solution for this issue, 'cause am also facing the same. If you got any solution, please help me with that. Thanks.