'Getting and Error with Shopify Graphql cartCreate on the release candidate?

I'm trying to use Shopify Graphql to create a custom basket with cartCreate Which is new and accessible on the 2020-10 endpoint:

https:// { shop } .myshopify.com/api/2020-10/graphql.json

This is my full code

import Cookies from "js-cookie";
import { GraphQLClient, gql } from "graphql-request";

async function shopifyRequest() {
  const graphQLClient = new GraphQLClient(
    "https://jamiegalbreath.myshopify.com/api/2020-10/graphql.json",
    {
      headers: {
        "Content-Type": "application/json",
        "X-Shopify-Storefront-Access-Token":
          process.env.REACT_APP_SHOPIFY_STOREFRONT_ACCESS_TOKEN,
      },
    }
  );


  const mutation = gql`
    mutation {
      cartCreate {
        cart {
          id
        }
        userErrors {
          code
          field
          message
        }
      }
    }
  `;

  const variables = {};

  const data = await graphQLClient.request(mutation, variables);

}

export async function test() {
  await shopifyRequest()
    .then(() => {
      console.log("DONE");
    })
    .catch((error) => console.error(error));
}

When the function returns I get an error: Field 'cartCreate' doesn't exist on type 'Mutation'



Solution 1:[1]

Don't know how you fixed it but run into same problem yesterday.

I was using "https://xxxx.myshopify.com/api/2021-07/graphql.json" and the 2021/10 api release will became stable almost on one week.

The interesting was that doing the following query

query getSchema {
 __schema {
    mutationType {
      fields {
        name
      }
    }
  }
}

Didn't return any cart field, so the error was from Shopify I think.

Solution for me:

Changed to the new upcoming version 2021/10 like ---> "https://xxxx.myshopify.com/api/2021-10/graphql.json", performed the same query and all the cart fields where available and everything worked fine.

Solution 2:[2]

cartCreate mutation is not available in Shopify API version 2020-10, it only available in the Shopify API version 2021-10 and above. So, use the latest version this and error will solve.

So change your URL from:

https://jamiegalbreath.myshopify.com/api/2020-10/graphql.json

To

https://jamiegalbreath.myshopify.com/api/2021-10/graphql.json

Solution 3:[3]

Header Content-Type must be 'application/graphql'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Guido Fier
Solution 2 Kishan Bharda
Solution 3 Isaque Bezerra