Shopify / shopify-app-template-node

MIT License
867 stars 391 forks source link

How can I do a Post? I cannot get the accessToken. #1272

Open joelveloso opened 1 year ago

joelveloso commented 1 year ago

Issue summary

ORDERS_UPDATED: { deliveryMethod: DeliveryMethod.Http, callbackUrl: "/api/webhooks", callback: async (topic, shop, body, webhookId, apiVersion) => { console.log("Received ORDERS_UPDATED webhook"); try { let session = await shopify.sessionStorage.loadSession(shop); if (!session) { console.error(Failed to load session for shop: ${shop}`); return; } } catch (error) { console.error("Error loading session:", error); }

  let accessToken = session.accessToken;

  const payload = JSON.parse(body);

  if (payload.closed_at === null) {
    console.log("Order is not closed. Triggering flow...");
    const url = `https://${shop}/admin/api/${apiVersion}/graphql.json`;
    // console.log(payload);
    const triggerFlow = async (id) => {
      console.log("Inside triggerFlow function. Order ID:", id);

      const query = `
        mutation {
          flowTriggerReceive(
            body: "{
              \\"trigger_id\\": \\"##########################\\",
              \\"properties\\": {
                \\"order_id\\": ${id}
              }
            }"
          ) {
            userErrors {field, message}
          }
        }
      `;

      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/graphql",
          "X-Shopify-Access-Token": accessToken,
        },
        body: JSON.stringify({ query }),
      });

      const responseData = await response.json();
      console.log(responseData);
    };

    // Trigger the Shopify Flow
    triggerFlow(payload.id)
      .then(() => {
        console.log("Flow triggered successfully");
      })
      .catch((error) => {
        console.error("Error triggering flow:", error);
      });
  } else {
    console.log("Order is closed. Not triggering flow.");
  }
},

}, }; `

This part:

let session = await shopify.sessionStorage.loadSession(shop);

Does not work. I am using your template. From the readme.MD it reads:

The Node app template comes with the following out-of-the-box functionality:

OAuth: Installing the app and granting permissions

How can I get the, or if someone can point me in the right direction

"X-Shopify-Access-Token": accessToken,

Kind Regards,