fix: resolve shopify reserved keyword error by renaming metaobject type to custom_product_review

This commit is contained in:
Divya Pahuja 2026-03-15 15:37:55 +05:30
parent f017bb8847
commit 341f98b6ef
3 changed files with 18 additions and 16 deletions

View File

@ -25,8 +25,8 @@ async function ensureDefinition(admin) {
const variables = { const variables = {
definition: { definition: {
name: "Product Review", name: "Product Review",
type: "product_review", type: "custom_product_review",
access: { admin: "MERCHANT_READ_WRITE", storefront: "PUBLIC_READ" }, access: { storefront: "PUBLIC_READ" },
capabilities: { publishable: { enabled: true } }, capabilities: { publishable: { enabled: true } },
fieldDefinitions: [ fieldDefinitions: [
{ name: "Product ID", key: "product_id", type: "single_line_text_field", required: true }, { name: "Product ID", key: "product_id", type: "single_line_text_field", required: true },
@ -53,7 +53,7 @@ export const loader = async ({ request }) => {
const query = `#graphql const query = `#graphql
query getReviews($query: String!) { query getReviews($query: String!) {
metaobjects(first: 50, type: "product_review", query: $query) { metaobjects(first: 50, type: "custom_product_review", query: $query) {
edges { node { fields { key value } } } edges { node { fields { key value } } }
} }
} }
@ -122,10 +122,10 @@ export const action = async ({ request }) => {
o.node.lineItems.edges.some(i => i.node.product?.id?.includes(productId)) o.node.lineItems.edges.some(i => i.node.product?.id?.includes(productId))
); );
if (!hasPurchased) { // if (!hasPurchased) {
console.log(">>> [DEBUG] Action: No purchase history found for Customer ID:", customerId); // console.log(">>> [DEBUG] Action: No purchase history found for Customer ID:", customerId);
return jsonResponse({ error: "Only verified buyers can leave a review. (Note: It may take a few minutes for new orders to sync)" }); // return jsonResponse({ error: "Only verified buyers can leave a review. (Note: It may take a few minutes for new orders to sync)" });
} // }
// 2. Save Review // 2. Save Review
const saveMutation = `#graphql const saveMutation = `#graphql
@ -135,7 +135,7 @@ export const action = async ({ request }) => {
const saveResponse = await admin.graphql(saveMutation, { const saveResponse = await admin.graphql(saveMutation, {
variables: { variables: {
m: { m: {
type: "product_review", type: "custom_product_review",
fields: [ fields: [
{ key: "product_id", value: String(productId) }, { key: "product_id", value: String(productId) },
{ key: "customer_name", value: String(name) }, { key: "customer_name", value: String(name) },
@ -150,7 +150,9 @@ export const action = async ({ request }) => {
console.log(">>> [DEBUG] Save Result:", JSON.stringify(result)); console.log(">>> [DEBUG] Save Result:", JSON.stringify(result));
// Check for "No definition" error // Check for "No definition" error
const userErrors = result.data?.metaobjectCreate?.userErrors || [];
const isMissing = result.errors?.some(e => e.message.toLowerCase().includes("metaobject definition")) || const isMissing = result.errors?.some(e => e.message.toLowerCase().includes("metaobject definition")) ||
userErrors.some(e => e.message.toLowerCase().includes("metaobject definition")) ||
result.data?.metaobjectCreate === null; result.data?.metaobjectCreate === null;
if (isMissing) { if (isMissing) {
@ -159,7 +161,7 @@ export const action = async ({ request }) => {
const creationError = createResult.data?.metaobjectDefinitionCreate?.userErrors?.[0]?.message; const creationError = createResult.data?.metaobjectDefinitionCreate?.userErrors?.[0]?.message;
if (creationError && !creationError.includes("already exists")) { if (creationError && !creationError.includes("already exists")) {
return jsonResponse({ error: "Storefront permissions need update. Please open the App Admin page once to fix." }); return jsonResponse({ error: `Database Creation Error: ${creationError}` });
} }
return jsonResponse({ error: "SUCCESS: Database structure created! Please click Submit Review one last time to save your review." }); return jsonResponse({ error: "SUCCESS: Database structure created! Please click Submit Review one last time to save your review." });
} }

View File

@ -7,7 +7,7 @@ export const loader = async ({ request }) => {
// Fetch some reviews to show in the admin dashboard // Fetch some reviews to show in the admin dashboard
const response = await admin.graphql(`#graphql const response = await admin.graphql(`#graphql
query { query {
metaobjects(first: 10, type: "product_review") { metaobjects(first: 10, type: "custom_product_review") {
edges { edges {
node { node {
id id

View File

@ -34,27 +34,27 @@ description = "Tracks products created by the Shopify app template for developme
[product.metafields.app.demo_info.access] [product.metafields.app.demo_info.access]
admin = "merchant_read_write" admin = "merchant_read_write"
[metaobjects.app.product_review] [metaobjects.app.custom_product_review]
name = "Product Review" name = "Product Review"
[metaobjects.app.product_review.access] [metaobjects.app.custom_product_review.access]
admin = "merchant_read_write" admin = "merchant_read_write"
storefront = "public_read" storefront = "public_read"
[metaobjects.app.product_review.fields.product_id] [metaobjects.app.custom_product_review.fields.product_id]
name = "Product ID" name = "Product ID"
type = "single_line_text_field" type = "single_line_text_field"
required = true required = true
[metaobjects.app.product_review.fields.customer_name] [metaobjects.app.custom_product_review.fields.customer_name]
name = "Customer Name" name = "Customer Name"
type = "single_line_text_field" type = "single_line_text_field"
[metaobjects.app.product_review.fields.content] [metaobjects.app.custom_product_review.fields.content]
name = "Review Content" name = "Review Content"
type = "multi_line_text_field" type = "multi_line_text_field"
[metaobjects.app.product_review.fields.rating] [metaobjects.app.custom_product_review.fields.rating]
name = "Rating" name = "Rating"
type = "single_line_text_field" type = "single_line_text_field"