@appliedpiper/graphql-template-contract
v1.0.0
Published
A GraphQL server template built with TypeScript, Apollo Server, and MongoDB.
Readme
GraphQL TypeScript Template
A standalone GraphQL server template built with TypeScript, MongoDB, and Apollo Server, designed for rapid API development. GraphQL Server can be deployed in a local or containerized environment.
Table of Contents
- Features
- Prerequisites
- Getting Started
- Configure Environment Variables
- Seeding the Database
- Customizing the Schema
- Testing
- Docker
- Contributing
- License
Features
- TypeScript-first GraphQL API
- MongoDB as primary data source
- Modular
.graphqlschema files - Apollo Server context with multiple datasources support
- Custom Scalars (
Date,Email) - GraphQL Codegen for TypeScript types
- Seed data mutations for users and orders
- Jest tests for resolvers and database operations
- Containerized GraphQL Production and Development builds
Prerequisites
- Node.js >= 18
- pnpm >= 10
- MongoDB instance (local or cloud)
Getting Started
Follow these steps to set up the project locally:
- Install pnpm globally (if not already installed):
npm install -g pnpm- Clone the Repository
git clone https://github.com/appliedpiper/graphql-ts-template.git
cd graphql-ts-template- Install the Dependencies
pnpm install- Configure Environment Variables Update the .env file in the project root to specify the MongoDB URI, Database Name and GraphQL Port
DOTENV_CONFIG_QUIET=true
GQL_PORT=4000
#Replace the MONGO_URI with your MongoDB connection string
MONGO_URI=mongodb://localhost:27017
DB_NAME=gql_template- Start the Development Server
pnpm devYour server should now be running at http://localhost:4000/graphql (or the port you specified).
- Seeding the Database Use the GraphQL mutation seedDatabase to populate user and orders collections. Defaults: userCount = 5, orderCount = 10.
mutation SeedDatabase($input: SeedInput) {
seedDatabase(input: $input) {
ordersInserted
usersInserted
}
}Define the SeedInput as part of the gql variables
"variables": {
"input": {
"orderCount": 10,
"userCount": 20
}
}- Execute GQL Queries From http://localhost:4000/graphql Build a query such as
query USER_QUERY {
user(name: "1") {
firstName
lastName
email
orders {
total
createdAt
}
}
}- Customizing the Schema Update the schema as desired (/src/schema), creating new types, scalars, queries and mutations. After you make changes be sure to stop the GraphQL server and run:
pnpm generateThis will create the new TypeScript Types from the GraphQL Schema in /src/generated/types.ts. /src/schema/typeDefs.ts imports all sub-schemas.
Depending on the size of your project or organizational preference, you may want to organize your schema directory based on feature.
Ex:
src/
├─ schema/
│ ├─ user/
│ │ ├─ typeDefs.graphql
│ │ ├─ resolvers.ts
│ ├─ order/
│ │ ├─ typeDefs.graphql
│ │ ├─ resolvers.ts- Testing JEST is configured for Typescript. Example test cases are located in /src/tests/*.test.ts. Tests and coverage report can be executed by running:
pnpm test- Docker To improve the deployment process I included a Dockerfile and docker-compose.yml to fully containerize a production or development environments.
# Development mode with hot reload
docker-compose up graphql-dev --build
# Production mode
docker-compose up graphql-prod --buildContributing
- Fork the repository
- Create a feature branch (git checkout -b feature/my-feature)
- Commit your changes (git commit -m 'Add new feature')
- Push to the branch (git push origin feature/my-feature)
- Open a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
