skip to Main Content

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’

3

Answers


  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.

    Login or Signup to reply.
  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
    
    Login or Signup to reply.
  3. Header Content-Type must be ‘application/graphql’

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search