I am creating a student portal for the students who pay for a monthly subscription through shopify as a react vite app. I have metaobjects for lessons, assignments and assessments, whose information will be shown in a calendar in the portal using react big calendar. I was able to get data about my products using the admin rest api and it was shown in json format at http://localhost:5000/api/products
. However, when I tried to do the same using graphql this time, it gave me this error in the terminal:
Error response from Shopify: {"errors":"Not Found"}
Error fetching metaobjects: Error: Network response was not ok
at file:///.../backend/server.js:78:10
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
I am new to graphql so debugging this issue has been very difficult and I haven’t been able to find a solution after tweaking the code multiple times.
I have a .env file in the same directory which stores the SHOPIFY_STORE_URL
and SHOPIFY_API_ACCESS_TOKEN
.
How do I fix this?
server.js
import express from "express";
import cors from "cors";
import fetch from "node-fetch";
import dotenv from "dotenv";
dotenv.config();
const app = express();
const PORT = process.env.PORT || 5000;
app.use(cors());
app.use(express.json()); // For parsing application/json
// REST API route for products
app.get("/api/products", async (req, res) => {
try {
const response = await fetch(
`https://${process.env.SHOPIFY_STORE_URL}/admin/api/2024-07/products.json`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
"X-Shopify-Access-Token":
process.env.SHOPIFY_API_ACCESS_TOKEN,
},
},
);
if (!response.ok) {
const errorText = await response.text();
console.error("Error response from Shopify:", errorText);
throw new Error("Network response was not ok");
}
const data = await response.json();
res.json(data.products);
} catch (error) {
console.error("Error fetching products:", error);
res.status(500).send("Error fetching products");
}
});
// GraphQL API route for metaobjects (using POST)
app.get("/api/metaobjects", async (req, res) => {
const query = `
{
metaobjects(type: "lessons", first: 10) {
edges {
node {
id
fields {
key
value
}
}
}
}
}
`;
try {
const response = await fetch(
`https://${process.env.SHOPIFY_STORE_URL}/admin/api/2024-07/graphql.json`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Shopify-Access-Token":
process.env.SHOPIFY_API_ACCESS_TOKEN,
},
body: JSON.stringify({ query }),
},
);
if (!response.ok) {
const errorText = await response.text();
console.error("Error response from Shopify:", errorText);
throw new Error("Network response was not ok");
}
const data = await response.json();
if (data.errors) {
console.error("GraphQL errors:", data.errors);
throw new Error('GraphQL errors');
}
return data.data.metaobjects.edges.map(edge => edge.node);
} catch (error) {
console.error("Error fetching metaobjects:", error);
res.status(500).send("Error fetching metaobjects");
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});