How can I correctly use GraphQL in a webhook?

robtarr
Shopify Partner
3 0 0

I'm adding a webhook, and in that webhook I want to make a graphql call to modify an order.


export const action = async ({ request }) => {
const { payload, topic, shop, session, admin } = await authenticate.webhook(request);

switch (topic) {
...
case "CHECKOUTS_CREATE":
if (admin) {
admin.graphql.query(`query here...`)
}
...

 


I assumed that I could use the graphql client here, but I get a TS error `Argument of type 'string' is not assignable to parameter of type 'GraphqlParams'.`


What is the right way to do this?

Replies 5 (5)

robtarr
Shopify Partner
3 0 0

OK, so for some reason the `admin.graphql` that comes back from `authenticate.webhook` is different than what comes back from `authenticate.admin`. If I pass `{data: "query here..."}` this works. Now I just need to craft the right query.

Dixor
Shopify Partner
30 3 11

Damn, thanks man. I really hate Shopify's docs.

inside yourAppName/app/routes/webhooks.jsx

 

 

 

 

 

case "CHECKOUTS_CREATE":
  if (session) {
    const response = await admin.graphql.query({data: `
      query {
        shop {
        name
      }
    }`
  });
}
break;

 

 

 

 

theaisyaaziz
Shopify Partner
14 1 6

I'm getting the same issue. Would you mind sharing the docs you refer to / what was your solution?

 

Thanks!

theaisyaaziz
Shopify Partner
14 1 6

if anyone is facing the same issue, here's what works for me:

isHook - true if it's from webhook's admin, false if it's from the web admin.

const response = this.isHook
? await this.graphql.query({ data: { query, variables } })
: await this.graphql(query, { variables });
vvast-paul
Shopify Partner
9 1 3

I have spent so long trying to figure out how to make admin graphQL calls from a webhook, and your answer solved it. Sincerely, thank you so much for sharing.

 

PS: Why there doesn't seem to be anything about this in the docs is beyond me!

Paul Simmonds
Acting Platform Developer
paul.simmonds@vvast.net
0117 214 1569

1st Floor, The Landing, 125 Redcliff Street, Bristol, BS1 6HU