The Task Tracker API is a GraphQL-based API for managing tasks and users. It allows users to create, update, and delete tasks, as well as create, update, and delete users. This project is implemented using Go, gqlgen, and Bun for database interaction.
- Create a new user with a unique identifier
- Update user details such as name and email
- Delete a user by ID
- Create a task associated with a user
- Update task details such as title and description
- Delete a task by ID
- Retrieve user information based on ID
- Retrieve task information based on ID
- Retrieve all users
- Retrieve all tasks
Task Tracker API
├── Create User
│ ├── Input: name, email
│ └── Output: created user information (id, name, email)
├── Update User
│ ├── Input: user ID, updated name, updated email
│ └── Output: updated user information (id, name, email)
├── Delete User
│ ├── Input: user ID
│ └── Output: deleted user ID
├── Create Task
│ ├── Input: user ID, task details (title, description)
│ └── Output: created task information (id, title, description, userId)
├── Update Task
│ ├── Input: task ID, updated details (title, description)
│ └── Output: updated task information (id, title, description, userId)
├── Delete Task
│ ├── Input: task ID
│ └── Output: deleted task ID
├── Get User
│ ├── Input: user ID
│ └── Output: user information (id, name, email)
├── Get Task
│ ├── Input: task ID
│ └── Output: task information (id, title, description, userId)
├── Get Users
│ └── Output: list of all users
└── Get Tasks
└── Output: list of all tasks
- Go: The programming language used for building the API.
- gqlgen: A Go library for generating GraphQL servers based on a schema and resolver functions.
- Bun: A lightweight and fast Go database toolkit for interacting with the database.
To get started with the Task Tracker API, follow these steps:
-
Clone the repository:
git clone https://github.com/zaahidali/Learn-go-language.git
-
Navigate to the project directory:
cd task-tracker-api
-
Install the required dependencies:
go mod download
-
Configure the database connection:
Open the
config.yaml
file and update the database connection details according to your setup. -
Run the API:
go run server.go
The API server will be running on
http://localhost:8080
.
The GraphQL schema defines the available queries and mutations for interacting with the Task Tracker API. Here is an overview of the schema:
type User {
id: ID!
name: String!
email: String!
}
type Task {
id: ID!
userId: String!
title: String!
description: String!
status: TaskStatus!
dueDate: String!
createdAt: String!
updatedAt: String!
}
enum TaskStatus {
COMPLETED
INPROGRESS
PENDING
}
type Query {
getUser(id: ID!): User!
getUsers: [User!]!
getTask(id: ID!): Task!
getTasks: [Task!]!
}
type Mutation {
createUser(name: String, email: String) : User!
createTask(userId: String, title: String, description: String, status: TaskStatus, dueDate: String): Task!
updateUser(id: ID, name: String, email: String): User!
deleteUser(id: ID): ID!
updateTask(id: ID, title: String, description: String, status: TaskStatus, dueDate: String): Task!
}
To install the go-migrate library and command line tool, follow these steps:
-
Install the migrate library:
Run the following command to install the migrate library:
go get -u github.com/golang-migrate/migrate/v4
-
Install the migrate command line tool:
Run the following command to install the migrate command line tool:
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
-
Add the bin directory to your PATH:
Make sure the bin directory of your GOPATH is added to your system's PATH. Run the following command to add it:
echo 'export PATH=$PATH:$(go env GOPATH)/bin' >> ~/.bashrc
-
Source the bashrc file:
Run the following command to load the new PATH into your current shell session:
source ~/.bashrc
After completing these steps, you should have go-migrate installed on your system and the migrate command line tool available for use.
This project uses a Makefile for managing database migrations and running the application. Make sure you have make
and PostgreSQL
installed on your system to use these commands.
-
First, you need to create your PostgreSQL database. Open a
psql
session from your terminal:psql -U postgres
Then run the following command at the
psql
prompt to create the database:CREATE DATABASE task_tracker_db_with_gql_bun;
Press
CTRL+D
to exit thepsql
session. -
Run
make migrate-up
to generate tables using the migrations in themigrations
directory.
make run
: Runs the Go application.make migrate-up
: Applies all available database migrations, updating your database schema to its latest version.make migrate-down
: Undoes all applied database migrations, rolling your database schema back to its initial state.make create-migration <name>
: Creates a new migration file in themigrations
directory. Replace<name>
with the desired name for the migration.make remove-migration <migration>
: Removes a specific migration file. Replace<migration>
with the name of the migration you want to remove. Use this command with caution, and only for migrations which have not been applied.
Please replace <name>
and <migration>
with appropriate values when using make create-migration
and make remove-migration
, respectively.
These instructions assume that you're using PostgreSQL with a default postgres
user. If your PostgreSQL installation uses different settings, you may need to adjust these instructions accordingly.
To start a new project with gqlgen, follow these steps:
-
Create a new project directory:
mkdir gqlgen-demo cd gqlgen-demo
-
Initialize the Go module:
go mod init github.com/[your_user_name]/gqlgen-demo
-
Create the tools.go file:
Run the following command to create the
tools.go
file with the necessary imports:printf '// +build tools\npackage tools\nimport (_ "github.com/99designs/gqlgen"\n _ "github.com/99designs/gqlgen/graphql/introspection")' | gofmt > tools.go
-
Tidy the Go module:
Run the following command to tidy the Go module and download the required dependencies:
go mod tidy
-
Initialize gqlgen:
Run the following command to initialize gqlgen in your project:
go run github.com/99designs/gqlgen init
This will generate the initial project structure and the
gqlgen.yml
configuration file. -
Tidy the Go module again:
Run the following command to tidy the Go module after the gqlgen initialization:
go mod tidy
-
Run the server:
Start the API server by running the following command:
go run server.go
The API server will be running on
http://localhost:8080
. -
Generating code for resolvers:
Whenever you make changes to the schema or resolver functions, you need to generate the corresponding code. Run the following command to generate the code:
go run github.com/99designs/gqlgen generate
This command will generate the necessary resolvers and resolver methods based on your schema.
Remember to modify the project structure and files according to your specific requirements.
I apologize for the misunderstanding. Here's an updated version with the code and the description added to it:
The following Playground code showcases how to interact with the Task Tracker API using GraphQL queries and mutations. You can use this code to test and explore the functionality of the API.
mutation {
createUser(name: "John Doe", email: "john@example.com") {
id
name
email
}
}
mutation {
updateUser(id: "123", name: "John Doe", email: "john@example.com") {
id
name
email
}
}
mutation {
deleteUser(id: "123")
}
query {
getUser(id: "123") {
id
name
email
}
}
query {
getUsers {
id
name
email
}
}
mutation {
createTask(userId:"6993", title: "Test Title", description: "This is a test", status: COMPLETED, dueDate: "2023-06-05") {
id
title
description
dueDate
}
}
mutation {
updateTask(id: "4938", title: "Hello Peter", description: "I am here", status: INPROGRESS, dueDate: "2024-06-05") {
title
description
status
dueDate
}
}
mutation {
deleteTask(id: "789")
}
query {
getTask(id: "789") {
id
title
description
}
}
query {
getTasks {
id
title
description
dueDate
}
}
Your contributions are most welcome! If you'd like to improve this project, please:
- Fork the repository.
- Clone your fork and create a new branch.
- Make and commit your changes.
- Push the changes to your fork.
- Submit a pull request.
After you've submitted the pull request, I will review your changes. Thank you for your contribution!
This project is licensed under the MIT License. See the LICENSE file for more information.