I recently started developing an e-commerce website using Next.js and Shopify's storefront API. I have successfully set up the connection and can list all the products from the API.
However, I encountered an issue when attempting to add a product for the first time by calling a specific function. The error arises during the creation of a new cart:
export async function addToCart(itemId: string, quantity: string) {
const createCartMutation = gql`
mutation createCart($cartInput: CartInput) {
cartCreate(input: $cartInput) {
cart {
id
}
}
}
`;
const variables = {
cartInput: {
lines: [
{
quantity: parseInt(quantity),
merchandiseId: itemId,
},
],
},
};
try {
const d = await graphQLClient.request(createCartMutation, variables);
return d;
} catch (error: any) {
debugger;
throw new Error(error);
}
}
The error message is:
https://i.sstatic.net/Zrzfi.png
It's worth noting that the two available products do not have variants, so the itemId
corresponds to the actual product ID rather than a variant ID.
I'm struggling to troubleshoot this issue and would greatly appreciate any assistance or insights. Thank you!
UPDATE: Performing the same mutation in the GraphQL explorer provided by Shopify yields successful results.