First start mysql server with docker:
docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=dbpass -e MYSQL_DATABASE=hackernews -d mysql:latest
Then create a Table names hackernews for our app:
docker exec -it mysql bash
mysql -u root -p
CREATE DATABASE hackernews;
Run the server:
make run
Navigate to https://localhost:8080 you can see graphiql playground and query the graphql server.
xdg-open http://localhost:8080/
Execute createUser mutation
mutation createUser {
createUser(input: {username: "usr2", password: "pwd"})
}
Expected JSON result:
{
"data": {
"createUser": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjI3MzgyNDQsInVzZXJuYW1lIjoidXNyMiJ9.z0yrV6ajZO8IqFBlEuTwAnKRP-C15MuL1REmjJ5YYU8"
}
}
Execute loginUser mutation
mutation loginUser {
login(input: {username: "usr2", password: "pwd"})
}
Expected JSON result:
{
"data": {
"login": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjI3MzgyNDQsInVzZXJuYW1lIjoidXNyMiJ9.z0yrV6ajZO8IqFBlEuTwAnKRP-C15MuL1REmjJ5YYU8"
}
}
Set Authorization Header, use token from loginUser
{
"Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjI3MzgyNDQsInVzZXJuYW1lIjoidXNyMiJ9.z0yrV6ajZO8IqFBlEuTwAnKRP-C15MuL1REmjJ5YYU8"
}
Execute createLink mutation
mutation createLink {
createLink(input: {title: "real link!", address: "www.graphql.org"}) {
user {
name
}
}
}
Expected JSON result:
{
"data": {
"createLink": {
"user": {
"name": "usr2"
}
}
}
}
Execute findLinks query
query findLinks {
links {
title
address
id
}
}
Expected JSON result:
{
"data": {
"links": [
{
"title": "real link!",
"address": "www.graphql.org",
"id": "1"
},
{
"title": "real link!",
"address": "www.graphql.org",
"id": "2"
}
]
}
}
Execute refreshToken mutation, provide current token as an input
mutation refreshToken{
refreshToken(input: {
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjI3MzgyNDQsInVzZXJuYW1lIjoidXNyMiJ9.z0yrV6ajZO8IqFBlEuTwAnKRP-C15MuL1REmjJ5YYU8"
})
}
Expected JSON result:
{
"data": {
"refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjI3Mzg5NjYsInVzZXJuYW1lIjoidXNyMiJ9.fKK07Zv6iuq6ep9FtV3CE7z_KDm7ljnZqRvePokSOEs"
}
}
to see the tutorial visit https://www.howtographql.com/graphql-go/0-introduction/