Add get webhooks query

This commit is contained in:
Hongbo Wu 2022-06-01 18:43:43 +08:00
parent 2094ffe6b2
commit 545d944ffd
2 changed files with 124 additions and 0 deletions

View file

@ -0,0 +1,39 @@
import { gql } from 'graphql-request'
import { gqlFetcher } from '../networkHelpers'
import { Webhook } from '../queries/useGetWebhooksQuery'
interface DeleteWebhookResult {
deleteWebhook: DeleteWebhook
errorCodes?: unknown[]
}
type DeleteWebhook = {
webhook: Webhook
}
export async function deleteWebhookMutation(
id: string
): Promise<any | undefined> {
const mutation = gql`
mutation {
deleteWebhook(id: "${id}") {
... on DeleteWebhookSuccess {
webhook {
id
}
}
... on DeleteWebhookError {
errorCodes
}
}
}
`
try {
const data = (await gqlFetcher(mutation)) as DeleteWebhookResult
return data.errorCodes ? undefined : data.deleteWebhook.webhook.id
} catch (error) {
console.log('deleteWebhookMutation error', error)
return undefined
}
}

View file

@ -0,0 +1,85 @@
import { gql } from 'graphql-request'
import useSWR from 'swr'
import { publicGqlFetcher } from '../networkHelpers'
export type WebhookEvent =
| 'PAGE_CREATED'
| 'PAGE_UPDATED'
| 'PAGE_DELETED'
| 'HIGHLIGHT_CREATED'
| 'HIGHLIGHT_UPDATED'
| 'HIGHLIGHT_DELETED'
export interface Webhook {
id: string
url: string
eventTypes: WebhookEvent[]
contentType: string
method: string
enabled: boolean
createdAt: Date
updatedAt: Date
}
interface WebhooksQueryResponse {
isValidating: boolean
webhooks: Webhook[]
revalidate: () => void
}
interface WebhooksQueryResponseData {
webhooks: WebhooksData
}
interface WebhooksData {
webhooks: unknown
}
export function useGetWebhooksQuery(): WebhooksQueryResponse {
const query = gql`
query GetWebhooks {
webhooks {
... on WebhooksSuccess {
webhooks {
id
url
eventTypes
contentType
method
enabled
createdAt
updatedAt
}
}
... on WebhooksError {
errorCodes
}
}
}
`
const { data, mutate, error, isValidating } = useSWR(query, publicGqlFetcher)
console.log('webhooks data', data)
try {
if (data) {
const result = data as WebhooksQueryResponseData
const webhooks = result.webhooks.webhooks as Webhook[]
return {
isValidating,
webhooks,
revalidate: () => {
mutate()
},
}
}
} catch (error) {
console.log('error', error)
}
return {
isValidating: false,
webhooks: [],
// eslint-disable-next-line @typescript-eslint/no-empty-function
revalidate: () => {},
}
}