import { createGraphQLClient } from "@saleor/apps-shared"; import { Client, gql } from "urql"; import { CreateAppWebhookDocument, CreateAppWebhookMutation, CreateAppWebhookMutationVariables, DeleteAppWebhookDocument, DeleteAppWebhookMutation, DeleteAppWebhookMutationVariables, DisableWebhookDocument, DisableWebhookMutation, DisableWebhookMutationVariables, EnableWebhookDocument, EnableWebhookMutation, EnableWebhookMutationVariables, FetchAppWebhooksDocument, FetchAppWebhooksQuery, } from "../../generated/graphql"; gql` query FetchAppWebhooks { app { webhooks { id name } } } `; gql` mutation CreateAppWebhook( $appId: ID! $name: String! $targetUrl: String! $query: String $isActive: Boolean! $asyncEvents: [WebhookEventTypeAsyncEnum!] $syncEvents: [WebhookEventTypeSyncEnum!] ) { webhookCreate( input: { app: $appId name: $name targetUrl: $targetUrl query: $query isActive: $isActive asyncEvents: $asyncEvents syncEvents: $syncEvents } ) { webhook { id } } } `; gql` mutation DeleteAppWebhook($id: ID!) { webhookDelete(id: $id) { webhook { id } } } `; gql` mutation DisableWebhook($id: ID!) { webhookUpdate(id: $id, input: { isActive: false }) { webhook { id } } } `; gql` mutation EnableWebhook($id: ID!) { webhookUpdate(id: $id, input: { isActive: true }) { webhook { id } } } `; export class AppWebhookRepository { constructor(private client: Client) {} async getAll() { const { error, data } = await this.client .query(FetchAppWebhooksDocument, {}) .toPromise(); if (error) { console.log("Was not able to fetch app webhooks", error.message); throw error; } return data?.app?.webhooks ?? []; } async create(variables: CreateAppWebhookMutationVariables) { const { error, data } = await this.client .mutation(CreateAppWebhookDocument, variables) .toPromise(); if (error) { console.log(`Was not able to create webhook for the app ${variables.appId}`, error.message); throw error; } return data?.webhookCreate?.webhook?.id; } async disable(id: string) { const { error, data } = await this.client .mutation(DisableWebhookDocument, { id, } as DisableWebhookMutationVariables) .toPromise(); if (error) { console.log(`Was not able to disable webhook ${id}`, error.message); throw error; } return data?.webhookUpdate?.webhook?.id; } async enable(id: string) { const { error, data } = await this.client .mutation(EnableWebhookDocument, { id, } as EnableWebhookMutationVariables) .toPromise(); if (error) { console.log(`Was not able to enable webhook ${id}`, error.message); throw error; } return data?.webhookUpdate?.webhook?.id; } async delete(id: string) { const { error, data } = await this.client .mutation(DeleteAppWebhookDocument, { id, } as DeleteAppWebhookMutationVariables) .toPromise(); console.log(data, error); if (error) { console.log(`Was not able to delete webhook ${id}`, error.message); throw error; } return data?.webhookDelete?.webhook?.id; } }