Starter project for backend development of Graphql APIs hooked into Typeorm and embedded Postgraphile infrastructure.
Alternatives: Graphql API with typescript via Prisma OR GraphQL API with typescript via TypeGraphQL
- Provides great development experience similar to
Prisma, including code generation of query bindings,GraphqlAPI schema, etc.. - Does not require separate running proxy process, like Prisma or Postrgaphile server. All
GraphQLqueries are wired directly to the connection with the database. - Server is powered by
ApolloServer 2.0 - Flexible and powerful schema definitions by
Typeormdecorators - Fine control for database migrations using
Typeormfeatures, including:- Automated generation of migration queries based on introspection of source code models and state of the database
- Run of the migrations on application's launch or manually via command line
- Revert of the migrations via command line
- Separate configurations and scripts for development, stage and production environment
TypescriptandTSLintenabled- Build script creates distributable folder without dependencies to local
node_modulesfolder - GraphQL playground server for experimenting with application's GraphQL API and with Postgraphile's GraphQL API
- Implement resolvers using
Typeormqueries or wire to embeddedPostgraphilebinding queries.
- Run
yarn install - Edit
config.dev.jsondatabase connection settings (usesTypeormconnection configuration options) - Launch everything in development
watchmode:yarn start- it will run the initial database setup (the first database schema migrations script) and will start development environment. Check terminal output for served endpoints and ports. - Play with example queries saved in examples.graphql file
- Build everthing into self contained distributable folder:
yarn build
- Edit
src/entity/**files to match your data model. It usesTypeormto define the database model. - Run
yarn migrations:generate, review and modify if necessary the generated migration script. In most cases you do not need to modify it,Typeormdoes nice job generating migration scripts. - Run
yarn migrations:runor just start the application's server to deploy the migrations - Run
yarn postgraphile:generateto generate postgraphile bindings and internal GraphQL schema for querying the database with the latest schema (code generation steps are explained here) - Wire Application's GraphQL API calls to Postgraphile API calls as it is required by your business logic: see
src/resolvers/**scripts for example.
yarn startstarts GraphQL server inwatchmode and Playground serveryarn watchstarts GraphQL server inwatchmodeyarn buildbuilds self contained distributable folder with production buildyarn playgroundopens the GraphQL Playground for theprojectsfrom.graphqlconfig.ymlyarn postgraphile:serverunsPostgraphileserver targeting development database. It is only for development and usage in playground.Postgraphileserver is not required in production.yarn migrations:generategenerates database schema migration queriesyarn migrations:run,yarn migrations:run:stageandyarn migrations:run:proddeployes all pending schema migrations targeting development, stage and production databases accordinglyyarn migrations:revertdeployes all pending schema migrations
Check other commands in the package.json file. They are all self explanatory.
| File name | Description |
|---|---|
config.*.json |
Datebase connection settings for dev, stage and production environments |
scripts/** |
Build and code generation scripts invoked by npm/yarn commands |
src/entity/** |
Definition of the database model |
src/migrations/** |
Generated or manually prepared database schema migrations scripts |
src/generated/** |
Code automatically generated for quering the database via Postgraphile rendered queries |
src/resolvers/** |
Implementation of your application's GraphQL API resolvers |
└──Mutation/auth.ts |
Example of implementing GraphQL resolvers using Typeorm queries |
└──Mutation/post.ts |
Example of implementing GraphQL resolvers using transparent proxing to Postgraphile queries resolver |
docker-compose.yaml |
Config for launching Postgres database locally via docker-compose up -d |
This feature is intentionally not added to the starter project for few reasons:
- Subscriptions, based on
PostgraphileorTypeormsubscriptions, do not scale beyong a single process. If you still prefer this there are many examples and blogs howTypeormsubscriptions can be wired to GraphQL subscription endpoint.Postgraphilealso has got subscriptions support via additional plugin. AsPostgraphileorTypeormnotifications are not caused by the database itself, you will need to wire both libraries for notifications or pick only one for invoking database mutations. - Better subscriptions are based on Postgres triggers raising notifications on row updates. This will scale better but require selective choice of what triggers to place. If you prefer this approach: create new database migration script which will add Postgres notification TRIGGER on create/update/delete AND subscribe to these notification using plaing
pgdriver. - Even better use some message queue, like
Kafkafor resilient, scalable, distributed notifications. But this goes beyond of this project boilerplate.
Enjoy!
MIT License
Copyright (c) 2018 Andrey Konstantinov & Contributors