api.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { ApolloClient, NormalizedCacheObject, HttpLink, InMemoryCache, DocumentNode } from '@apollo/client'
  2. import fetch from 'cross-fetch'
  3. import {
  4. DataObjectDetailsFragment,
  5. GetDataObjectDetails,
  6. GetDataObjectDetailsQuery,
  7. GetDataObjectDetailsQueryVariables,
  8. DistirubtionBucketsWithObjectsFragment,
  9. GetDistributionBucketsWithObjectsQuery,
  10. GetDistributionBucketsWithObjectsQueryVariables,
  11. GetDistributionBucketsWithObjects,
  12. } from './generated/queries'
  13. import { Maybe } from './generated/schema'
  14. export class QueryNodeApi {
  15. private apolloClient: ApolloClient<NormalizedCacheObject>
  16. public constructor(endpoint: string) {
  17. this.apolloClient = new ApolloClient({
  18. link: new HttpLink({ uri: endpoint, fetch }),
  19. cache: new InMemoryCache(),
  20. defaultOptions: { query: { fetchPolicy: 'no-cache', errorPolicy: 'all' } },
  21. })
  22. }
  23. // Get entity by unique input
  24. protected async uniqueEntityQuery<
  25. QueryT extends { [k: string]: Maybe<Record<string, unknown>> | undefined },
  26. VariablesT extends Record<string, unknown>
  27. >(
  28. query: DocumentNode,
  29. variables: VariablesT,
  30. resultKey: keyof QueryT
  31. ): Promise<Required<QueryT>[keyof QueryT] | null> {
  32. return (await this.apolloClient.query<QueryT, VariablesT>({ query, variables })).data[resultKey] || null
  33. }
  34. // Get entities by "non-unique" input and return first result
  35. protected async firstEntityQuery<
  36. QueryT extends { [k: string]: unknown[] },
  37. VariablesT extends Record<string, unknown>
  38. >(query: DocumentNode, variables: VariablesT, resultKey: keyof QueryT): Promise<QueryT[keyof QueryT][number] | null> {
  39. return (await this.apolloClient.query<QueryT, VariablesT>({ query, variables })).data[resultKey][0] || null
  40. }
  41. // Query-node: get multiple entities
  42. protected async multipleEntitiesQuery<
  43. QueryT extends { [k: string]: unknown[] },
  44. VariablesT extends Record<string, unknown>
  45. >(query: DocumentNode, variables: VariablesT, resultKey: keyof QueryT): Promise<QueryT[keyof QueryT]> {
  46. return (await this.apolloClient.query<QueryT, VariablesT>({ query, variables })).data[resultKey]
  47. }
  48. public getDataObjectDetails(objectId: string): Promise<DataObjectDetailsFragment | null> {
  49. return this.uniqueEntityQuery<GetDataObjectDetailsQuery, GetDataObjectDetailsQueryVariables>(
  50. GetDataObjectDetails,
  51. { id: objectId },
  52. 'storageDataObjectByUniqueInput'
  53. )
  54. }
  55. public getDistributionBucketsWithObjects(ids: string[]): Promise<DistirubtionBucketsWithObjectsFragment[]> {
  56. return this.multipleEntitiesQuery<
  57. GetDistributionBucketsWithObjectsQuery,
  58. GetDistributionBucketsWithObjectsQueryVariables
  59. >(GetDistributionBucketsWithObjects, { ids }, 'distributionBuckets')
  60. }
  61. }