omnivore/packages/web/lib/networking/mutations/setWebhookMutation.ts

51 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-06-01 13:08:13 +00:00
import { gql } from 'graphql-request'
import { gqlFetcher } from '../networkHelpers'
import { Webhook, WebhookEvent } from '../queries/useGetWebhooksQuery'
2022-06-02 04:32:13 +00:00
export interface SetWebhookInput {
contentType?: string[]
enabled?: boolean
eventTypes: WebhookEvent[]
id?: string
method?: string
url: string
}
2022-06-01 13:08:13 +00:00
interface SetWebhookResult {
setWebhook: SetWebhook
errorCodes?: unknown[]
}
type SetWebhook = {
webhook: Webhook
}
export async function setWebhookMutation(
2022-06-02 04:32:13 +00:00
input: SetWebhookInput
): Promise<string | undefined> {
2022-06-01 13:08:13 +00:00
const mutation = gql`
mutation SetWebhook($input: SetWebhookInput!) {
setWebhook(input: $input) {
... on SetWebhookSuccess {
webhook {
id
}
}
... on SetWebhookError {
errorCodes
}
}
}
`
try {
const data = (await gqlFetcher(mutation, {
2022-06-02 04:32:13 +00:00
input,
2022-06-01 13:08:13 +00:00
})) as SetWebhookResult
return data.errorCodes ? undefined : data.setWebhook.webhook.id
} catch (error) {
console.log('setWebhookMutation error', error)
return undefined
}
}