How to get the globally unique identifier of a Shopify store using GraphQL
How to get the globally unique identifier of a Shopify store using GraphQL
I am trying to get the shopID using a GraphQL. I can get the value using a GraphQL app but when I run the query through a function I get null.
export async function getShopId(admin) {
// Your GraphQL query to get the shop ID
const GET_SHOP_ID = `
{
shop {
id
}
}
`;
try {
// Execute the GraphQL query using the admin object
const { data, errors } = await admin(GET_SHOP_ID);
if (errors) {
// Handle errors if the query fails
console.error('Error fetching shop ID:', errors);
return null;
}
// Access the shop ID from the query response
const shopId = data?.shop?.id;
console.log(data);
if (!shopId) {
// Handle the case where shop ID is not available
console.error('Shop ID not found in the GraphQL response.');
return null;
}
return shopId;
} catch (error) {
console.error('Error in getShopId:', error);
return null;
}
}
I am calling this function through a loader so the admin and the graphql should exist but I keep getting null. Here is the loader
export async function loader({ request, params }) {
const { admin, session } = await authenticate.admin(request);
const { shop } = session;
const graphql = admin.graphql;
// Example: Fetching shop ID using getShopId
const shopIdm = await getShopId(graphql);
console.log("shopId-->" + shopIdm);
return null
}
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand