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

53 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`
mutation {
createLabel(
input: {
color: "${color}"
name: "${name}"
description: "${description}"
}
) {
... on CreateLabelSuccess {
label {
id
name
color
description
createdAt
}
}
... on CreateLabelError {
errorCodes
}
}
}
`
try {
2022-04-11 18:31:28 +00:00
const data = await gqlFetcher(mutation) as CreateLabelResult
2022-02-22 10:33:39 +00:00
console.log('created label', data)
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
}
}