Files
omnivore/packages/web/lib/networking/mutations/setWebhookMutation.ts
2022-06-02 12:32:13 +08:00

51 lines
1.1 KiB
TypeScript

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