From aada005e50dc4d88064516921bb6cc1dcff04238 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 6 Jun 2022 12:53:25 +0800 Subject: [PATCH] Add revoke api key mutation in Web --- .../mutations/revokeApiKeyMutation.ts | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 packages/web/lib/networking/mutations/revokeApiKeyMutation.ts diff --git a/packages/web/lib/networking/mutations/revokeApiKeyMutation.ts b/packages/web/lib/networking/mutations/revokeApiKeyMutation.ts new file mode 100644 index 000000000..83f9447f5 --- /dev/null +++ b/packages/web/lib/networking/mutations/revokeApiKeyMutation.ts @@ -0,0 +1,39 @@ +import { gql } from 'graphql-request' +import { gqlFetcher } from '../networkHelpers' +import { ApiKey } from '../queries/useGetApiKeysQuery' + +interface RevokeApiKeyResult { + revokeApiKey: RevokeApiKey + errorCodes?: unknown[] +} + +type RevokeApiKey = { + apiKey: ApiKey +} + +export async function revokeApiKeyMutation( + id: string +): Promise { + const mutation = gql` + mutation { + revokeApiKey(id: "${id}") { + ... on RevokeApiKeySuccess { + apiKey { + id + } + } + ... on RevokeApiKeyError { + errorCodes + } + } + } + ` + + try { + const data = (await gqlFetcher(mutation)) as RevokeApiKeyResult + return data.errorCodes ? undefined : data.revokeApiKey.apiKey.id + } catch (error) { + console.log('revokeApiKeyMutation error', error) + return undefined + } +}