A Go GraphQL API for users, companies, roles, interview questions, and posts.
- Go 1.25
- gqlgen for GraphQL server generation
- GORM with PostgreSQL driver
- Atlas for database migrations
- Air (optional) for live-reload during development
.
|-- server.go # API entrypoint (GraphQL server + Playground)
|-- loader.go # Atlas schema loader from GORM models
|-- gqlgen.yml # gqlgen codegen config
|-- atlas.hcl # Atlas migration configuration
|-- database/
| |-- connection.go # DB connection setup
| |-- sql.go # GORM models
|-- graph/
| |-- schema.graphqls # GraphQL schema
| |-- schema.resolvers.go # Resolver implementations
| |-- generated.go # gqlgen generated server code
| |-- model/models_gen.go # gqlgen generated GraphQL models
|-- migrations/
| |-- 20260320174513.sql # SQL migration(s)
- Go 1.25+
- PostgreSQL running locally or remotely
- Atlas CLI (for migration commands)
- Optional: Air for hot reload
Create a .env file in the project root.
PORT=8080
DB_URL=postgres://postgres:mysecretpassword@127.0.0.1:5499/postgres?sslmode=disableVariables:
PORT: HTTP port for the API server (defaults to8080if unset).DB_URL: PostgreSQL connection string used by GORM.
- Download dependencies.
go mod download-
Ensure PostgreSQL is running and
DB_URLpoints to it. -
Apply migrations.
atlas migrate apply --env gorm- Start the server.
go run .- Open GraphQL Playground.
- Playground:
http://localhost:8080/ - GraphQL endpoint:
http://localhost:8080/query
If Air is installed:
airRun after schema changes:
go run github.com/99designs/gqlgen generateloader.go exposes the GORM schema for Atlas.
atlas migrate diff <migration_name> --env gormExample:
atlas migrate diff add_new_field --env gormgetUser(id: ID!): UsergetAllCompanies: [Company!]!getRolesByCompany(companyId: ID!): [Role!]!getQuestionsByCompany(companyId: ID!): [QuestionBank!]!getPost(id: ID!): PostgetAllPosts: [Post!]!
createUser(input: CreateUserInput!): User!createCompany(input: CreateCompanyInput!): Company!createRole(input: CreateRoleInput!): Role!createQuestion(input: CreateQuestionInput!): QuestionBank!createPost(input: CreatePostInput!): Post!deletePost(id: ID!): Boolean!
query GetAllCompanies {
getAllCompanies {
id
companyName
}
}mutation CreateCompany {
createCompany(input: { companyName: "Acme" }) {
id
companyName
}
}createUseris declared in the schema but currently not implemented in resolvers.createQuestioncurrently does not persistcompanyIdandyearsfrom input.createPostcurrently does not persistuserId(and does not exposecompanyIdin GraphQL input).getUserreturns onlyidandusernamefields from resolver mapping.getRolesByCompanysetscompanyIdin response using role ID instead of company ID.- Password handling in current models/resolvers does not include hashing logic.
- Error:
DB_URL environment variable not set- Add
DB_URLto.envand restart.
- Add
- Error:
Error loading .env file- Ensure
.envexists in the project root.
- Ensure
- Migration problems:
- Verify
atlas.hclhas the correct dev database URL.
- Verify
- Server starts but GraphQL fails:
- Confirm database connectivity and migration state.
- The server serves GraphQL Playground at
/and the GraphQL API at/query. - Generated files under
graph/generated.goandgraph/model/models_gen.goshould be treated as codegen output.