π Welcome to the Fastify-Prisma-Nexus-GraphQL-Jest Boilerplate! π
This boilerplate provides you with a solid foundation to build amazing and professional GraphQL servers using Fastify, Prisma, Nexus, GraphQL, and Jest!
To get started, make sure you have the following installed:
- Node.js v19.6.1 or later π’
- PostgreSQL (for the database) π
- Yarn (recommended) or npm π§Ά
- Docker π³
Check the engines
and dependencies
sections in the package.json
for the specific versions of required packages.
Follow these simple steps to set up the project:
- Clone the repository:
git clone https://github.com/nimatra/fastify-prisma-nexus-graphql-jest.git
π¦ - Change to the project directory:
cd fastify-prisma-nexus-graphql-jest
π - Install dependencies:
yarn install
ornpm install
𧩠- Set up environment variables by creating a
.env
file in the project root, based on the provided.env.example
file. π - Update the
prisma/schema.prisma
file with your database connection details. ποΈ - Run database migrations:
yarn migrate:dev
ornpm run migrate:dev
π§ - Start the development server:
yarn dev
ornpm run dev
π
π‘ Bonus: You can use the included Docker Compose file in /dockers/db-compose.yml
to quickly set up a local PostgreSQL database. π³
Now you're ready to build your next amazing GraphQL server! Let's get started! π
This boilerplate comes packed with a variety of features to help you kickstart your GraphQL server:
The project includes built-in authentication using Fastify, Passport, and bcryptjs. You can easily integrate your preferred authentication provider or extend the current implementation to suit your needs.
Effortlessly manage your database schema using Prisma migrations. This boilerplate includes scripts for running, resetting, and deploying migrations, ensuring that your database schema remains in sync with your code.
- Pino for logging π
- Altair GraphQL Client for interacting with your GraphQL API π‘
- Swagger for API documentation π
The boilerplate comes with a sample schema that demonstrates how to define your GraphQL types and resolvers using Nexus. This schema covers the models listed below and includes relationships between them. You can easily customize the schema to match your application's needs.
The template includes sample code for the following models:
- Users π₯
- Tasks π
- Comments π¬
- UserTasks π€π
- TaskRoles π
prisma-erd-generator
generates an informative ERD diagram that is helpful for developer discussions. π
To bootstrap your production database with initial data, you can use the provided seed file located in the prisma/seed.ts
directory. Modify it to include your desired seed data and run the db:push
script to populate your production database. π
The boilerplate includes a comprehensive testing setup using Jest with a pre-configured jest.config.js
file. You'll find sample tests for the included models in the tests
directory. These tests cover various aspects of the application, such as resolvers, middleware, and utility functions. The custom configuration for Jest includes:
- TypeScript support with the
ts-jest
preset. - A dedicated test environment, using Node.js.
- Global setup and teardown scripts for test initialization and cleanup.
- Test environment variables set to load from a separate
.env.test
file.
To run the tests, simply execute the test
or test:all
script. π¦
Every test run will create a new test database and seed the test/seed.ts
data into the database. It's recommended to use the Docker-based database. π³
π Test Coverage π
Keep track of your project's test coverage with Jest's built-in coverage reporting. The boilerplate is pre-configured to generate coverage reports when running the tests. You can view the coverage report in the coverage
directory after running the tests. π
The included Github action, named "Run Tests for Modified Files", ensures high code quality by automatically running tests for modified files in your pull requests. The action is triggered when a pull request is opened, updated, or labeled on the main
branch.
Here's what the action does:
- Runs on the latest Ubuntu environment.
- Sets up a PostgreSQL 15 service with a test database and user.
- Checks out your repository.
- Sets up Node.js with version 19 and caches the Yarn dependencies.
- Installs dependencies using the
yarn install --frozen-lockfile
command. - Gets the list of modified files in the pull request with a
.test.ts
extension. - Runs Jest tests only for the modified files, using the
yarn test --findRelatedTests
command.
The action makes use of environment variables to configure the test database connection. This ensures a seamless and efficient testing process for every commit to the repository. β
We welcome contributions to this open-source boilerplate! If you have any ideas or suggestions, please feel free to open an issue or submit a pull request. We'll review your changes and work with you to ensure that this boilerplate remains a top choice for developers building Fastify GraphQL servers. π‘
π License π
This project is licensed under the MIT License. See the LICENSE
file for more information. π
π Acknowledgments π
We'd like to thank the developers and maintainers of Fastify, Prisma, Nexus, GraphQL, and Jest for their excellent work on these libraries. This boilerplate wouldn't be possible without their efforts. π