Topics covering webhook creation & management, event handling, Pub/Sub, and Eventbridge, in Shopify apps.
I am using Next JS, GraphQL and Koa to build a connector for Shopify. I want to send additional data to koa and saw includeFields on the shopify API page.
Here is what my webhook creation gql looks like:
const SUBSCRIBE = gql`
mutation webhookSubscriptionCreate {
webhookSubscriptionCreate(
topic: BULK_OPERATIONS_FINISH
webhookSubscription: {
format: JSON,
callbackUrl: "${process.env.KEY}/bulk",
includeFields: ["hello world"]
}
) {
userErrors {
field
message
}
webhookSubscription {
id
}
}
}
`
Notice the field includeFields which I'd like to use to send additional data to koa.
Then I run a bulk operation:
export const PRODUCT_BULK_OPERATION = () => {
return gql`mutation bulkOperationRunQuery {
bulkOperationRunQuery(query: """
{
products(first: 10) {
edges {
cursor
node {
title
id
}
}
}
}
"""
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}`;
};
and here is how I can receive the webhook:
import { receiveWebhook } from '@shopify/koa-shopify-webhooks';
const webhook = receiveWebhook({
secret: process.env.SECRET
})
router.post("/bulk", webhook, async (ctx) => {
try {
// Tried
console.log("ctx.request.body", ctx.request.body)
console.log("ctx.state.webhook", ctx.state.webhook)
await Shopify.Webhooks.Registry.process(ctx.req, ctx.res);
console.log(`Webhook processed, returned status code 200`);
} catch (error) {
console.log(`2) Failed to process webhook: ${error}`);
}
});
The data I get from the terminal:
ctx.request.body {
admin_graphql_api_id: 'gid://shopify/BulkOperation/00000000000',
completed_at: '2021-12-06T22:24:25+04:00',
created_at: '2021-12-06T22:24:24+04:00',
error_code: null,
status: 'completed',
type: 'query'
}
ctx.state.webhook {
topic: 'BULK_OPERATIONS_FINISH',
domain: 'dev-store.myshopify.com',
payload: {
admin_graphql_api_id: 'gid://shopify/BulkOperation/00000000000',
completed_at: '2021-12-06T22:24:25+04:00',
created_at: '2021-12-06T22:24:24+04:00',
error_code: null,
status: 'completed',
type: 'query'
}
}
As you can see, it does not return includeFields in koa. I tried using cookies before to send the same data from Next JS but It didn't work either.
Because bulk operations are only available in GraphQL, I believe this question is more linked to GraphQL. However, if somebody has done this in REST with another webhook subscription topic, please leave a comment.
Any suggestions?