A GraphQL server that supports both PostgreSQL and MongoDB databases, built with Go and gqlgen.
- Dual database support (PostgreSQL and MongoDB)
- GraphQL API with playground
- CRUD operations for Patients
- Database schema validation
- Environment-based configuration
- Go 1.16 or higher
- PostgreSQL
- MongoDB
- Make (optional, for using Makefile commands)
- Clone the repository:
git clone https://github.com/sdshah09/Go-GraphQL
cd my-graphql-server- Install dependencies:
go mod download- Create a
.envfile in the root directory:
# PostgreSQL Configuration
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your_password
DB_NAME=your_db_name
# MongoDB Configuration
MONGO_URI=mongodb://localhost:27017
MONGO_DB=your_db_name- Run the server:
go run server.goThe GraphQL playground will be available at http://localhost:8080
mutation {
createPatient(
input: {
first_name: "John"
last_name: "Doe"
db: "postgres" # or "mongo"
}
) {
id
first_name
last_name
}
}query {
patients(db: "postgres") { # or "mongo"
id
first_name
last_name
}
}CREATE TABLE patients (
id SERIAL PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255)
);{
$jsonSchema: {
bsonType: "object",
required: ["first_name"],
properties: {
first_name: {
bsonType: "string",
description: "must be a string and is required"
},
last_name: {
bsonType: "string",
description: "must be a string if present"
}
}
}
}.
├── config/ # Configuration management
├── graph/ # GraphQL schema and resolvers
├── utils/ # Database utilities
│ ├── postgres/ # PostgreSQL specific code
│ └── mongodb/ # MongoDB specific code
├── server.go # Main server file
└── .env # Environment variables
To regenerate GraphQL code after schema changes:
go run github.com/99designs/gqlgen generate