import useSWR, { SWRResponse, SWRConfiguration } from 'swr' import { request } from '@/lib/tabby-gql-client' import { Variables } from 'graphql-request' import { TypedDocumentNode } from '@graphql-typed-document-node/core' import { ASTNode, Kind, OperationDefinitionNode } from 'graphql' const isOperationDefinition = (def: ASTNode): def is OperationDefinitionNode => def.kind === Kind.OPERATION_DEFINITION function useGraphQL( document: TypedDocumentNode, variables?: TVariables, options?: SWRConfiguration ): SWRResponse { return useSWR( [ document.definitions.find(isOperationDefinition)?.name?.value, document, variables ], ([_key, document, variables]) => { return request({ document, variables }) }, options ) } export { useGraphQL }