This project attempts to provide a simple framework for GraphQL server development using graphql-yoga with the freedom of selecting and implementing any database.
This app implements a small retail store wherein store administrators can create users (customers), products and post a sale transaction. Each sale transaction record has an active user (customer) and active product.
Core idea is to provide service definitions which conform to business logic as interfaces under src/service/ (service definitions). Service definitions may or may not be dependent on a persistent storage (database / external APIs / files) but if it they are, then persistent storage is implemented by implementing corresponding interface at src/repository/ (service repository). Similarly, business logic of a particular service definition is defined at src/controller/ (service controller). If persistent storage is required then service controller depends upon a service repository which implements the relevant service definition. GraphQL resolvers then directly use service controller objects.
For rest of things such as DB implementation, etc. refer Project structure
                                                            ┌---------------------------┐
                                                            |  Service Definition (SD)  |
                                                            |                           |
                                                            |            User           |
                                                            |      - getDetail()        |
                                                            |                           |
   ┌-----------┐    ┌--------------------------┐            |         FileHandler       |
   |     C     |    |            API           |            |      - upload()           |
   |     L     |    |       Depends on SC      |            |      - download()         |
   |     I     |    |                          |     ┌------|                           |------┐
   |     E     |◄---|   - mutation:upload      |     |      |        Authenticate       |      |
   |     N     |    |      └FM.uploadAuth()    |     |      |      - authUser()         |      |
   |     T     |    |                          |     |      |                           |      |
   |           |    |  - query:download        |     |      |         FileManager       |      |
   | Calls API |    |     └FM.downloadAuth()   |     |      |      - uploadAuth()       |      |
   └-----------┘    └--------------------------┘     |      |      - downloadAuth()     |      |
                                ▲                    |      |                           |      |
                                |                    |      └---------------------------┘      |
                                |                    ▼                                         ▼
                        ┌-------------------------------------------┐            ┌-------------------------------------------┐
                        |          Service Controller (SC)          |            |          Service Repository (SR)          |
                        |      Implements SD, may depend on SR      |            |  Implements SD, connects to other API, DB |
                        |                                           |            |                                           |
                        |            Authenticate (AUTH)            |◄-----------|               User (USR)                  |
                        |           - authUser()                    |            |           - getDetail()                   |
                        |             └USR.getDetail()              |            |             └Query Database               |
                        |                                           |            |                                           |
                        |              FileManager (FM)             |            |            FileHandler (FH)               |
                        |           - uploadAuth()                  |            |           - upload()                      |
                        |             └AUTH.authUser()              |            |             └Upload file to server        |
                        |              └FH.upload()                 |            |                                           |
                        |                                           |            |           - download()                    |
                        |           - downloadAuth()                |            |             └Download file from server    |
                        |             └AUTH.authUser()              |            └-------------------------------------------┘
                        |              └FH.download()               |
                        └-------------------------------------------┘
| Folder Path | Description | 
|---|---|
| env/ | Environment files | 
| logs/ | Logs directory | 
| src/controller/ | Business Logic implementation of services defined in src/sevice/ | 
| src/core/ | Basic functionalities required across the project | 
| src/core/repository/ | Database implementation. Currently supports MongoDB, MySQL and LokiJS | 
| src/di/ | Dependency injection configuration. Implemented using inversify | 
| src/entity/ | Class and Interface definitions | 
| src/error-handler/ | Express error handler with custom error objects | 
| src/event/ | Implement various event streams primarily for logging | 
| src/graphql/ | GraphQL resolvers and type definitions | 
| src/middleware/ | Routers and Request handlers for managing basic express security | 
| src/repository/ | Repository implementation of services defined in src/sevice/ | 
| src/service/ | Service definitions | 
| src/index | Handles server initialization and startup | 
| test/ | Test scripts | 
| test/api/ | API tests | 
| test/controller/ | Controller tests | 
| test/repository/ | Repository tests | 
| test/di/ | Dependency injection configuration for controller and repository tests | 
| util/build.js | Utility to build project | 
| nodemon.json | Nodemon configuration | 
| package.json | Project dependencies | 
| tsconfig.json | Transpiling configuration | 
| tsconfig.prod.json | Production transpiling configuration | 
| tslint.json | TS Linting rules | 
Clone this repository and go to this project's root location
npm i -g tsc ts-node ts-mocha tslint nodemonnpm i -DRefer DB configuration file
npm run start:devOpen browser and go to http://localhost:4000/playgroundnpm testnpm run buildnpm start