omnivore/packages/web/lib/networking/mutations/createLabelMutation.ts

52 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-02-22 10:33:39 +00:00
import { gql } from 'graphql-request'
2022-04-11 18:31:28 +00:00
import { Label } from '../fragments/labelFragment'
2022-02-22 10:33:39 +00:00
import { gqlFetcher } from '../networkHelpers'
2022-04-11 18:31:28 +00:00
type CreateLabelResult = {
createLabel: CreateLabel
errorCodes?: unknown[]
}
type CreateLabel = {
label: Label
}
2022-02-22 10:33:39 +00:00
export async function createLabelMutation(
name: string,
color: string,
description?: string
): Promise<any | undefined> {
const mutation = gql`
2023-06-19 08:28:41 +00:00
mutation CreateLabel($input: CreateLabelInput!) {
2023-06-16 04:51:21 +00:00
createLabel(input: $input) {
2022-02-22 10:33:39 +00:00
... on CreateLabelSuccess {
label {
id
name
color
description
createdAt
}
}
... on CreateLabelError {
errorCodes
}
}
}
`
try {
2023-06-16 04:51:21 +00:00
const data = (await gqlFetcher(mutation, {
input: {
name,
color,
description,
},
})) as CreateLabelResult
2022-04-11 18:31:28 +00:00
return data.errorCodes ? undefined : data.createLabel.label
2022-02-22 10:33:39 +00:00
} catch (error) {
console.log('createLabelMutation error', error)
return undefined
}
}