fetchCategories.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import fs from 'fs'
  2. import path from 'path'
  3. import { ApolloClient, InMemoryCache, HttpLink, gql } from '@apollo/client'
  4. import fetch from 'cross-fetch'
  5. type categoryType = {
  6. id: string
  7. name: string
  8. createdInBlock: number
  9. createdAt: Date
  10. updatedAt: Date
  11. }
  12. async function main() {
  13. const env = process.env
  14. const queryNodeUrl: string = env.QUERY_NODE_URL || 'http://127.0.0.1:8081/graphql'
  15. console.log(`Connecting to Query Node at: ${queryNodeUrl}`)
  16. const queryNodeProvider = new ApolloClient({
  17. link: new HttpLink({ uri: queryNodeUrl, fetch }),
  18. cache: new InMemoryCache(),
  19. })
  20. const videoCategories = await getCategories(queryNodeProvider, 'videoCategories')
  21. const channelCategories = await getCategories(queryNodeProvider, 'channelCategories')
  22. fs.writeFileSync(
  23. path.resolve(__dirname, '../data/videoCategories.json'),
  24. JSON.stringify(videoCategories, undefined, 4)
  25. )
  26. fs.writeFileSync(
  27. path.resolve(__dirname, '../data/channelCategories.json'),
  28. JSON.stringify(channelCategories, undefined, 4)
  29. )
  30. console.log(`${videoCategories.length} video categories exported & saved!`)
  31. console.log(`${channelCategories.length} channel categories exported & saved!`)
  32. }
  33. async function getCategories(queryNodeProvider, categoryType): Promise<Array<categoryType>> {
  34. const GET_ALL_CATEGORY_ITEMS = gql`
  35. query {
  36. ${categoryType} {
  37. id
  38. name
  39. createdInBlock
  40. createdAt
  41. updatedAt
  42. }
  43. }
  44. `
  45. const queryResult = await queryNodeProvider.query({ query: GET_ALL_CATEGORY_ITEMS })
  46. const categories = queryResult.data[categoryType].map(({ id, name, createdInBlock, createdAt, updatedAt }) => {
  47. return {
  48. id,
  49. name,
  50. createdInBlock,
  51. createdAt,
  52. updatedAt,
  53. }
  54. })
  55. return categories
  56. }
  57. main()
  58. .then(() => process.exit())
  59. .catch(console.error)