This is example of using GraphQL for API in Go with MySQL. This project is for my learning purpose.
Tech | Description |
---|---|
graphql-go | graphql-go An implementation of GraphQL for Go / Golang. |
graphql-go-handler | Middleware to handle GraphQL queries through HTTP requests. |
godotenv | A Go (golang) port of the Ruby dotenv project (which loads env vars from a .env file). |
go-mysql-driver | A MySQL-Driver for Go's database/sql package. |
- Install Go
- Install MySQL
- Create database
- Create local.env
export DB_CONN="<username>:<password>@tcp(<hostname>:<port>)/<db_name>?parseTime=true"
Query to create new user
mutation {
createUser(name: "Kharisma Januar", email: "kharisma.januar@gmail.com") {
id
name
email
created_at
}
}
Query to get all users
query {
users {
id
name
email
created_at
updated_at
}
}
Query to get user by id
query {
user(id: 1) {
id
name
email
created_at
updated_at
}
}
Query to update user
mutation {
updateUser(id: 1, name: "Kharisma Januar", email: "kharisma.januar@gmail.com") {
id
name
email
updated_at
}
}
Query to delete user (soft delete)
mutation {
deleteUser(id: 1) {
id
}
}
Query to create new task
mutation {
createTask(name: "Study GraphQL Go", description: "Implement GraphQL in Go", user_id:1) {
id
name
created_at
user{
id
}
}
}
Query to get all tasks
query{
tasks{
id
name
description
is_complete
created_at
updated_at
user{
id
name
email
}
}
}
Query to get task by id
query {
task(id: 1) {
id
name
description
is_complete
user{
id
email
name
}
created_at
updated_at
}
}
Query to update task
mutation {
updateTask(id: 1, name: "Learn Git", description: "Learn Git for beginners", user_id:1, is_complete: true) {
id
name
description
user{
id
name
}
is_complete
updated_at
}
}
Query to delete task (soft delete)
mutation{
deleteTask(id:1) {
id
}
}