Fix Monday.com webhook subscription and improve API client

- Add API-Version header to all Monday.com GraphQL requests
- Switch create_webhook/delete_webhook to inline values matching Monday's
  documented syntax (fixes Internal Server Error on subscribe)
- Add extended error details from Monday.com GraphQL responses

Made-with: Cursor
This commit is contained in:
Randall Stillwell 2026-04-07 13:07:46 -05:00
parent d58285785e
commit f87ab36493

View file

@ -9,6 +9,7 @@ async function gql(token: string, query: string, variables?: Record<string, unkn
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: token, Authorization: token,
"API-Version": "2024-10",
}, },
body: JSON.stringify({ query, variables }), body: JSON.stringify({ query, variables }),
}); });
@ -18,7 +19,12 @@ async function gql(token: string, query: string, variables?: Record<string, unkn
} }
const json = await res.json(); const json = await res.json();
if (json.errors?.length) { if (json.errors?.length) {
throw new Error(`Monday.com GraphQL: ${json.errors[0].message}`); const err = json.errors[0];
const detail = err.extensions ? ` (${JSON.stringify(err.extensions)})` : "";
throw new Error(`Monday.com GraphQL: ${err.message}${detail}`);
}
if (json.error_message) {
throw new Error(`Monday.com: ${json.error_message}`);
} }
return json.data; return json.data;
} }
@ -169,11 +175,15 @@ export async function createWebhookSubscription(
boardId: string, boardId: string,
callbackUrl: string callbackUrl: string
): Promise<string> { ): Promise<string> {
const escapedUrl = callbackUrl.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
const data = await gql(token, ` const data = await gql(token, `
mutation ($boardId: ID!, $url: String!) { mutation {
create_webhook(board_id: $boardId, url: $url, event: change_column_value) { id } create_webhook(board_id: ${boardId}, url: "${escapedUrl}", event: change_column_value) {
id
board_id
} }
`, { boardId, url: callbackUrl }); }
`);
return String(data.create_webhook.id); return String(data.create_webhook.id);
} }
@ -182,8 +192,10 @@ export async function deleteWebhookSubscription(
webhookId: string webhookId: string
): Promise<void> { ): Promise<void> {
await gql(token, ` await gql(token, `
mutation ($webhookId: ID!) { mutation {
delete_webhook(id: $webhookId) { id } delete_webhook(id: ${webhookId}) {
id
} }
`, { webhookId }); }
`);
} }