From 1b024214fefaa6d02b96dcaee76044f0356fedef Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 2 Jun 2022 11:02:57 +0800 Subject: [PATCH] Add get webhook query in web --- .../networking/queries/useGetWebhookQuery.tsx | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 packages/web/lib/networking/queries/useGetWebhookQuery.tsx diff --git a/packages/web/lib/networking/queries/useGetWebhookQuery.tsx b/packages/web/lib/networking/queries/useGetWebhookQuery.tsx new file mode 100644 index 000000000..e5e05a6d2 --- /dev/null +++ b/packages/web/lib/networking/queries/useGetWebhookQuery.tsx @@ -0,0 +1,62 @@ +import { gql } from 'graphql-request' +import useSWR from 'swr' +import { makeGqlFetcher } from '../networkHelpers' +import { Webhook } from './useGetWebhooksQuery' + +interface WebhookQueryResponse { + isValidating?: boolean + webhook?: Webhook + revalidate?: () => void +} + +interface WebhookQueryResponseData { + webhook: WebhookData +} + +interface WebhookData { + webhook: unknown +} + +export function useGetWebhookQuery(id: string): WebhookQueryResponse { + const query = gql` + query GetWebhook($id: ID!) { + webhook(id: $id) { + ... on WebhookSuccess { + webhook { + id + url + eventTypes + contentType + method + enabled + createdAt + updatedAt + } + } + ... on WebhookError { + errorCodes + } + } + } + ` + + const { data, mutate, isValidating } = useSWR(query, makeGqlFetcher({ id })) + console.log('webhook data', data) + + try { + if (data) { + const result = data as WebhookQueryResponseData + const webhook = result.webhook.webhook as Webhook + return { + isValidating, + webhook, + revalidate: () => { + mutate() + }, + } + } + } catch (error) { + console.log('error', error) + } + return {} +}