Git Product home page Git Product logo

graphql-typescript-boilerplate's Introduction

GraphQL TypeScript Boilerplate

GitHub version Dependency Status devDependency Status Build Status Coverage Status

This project attempts to provide a simple framework for GraphQL server development using graphql-yoga with the freedom of selecting and implementing any database.

Application Business logic

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.

Project Structure logic

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

Pictorial representation for a file management service with user authentication:

                                                            ┌---------------------------┐
                                                            |  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()               |
                        └-------------------------------------------┘

Project structure

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

To start server, follow these steps

Step 1: Install NodeJS & NPM

NodeJS & NPM

Step 2: Get project

Clone this repository and go to this project's root location

Step 3: Add global dependencies

npm i -g tsc ts-node ts-mocha tslint nodemon

Step 4: Add local dependencies

npm i -D

Step 5: Configure Database

Refer DB configuration file

Step 6: Run in dev environment

npm run start:dev

Step 7: Check API

Open browser and go to http://localhost:4000/playground

Step 8 (Optional): Run test cases

npm test

Step 9: Build for production

npm run build

Step 10: Run in production environment

npm start

graphql-typescript-boilerplate's People

Contributors

akash-kansara avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

sf-kansara

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.