How to I get the shop id without having to do a query or api request ?
thangpqgm opened this issue ยท 5 comments
Currently, I can get the shop domain in the session but I can't get the shop id.
I have to do a query into the session table to get the shop id, which is really annoying,
const { session } = await authenticate.admin(request);
const shopData = await db.shop.findFirst({
where: { myshopifyDomain: session.shop },
});
console.log(shopData.shopifyShopId)
Getting the shop ID by calling rest API or graph ql also has the same cost.
I thought about saving the shop ID in a cookie but it seems impossible because the admin app is wrapped in an iframe tag,
So is there a simpler way for me to get the shop id in the loader?
You can get it from session:
const shopDomain = session.shop;
Edit: I did not read well, the "id" is not possible to get without query, there are other disscusions about the topic
Hi @thangpqgm, thank you for opening this issue. I'm going to put it in front of the team for discussion.
I want to know if it's safe to use this "shop" field as a store identifier, for scenarios like storing whether a shop is VIP or how many credits it has. Otherwise, wouldn't we need to retrieve a shopId again in each loader?
Hey everyone ๐
We chatted about this issue as a team but it was not clear to use exactly what the use case is. We understand you want the ID of the shop, but why? If you can frame the requirement in terms of a user flow or a business casse that would really help us decided how to address this.
Judging by the recently released subscriptions reference app, it seems like it's safe to use session.shop
as the identifier in your DB (i.e. tenant id): https://github.com/Shopify/subscriptions-reference-app/blob/main/prisma/schema.prisma
The prisma schema contains shop
and is queried simply by always including shop from session.shop
:
const dunningTracker = await prisma.dunningTracker.findFirst({
where: {
shop,
contractId: subscriptionContract.id,
billingCycleIndex,
},
});