kinngh / shopify-node-express-mongodb-app

An embedded Shopify app starter template made with Node, Express, React and Vite, with all the required stuff hooked up.
https://x.com/kinngh
MIT License
290 stars 97 forks source link

How to work with graphql using webhook. #69

Closed kmajay978 closed 1 year ago

kmajay978 commented 1 year ago

I am working with order create and order update webhook. I wanted to add the tags to the order but i am not able to do it. Kindly let me know how to work with Graphql in webhook.

Query:- How can i pass the req, res in clientProvider.graphqlClient using webhook file. (orders_create.js)

Here is the code , what i am trying to do.

import clientProvider from "../../utils/clientProvider.js";

Code

 //  graphql operations
    const { client } = await clientProvider.graphqlClient({
      req,
      res,
      isOnline: false,
    });

    const response =  await client.query({
      data: `mutation tagsAdd {
        tagsAdd(id: "gid://shopify/Order/${orderId}", tags: "Preorder") {
          node {
            id
          }
          userErrors {
            field
            message
          }
        }
      }
      `,
    });
kinngh commented 1 year ago

You can't use clientProvider in webhooks. shop is already available in webhook function params, use that to load session ID and then sessionHandler.loadSession() to load a session and pass it to shopify to create a new GraphQL client

kmajay978 commented 1 year ago

Ok got it, but I do have one doubt, from where to load session Id using shop? From Database?

Kindly provide the code sample if possible, I am trying to work with shopify webhook and client from 2 days, but no luck. Help would be appreciated.

Thanks

kinngh commented 1 year ago

Read the documentation: https://github.com/Shopify/shopify-api-js#guides

kmajay978 commented 1 year ago

Hi @kinngh

I tried with the documentation, and here is my code, but still things not working and i am getting accessToken undefined issue.

Here is my approach, let me know what i am missing here.

 const sessionResult = await SessionModel.findOne({shop});  //offline session 

    const session = await sessionHandler.loadSession(sessionResult.id);

    console.log(session,'newSession.......');
   const client = new shopify.clients.Graphql({ session });

   const work = await client.query({ data: TEST_QUERY });

    console.log(work,'work............');

Error i am getting

[server] TypeError: Cannot read properties of undefined (reading 'accessToken')
[server]     at new GraphqlClient (E:\Shopify app\mypre\node_modules\@shopify\shopify-api\lib\clients\graphql\graphql_client.js:14:57)
kmajay978 commented 1 year ago

Hello @kinngh , after researching a lot, i got to know that issue was related to package, it seems it doesn't support properly. Now i sifted on official package and it's working fine with official package. Thanks anyway.

kinngh commented 1 year ago

@kmajay978 just a heads up, there's an update to clientProvider so you can create a new client with clientProvider.offline method