https://github.com/elmariachi111/fastify-api-demo
a sample repository how to build REST/GQL APIs in Typescript centered around entity definitions using Fastify
https://github.com/elmariachi111/fastify-api-demo
fastify graphql rest-api typegraphql typeorm typescript
Last synced: 12 days ago
JSON representation
a sample repository how to build REST/GQL APIs in Typescript centered around entity definitions using Fastify
- Host: GitHub
- URL: https://github.com/elmariachi111/fastify-api-demo
- Owner: elmariachi111
- Created: 2021-04-27T11:03:47.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-05-02T12:04:50.000Z (almost 5 years ago)
- Last Synced: 2025-06-01T13:21:53.262Z (11 months ago)
- Topics: fastify, graphql, rest-api, typegraphql, typeorm, typescript
- Language: TypeScript
- Homepage:
- Size: 81.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Fastify in Typescript with Graphql, Typeorm & Tests
I've been a seasoned and very satisfied user of PHP's [API Platform](https://api-platform.com/) and since I'm still looking for a framework that lets me build APIs equally swiftly, I evaluated a lot of libraries in the JS space. [Fastify](https://www.fastify.io) turned out to be one promising candidate for the foundation.
Even though it's a much more general purpose framework, it deals with many advantages that API Platform comes with.
After cloning you can test it yourself:
```
yarn
yarn run test
yarn run dev
```
The administrative endpoints are
```
http://localhost:3000/docs
http://localhost:3000/graphql
```
This application demonstrates REST API based persistence based on entity definitions. I defined two entities (Token, Transaction) that relate to each other as one to many.
It's using
- [TypeORM](https://typeorm.io/#/) as persistence layer (analoguous to DoctrineORM)
- [TypeGraphQL](https://typegraphql.com/) for GraphQL definitions
- [TypeDI](https://www.npmjs.com/package/typedi) for dependency injection (mandatory for TypeGraphQL's ORM resolvers)
- Fastify's JSON schema support and [typebox](https://github.com/sinclairzx81/typebox) for Typescript translation
- [fastify-swagger](https://github.com/fastify/fastify-swagger) to generate a dynamic OpenAPI documentation
- mocha as test runner
- declaration merging for type safety and IDE completion
## Opinion
There are lesser cobbles on the path to glory when you use API Platform. It's built around Doctrine ORM and JSON-LD and allows you to build complete REST APIs out of simple entity definitions by adding a mere `ApiResource` annotation.
In this repository's approach you'll still have to define schemas and fastify types along your entity (it's likely possible to infer them by analyzing the entity types but that's not clearly documented).
In this approach it's far more obvious how to customize things since you're not relying on an opinionated framework. That leads to more boilerplate code but makes you feel much more in control after all.
## execute migrations
gen
yarn run typeorm migration:generate -n initial -f src/config/ormconfig.json
run
yarn run typeorm migration:run -f src/config/ormconfig.json