GQL for the integrations APIs

This commit is contained in:
Jackson Harper 2022-10-23 07:54:10 +08:00
parent 3b7ad0fe19
commit 349b24947b
2 changed files with 141 additions and 0 deletions

View file

@ -0,0 +1,65 @@
import { gql } from 'graphql-request'
import { gqlFetcher } from '../networkHelpers'
type IntegrationType = 'readwise'
export type SetIntegrationInput = {
id?: string
type: IntegrationType
token: string,
enabled: boolean
}
type SetIntegrationResult = {
setIntegration?: SetIntegrationSuccess
errorCodes?: unknown[]
}
type SetIntegrationSuccess = {
integration: Integration
}
type Integration = {
id: string
type: IntegrationType
token: string
enabled: boolean
createdAt: Date
updatedAt: Date
}
export async function setIntegrationMutation(
input: SetIntegrationInput
): Promise<string | undefined> {
const mutation = gql`
mutation SetIntegration(
$input: SetIntegrationInput!
) {
setIntegration(input: $input) {
... on SetIntegrationSuccess {
integration {
id
type
token
enabled
createdAt
updatedAt
}
}
... on SetIntegrationError {
errorCodes
}
}
}
`
try {
const data = await gqlFetcher(mutation) as SetIntegrationResult
console.log(input, data);
const output = data as any
console.log(output)
return output?.updatedLabel
} catch (err) {
return undefined
}
}

View file

@ -0,0 +1,76 @@
import { gql } from 'graphql-request'
import useSWR from 'swr'
import { publicGqlFetcher } from '../networkHelpers'
export interface Integration {
id: String
type: IntegrationType
token: String
enabled: Boolean
createdAt: Date
updatedAt: Date
}
export type IntegrationType =
| 'READWISE'
interface IntegrationsQueryResponse {
isValidating: boolean
integrations: Integration[]
revalidate: () => void
}
interface IntegrationsQueryResponseData {
integrations: IntegrationsData
}
interface IntegrationsData {
integrations: unknown
}
export function useGetIntegrationsQuery(): IntegrationsQueryResponse {
const query = gql`
query GetIntegrations {
integrations {
... on IntegrationsSuccess {
integrations {
id
type
token
enabled
createdAt
updatedAt
}
}
... on IntegrationsError {
errorCodes
}
}
}
`
const { data, mutate, error, isValidating } = useSWR(query, publicGqlFetcher)
console.log('integrations data', data)
try {
if (data) {
const result = data as IntegrationsQueryResponseData
const integrations = result.integrations.integrations as Integration[]
return {
isValidating,
integrations,
revalidate: () => {
mutate()
},
}
}
} catch (error) {
console.log('error', error)
}
return {
isValidating: false,
integrations: [],
// eslint-disable-next-line @typescript-eslint/no-empty-function
revalidate: () => {},
}
}