Troubleshooting Shopify integration with Sanity.io
32 replies
Last updated: May 20, 2022
S
Hi guys,I'm trying to connect with shopify as in this guide:
https://www.sanity.io/guides/headless-ecommerce-with-sanity-and-shopify-api
After I did every step It's giving me a
ReferenceError: homepageQuery is not defined
https://www.sanity.io/guides/headless-ecommerce-with-sanity-and-shopify-api
After I did every step It's giving me a
ReferenceError: homepageQuery is not defined
import { getClient } from "../lib/sanity.server"; import groq from "groq"; import Head from "next/head"; import Link from "next/link"; import { urlFor } from "../lib/sanity"; import { PortableText } from "@portabletext/react"; import { gql, GraphQLClient } from "graphql-request"; function HomePage({ data }) { const { homepageData, collection } = data; return ( <main className="bg-gray-50"> <div className="h-96 bg-indigo-500 flex justify-center items-center"> <h1 className="text-white font-semibold text-6xl"> {homepageData.heroTitle} </h1> </div> </main> ); } export async function getStaticProps() { const homepageData = await getClient().fetch(homepageQuery, {}); const graphQLClient = new GraphQLClient(process.env.NEXT_PUBLIC_SHOPIFY_URL, { headers: { "X-Shopify-Storefront-Access-Token": process.env.NEXT_PUBLIC_TOKEN, }, }); // Shopify Request const query = gql` { collectionByHandle(handle: "homepage") { id title products(first: 12) { edges { node { id title variants(first: 1) { edges { node { id } } } images(first: 1) { edges { node { altText transformedSrc } } } } } } } } `; const res = await graphQLClient.request(query); if (res.errors) { console.log(JSON.stringify(res.errors, null, 2)); throw Error("Unable to retrieve Shopify Products. Please check logs"); } return { props: { data: { homepageData, collection: res.collectionByHandle, }, }, }; } export default HomePage;
May 20, 2022, 10:32 AM
K
Yes,
homepageQuery, which you reference in
getStaticProps, is not defined. That’s what the error says.
May 20, 2022, 10:38 AM
S
Yes thats true but once defined it throws me a
Error: GraphQL Error (Code: 404): {"response":{"error":"","status":404,"headers":{}},"request":{"query":"\n {\n collectionByHandle(handle: \"homepage\") {\n id\n title\n products(first: 12) {\n edges {\n node {\n id\n title\n variants(first: 1) {\n edges {\n node {\n id\n }\n }\n }\n images(first: 1) {\n edges {\n node {\n altText\n transformedSrc\n }\n }\n }\n }\n }\n }\n }\n }\n "}}
May 20, 2022, 11:05 AM
K
Seems like wherever it's trying to submit it, the endpoint doesn't exist. Perhaps
process.env.NEXT_PUBLIC_SHOPIFY_URLis not defined.
May 20, 2022, 11:08 AM
S
hmm I have the url and the token in a .env.local
May 20, 2022, 11:10 AM
K
Maybe try to console.log them before your query to make sure?
May 20, 2022, 11:11 AM
S
maybe NEXT_PUBLIC_SHOPIFY_URL should be stated differently?
May 20, 2022, 11:15 AM
K
You could also try inlining it for now, just to see if it works.
May 20, 2022, 11:16 AM
S
how do I do that?
May 20, 2022, 11:17 AM
K
Just replace the environment variable with the actual value in your code.
May 20, 2022, 11:19 AM
S
now it throws this SyntaxError: Invalid left-hand side in assignment
May 20, 2022, 11:21 AM
K
I can't debug that without seeing code. I assume you are assigning a variable at the wrong place.
May 20, 2022, 11:22 AM
S
export async function getStaticProps({homepageQuery}) { const homepageData = await getClient().fetch(homepageQuery, {}); const graphQLClient = new GraphQLClient(process.env.NEXT_PUBLIC_SHOPIFY_URL="MY URL HERE", { headers: { "X-Shopify-Storefront-Access-Token": process.env.NEXT_PUBLIC_TOKEN="MY KEY HERE", }, });
May 20, 2022, 11:26 AM
K
Wait, hold on. What I mean is just putting your URL directly in your code. Right now you‘re doing
process.env.*assignment in the middle of your function. It’s just:
GraphQLClient(url, {…})
May 20, 2022, 11:28 AM
S
like so ? but what about the token?
GraphQLClient("<http://example.myshopify.com|example.myshopify.com>", { headers: { "X-Shopify-Storefront-Access-Token": process.env.NEXT_PUBLIC_TOKEN, }, });
May 20, 2022, 11:31 AM
K
Same for now (don’t share it though). We just want to see if the request works at all or if it's a process.env issue.
May 20, 2022, 11:34 AM
S
It's not the real url its just for sharing it with you,however it is giving me a
TypeError: Only absolute URLs are supported
May 20, 2022, 11:37 AM
S
Also the setup for my sanity connection is made in a sanity.server.js file where the token is sitting. Might it be that I should approach shopify the same way as how I did with the sanity token?
May 20, 2022, 11:39 AM
Like this:
change out
const homepageData = await getClient().fetch(homepageQuery, {}); const graphQLClient = new GraphQLClient( '<https://shop.myshopify.com/api/2022-04/graphql.json>', { headers: { 'X-Shopify-Storefront-Access-Token': '123456789asdfghjkl' } } ) };
shopand
123456789asdfghjklfor your own shop name and access token.
May 20, 2022, 11:44 AM
S
Errors are gone now and i see this in the localhost3000:
May 20, 2022, 11:48 AM
S
So It was the url that wasnt right
May 20, 2022, 11:49 AM
S
But when I try to do it like
it breaks again
process.env.NEXT_PUBLIC_SHOPIFY_URL
May 20, 2022, 11:50 AM
S
same problem
May 20, 2022, 11:52 AM
S
NEXT_PUBLIC_SHOPIFY_URL="<https://shop.myshopify.com/api/2022-04/graphql.json>" NEXT_PUBLIC_TOKEN="SECRET"
May 20, 2022, 11:54 AM
S
Error: GraphQL Error (Code: 401): {"response":{"error":"","status":401,"headers":{}},"request":{"query":"\n {\n collectionByHandle(handle: \"homepage\") {\n id\n title\n products(first: 12) {\n edges {\n node {\n id\n title\n variants(first: 1) {\n edges {\n node {\n id\n }\n }\n }\n images(first: 1) {\n edges {\n node {\n altText\n transformedSrc\n }\n }\n }\n }\n }\n }\n }\n }\n "}}
May 20, 2022, 11:55 AM
K
Try removing the quotation marks from the variables in your
.env.
May 20, 2022, 11:56 AM
S
Ah you did it Kitty!
May 20, 2022, 11:56 AM
K
I’ve been bitten that this shit more than once. It’s always a pain to track down as well. 😄
May 20, 2022, 11:57 AM
S
It needed 3 people to look at it hahaha
May 20, 2022, 11:57 AM
S
Thanks guys
May 20, 2022, 11:58 AM
Sanity– build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.