import { useEffect } from "react"; import { useFetcher } from "@remix-run/react"; import { Page, Layout, Text, Card, Button, BlockStack, Box, List, Link, InlineStack, } from "@shopify/polaris"; import { TitleBar, useAppBridge } from "@shopify/app-bridge-react"; import { authenticate } from "../shopify.server"; export const loader = async ({ request }) => { await authenticate.admin(request); return null; }; export const action = async ({ request }) => { const { admin } = await authenticate.admin(request); const color = ["Red", "Orange", "Yellow", "Green"][ Math.floor(Math.random() * 4) ]; const response = await admin.graphql( `#graphql mutation populateProduct($product: ProductCreateInput!) { productCreate(product: $product) { product { id title handle status variants(first: 10) { edges { node { id price barcode createdAt } } } } } }`, { variables: { product: { title: `${color} Snowboard`, }, }, }, ); const responseJson = await response.json(); const product = responseJson.data.productCreate.product; const variantId = product.variants.edges[0].node.id; const variantResponse = await admin.graphql( `#graphql mutation shopifyRemixTemplateUpdateVariant($productId: ID!, $variants: [ProductVariantsBulkInput!]!) { productVariantsBulkUpdate(productId: $productId, variants: $variants) { productVariants { id price barcode createdAt } } }`, { variables: { productId: product.id, variants: [{ id: variantId, price: "100.00" }], }, }, ); const variantResponseJson = await variantResponse.json(); return { product: responseJson.data.productCreate.product, variant: variantResponseJson.data.productVariantsBulkUpdate.productVariants, }; }; export default function Index() { const fetcher = useFetcher(); const shopify = useAppBridge(); const isLoading = ["loading", "submitting"].includes(fetcher.state) && fetcher.formMethod === "POST"; const productId = fetcher.data?.product?.id.replace( "gid://shopify/Product/", "", ); useEffect(() => { if (productId) { shopify.toast.show("Product created"); } }, [productId, shopify]); const generateProduct = () => fetcher.submit({}, { method: "POST" }); return ( Go to Settings Page Congrats on creating a new Shopify app 🎉 This embedded app template uses{" "} App Bridge {" "} interface examples like an{" "} additional page in the app nav , as well as an{" "} Admin GraphQL {" "} mutation demo, to provide a starting point for app development. Get started with products Generate a product with GraphQL and get the JSON output for that product. Learn more about the{" "} productCreate {" "} mutation in our API references. {fetcher.data?.product && ( )} {fetcher.data?.product && ( <> {" "} productCreate mutation
                        
                          {JSON.stringify(fetcher.data.product, null, 2)}
                        
                      
{" "} productVariantsBulkUpdate mutation
                        
                          {JSON.stringify(fetcher.data.variant, null, 2)}
                        
                      
)}
App template specs Framework Remix Database Prisma Interface Polaris {", "} App Bridge API GraphQL API Next steps Build an{" "} {" "} example app {" "} to get started Explore Shopify’s API with{" "} GraphiQL
); }