A GraphQL server built with Go that manages courses and categories. This project uses gqlgen for GraphQL implementation and SQLite as the database.
- Categories Management: Create and list categories
- Courses Management: Create and list courses with category relationships
- GraphQL Playground: Interactive interface for testing queries and mutations
- SQLite Database: Lightweight database for data persistence
- Relational Data: Full support for course-category relationships
- Go 1.24.4
- GraphQL with gqlgen
- SQLite database
- UUID for unique identifiers
server-graphql/
├── graph/
│ ├── model/ # Generated GraphQL models
│ ├── schema.graphqls # GraphQL schema definition
│ ├── schema.resolvers.go # GraphQL resolvers
│ ├── resolver.go # Main resolver struct
│ └── generated.go # Generated GraphQL code
├── internal/
│ └── database/ # Database layer
│ ├── category.go # Category repository
│ └── course.go # Course repository
├── server/
│ └── server.go # HTTP server setup
├── data.db # SQLite database file
├── gqlgen.yml # gqlgen configuration
├── tools.go # Build tools
├── go.mod # Go modules
└── go.sum # Go modules checksums
-
Clone the repository
git clone https://github.com/sergioc0sta/server-graphql cd server-graphql -
Install dependencies
go mod tidy
-
Generate GraphQL code
go run github.com/99designs/gqlgen generate
-
Run the server
go run server/server.go
-
Access GraphQL Playground Open your browser and navigate to: `http://localhost:8080/
GraphQL Schema
type Category {
id: ID!
name: String!
description: String
courses: [Courses!]!
}
type Courses {
id: ID!
name: String!
description: String
category: Category!
}
input NewCategory {
name: String!
description: String
}
input NewCourse {
name: String!
description: String
categoryId: ID!
}
type Query {
categories: [Category!]!
courses: [Courses!]!
}
type Mutation {
createCategory(input: NewCategory!): Category!
createCourse(input: NewCourse!): Courses!
}API Examples
mutation NewCategory {
createCategory(input: {name: "mistas", description: "pao ou rosca"}) {
id
name
description
}
}Expected Response:
{
"data": {
"createCategory": {
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "mistas",
"description": "pao ou rosca"
}
}
}mutation NewCourse {
createCourse(input: {
name: "novo",
description: "top",
categoryId: "918d7b83-ca5c-4916-8988-63e3b44b974c"
}) {
id
name
description
category {
id
name
description
}
}
}Expected Response:
{
"data": {
"createCourse": {
"id": "123e4567-e89b-12d3-a456-426614174000",
"name": "novo",
"description": "top",
"category": {
"id": "918d7b83-ca5c-4916-8988-63e3b44b974c",
"name": "mistas",
"description": "pao ou rosca"
}
}
}
}query FindCategories {
categories {
id
name
description
}
}query FindCategoriesAndCourses {
categories {
id
name
description
courses {
id
name
description
}
}
}query FindCourses {
courses {
id
description
name
}
}query FindCoursesAndCategory {
courses {
name
description
id
category {
name
description
id
}
}
}The application uses SQLite with the following tables:
CREATE TABLE categories (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT
);CREATE TABLE courses (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
category_id TEXT,
FOREIGN KEY (category_id) REFERENCES categories(id)
);Development
After modifying the schema file (graph/schema.graphqls), regenerate the code:
go run github.com/99designs/gqlgen generate- Update the GraphQL schema in
graph/schema.graphqls - Run code generation
- Implement the resolver methods in
graph/schema.resolvers.go
The database layer is located in internal/database/:
category.go- Handles category CRUD operationscourse.go- Handles course CRUD operations
PORT- Server port (default: 8080)
The gqlgen configuration is in gqlgen.yml. Key settings:
- Schema location:
graph/*.graphqls - Generated models:
graph/model/models_gen.go - Resolvers:
graph/schema.resolvers.go
- Start the server:
go run server/server.go - Open
http://localhost:8080/in your browser - Use the examples above to test the API
- Explore the schema using the built-in documentation