Troubleshooting Shopify integration with Sanity.io

32 replies
Last updated: May 20, 2022
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
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
Yes,
homepageQuery
, which you reference in
getStaticProps
, is not defined. That’s what the error says.
May 20, 2022, 10:38 AM
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
Seems like wherever it's trying to submit it, the endpoint doesn't exist. Perhaps
process.env.NEXT_PUBLIC_SHOPIFY_URL
is not defined.
May 20, 2022, 11:08 AM
hmm I have the url and the token in a .env.local
May 20, 2022, 11:10 AM
Maybe try to console.log them before your query to make sure?
May 20, 2022, 11:11 AM
maybe NEXT_PUBLIC_SHOPIFY_URL should be stated differently?
May 20, 2022, 11:15 AM
You could also try inlining it for now, just to see if it works.
May 20, 2022, 11:16 AM
how do I do that?
May 20, 2022, 11:17 AM
Just replace the environment variable with the actual value in your code.
May 20, 2022, 11:19 AM
now it throws this SyntaxError: Invalid left-hand side in assignment
May 20, 2022, 11:21 AM
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
  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",
      },
    });
I'm placing the url and the token after = inside a ' ' ... that should have done the trick right ?
May 20, 2022, 11:26 AM
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
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
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
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
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:
  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'
      }
    }
  )
};
change out
shop
and
123456789asdfghjkl
for your own shop name and access token.
May 20, 2022, 11:44 AM
Errors are gone now and i see this in the localhost3000:
May 20, 2022, 11:48 AM
So It was the url that wasnt right
May 20, 2022, 11:49 AM
But when I try to do it like
process.env.NEXT_PUBLIC_SHOPIFY_URL
it breaks again
May 20, 2022, 11:50 AM
Try restarting next.js if you haven’t already.
May 20, 2022, 11:51 AM
same problem
May 20, 2022, 11:52 AM
Can you copy paste that line from your env file into this thread?
May 20, 2022, 11:53 AM
NEXT_PUBLIC_SHOPIFY_URL="<https://shop.myshopify.com/api/2022-04/graphql.json>"
NEXT_PUBLIC_TOKEN="SECRET"
May 20, 2022, 11:54 AM
it breaks again
what’s the error?
May 20, 2022, 11:55 AM
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
Try removing the quotation marks from the variables in your
.env
.
May 20, 2022, 11:56 AM
Ah you did it Kitty!
May 20, 2022, 11:56 AM
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
It needed 3 people to look at it hahaha
May 20, 2022, 11:57 AM
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.

Was this answer helpful?