This project is an example of how to create and deploy a GraphQL API using Apollo Server 4 and Railway. It includes basic setup, resolvers, and is configured for easy development and deployment.
- Apollo Server 4: Uses the latest version of Apollo Server to create GraphQL APIs.
- TypeGraphQL: Defines the GraphQL schema using TypeScript decorators.
- Railway Deployment: Easily deployable using the Railway platform.
- Class Validator: Validates input data at the class level.
- Node.js: Version 20 or higher.
- Bun: A fast all-in-one runtime for JavaScript and TypeScript.
- Railway: Account set up to deploy the application (optional).
-
Clone the repository:
git clone https://github.com/your-username/graphql-api-with-apollo-server-railway.git cd graphql-api-with-apollo-server-railway
-
Install dependencies:
bun install
-
Configure environment variables. Create a .env file in the root of the project and add:
PORT=4000
bun run dev
: Starts the server in development mode.bun run dev:watch
: Starts the server in development mode with hot-reload.bun run build
: Compiles TypeScript code to JavaScript.bun run build:bun
: Compiles the code using Bun with minification.bun run start
: Compiles and starts the server from the compiled code.
To start the server in development mode, use:
bun run dev
The server will be available at http://localhost:4000.
The project includes a Docker setup with a Dockerfile and docker-compose.yml to handle both development and production environments.
This project can be easily run using Docker, which helps in setting up a consistent development environment. Follow the steps below to build and run the project using Docker.
- Docker
- docker-compose
Navigate to the directory containing the Dockerfile and docker-compose.yml and build the Docker images:
- Build the Docker Images:
docker-compose build
- Run in Development Mode:
docker-compose up app
This will start the development server with hot-reload enabled, allowing you to see changes in real-time. The server will be available at http://localhost:4000.
- Run in Production Mode:
docker-compose up app-prod
This will start the server in production mode. The server will be available at http://localhost:4000.
With the server running, you can make a GraphQL query. The following query uses the HelloWorldResolver:
query ExampleQuery($message: String!) {
log(message: $message)
}
Operation:
{
"message": "Hello, World!"
}
Expected response:
{
"data": {
"log": "Hey there! This is the message: Hello, World!"
}
}
This repository is part of the post written at https://dev.to/emunhoz/create-and-deploy-your-first-graphql-api-with-apollo-server-4-and-railway-2c3b