cugetreg icon indicating copy to clipboard operation
cugetreg copied to clipboard

[BACKLOG] Move all multi-project types and schema into one single lib

Open bombnp opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe. Both cugetreg-web and cugetreg-api have separate type definitions for GraphQL types (cugetreg-api= auto-generated from .graphql files, cugetreg-web= manual declaration). This mean changes to cugetreg-api'schema can break without cugetreg-web realizing it. cugetreg-api and cu-scraper both connects to MongoDB to access course data. The mongo schema is currently separated as well, and we need to change both of them when we make changes to the database schema.

Describe the solution you'd like

  1. Create a lib in monorepo as schema to act as a single source of type declaration between multiple projects (mostly frontend and backend). For GraphQL types, utilize auto-generated types from .graphql files for both projects. Note: this will require heavy refactoring on the frontend code.
  • MongoDB schemas: used by cugetreg-api and cu-scraper
  • GraphQL types: used by cugetreg-api and cugetreg-web
  1. Relocate mongo schema in cugetreg-api to this project, to share with cu-scraper (after we migrate scraper to this repo)

Additional context

Don't worry about cu-scraper for now as it may be deprecated in favor of new implementation soon.

Task Advisor @bombnp

bombnp avatar Aug 23 '22 16:08 bombnp

Summary

Motivation and Changes for Frontend

  • Currently, we have two sets of GraphQL schema/types, one is in backend, one is in frontend
  • We should have Single Source of Truth. Like every company will do, we will use backend graphql as main schema
  • We will have codegen (libs/codegen) that read backend's graphql schema and generate types/documents/react hooks
  • All apollo queries on the frontend should now all import types/documents/react hooks from libs/codegen

Backend

  • Like frontend, we declare mongoose schema and graphql schema separately. This may cause some mismatch. We need to find a solution to write schema once and it generates the other. @Nacnano

PR Spliting

In this ticket, I would like to split PR into 2 (or more part) for easier reviewing. Sub-PRs are independent and can be merged separately.

Sub-PR 1: Frontend #415

  • libs/codegen, generate GraphQL types and documents from cugetreg-api's GraphQL schemas.
  • Convert all frontend (cugetreg-web) queries to use generated type from libs/codegen
  • Remove @thinc-org/chula-courses from dependencies

Sub-PR 2: Backend [TODO]

  • Convert to code first schema?

leomotors avatar Sep 10 '22 07:09 leomotors

@Leomotors since we're not using Nx anymore, does codegen still work? The generated code is probably fine, but the action to generate it required Nx right?

bombnp avatar Feb 02 '23 15:02 bombnp

@Leomotors since we're not using Nx anymore, does codegen still work? The generated code is probably fine, but the action to generate it required Nx right?

We need to convert the command into turbo way. Which is not that complex.

leomotors avatar Feb 02 '23 15:02 leomotors