Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed typo in README #2

Open
wants to merge 70 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
7286f0b
fix: fixed typo in README
AndriyKalashnykov Sep 8, 2022
cba2ff7
updates
AndriyKalashnykov Sep 8, 2022
aaaec4e
updates
AndriyKalashnykov Sep 8, 2022
d75540b
updates
AndriyKalashnykov Sep 8, 2022
fd8dc31
updates
AndriyKalashnykov Sep 8, 2022
d3a8410
updates
AndriyKalashnykov Sep 8, 2022
2ab3343
fix: updated README, added error handling to links and users
AndriyKalashnykov Sep 9, 2022
dd9e83d
fix: updated README
AndriyKalashnykov Sep 9, 2022
e1ac8a2
added schema
AndriyKalashnykov Sep 9, 2022
e611a7b
Updated README
AndriyKalashnykov Sep 9, 2022
dbfd6b6
Updated README
AndriyKalashnykov Sep 9, 2022
b6414ec
Bump actions/checkout from 3.0.2 to 3.1.0
dependabot[bot] Oct 4, 2022
1db2356
Bump actions/setup-go from 3.3.0 to 3.4.0
dependabot[bot] Dec 1, 2022
73753ff
Merge pull request #1 from AndriyKalashnykov/dependabot/github_action…
AndriyKalashnykov Dec 5, 2022
bbdeda9
Merge pull request #3 from AndriyKalashnykov/dependabot/github_action…
AndriyKalashnykov Dec 5, 2022
ada8050
Bump actions/setup-go from 3.4.0 to 3.5.0
dependabot[bot] Dec 13, 2022
b1d9664
Bump actions/checkout from 3.1.0 to 3.3.0
dependabot[bot] Jan 5, 2023
4756b6b
Add renovate.json
renovate[bot] May 26, 2023
2c37aaa
Merge pull request #5 from AndriyKalashnykov/dependabot/github_action…
AndriyKalashnykov May 26, 2023
fbdc882
Merge pull request #6 from AndriyKalashnykov/dependabot/github_action…
AndriyKalashnykov May 26, 2023
c3c0256
Merge pull request #7 from AndriyKalashnykov/renovate/configure
AndriyKalashnykov May 26, 2023
1fb3aa7
fix(deps): update module github.com/99designs/gqlgen to v0.17.31
renovate[bot] May 26, 2023
3451078
fix(deps): update module github.com/vektah/gqlparser/v2 to v2.5.1
renovate[bot] May 26, 2023
bd9aa6a
Merge pull request #9 from AndriyKalashnykov/renovate/github.com-vekt…
AndriyKalashnykov May 26, 2023
cda4a01
Merge pull request #8 from AndriyKalashnykov/renovate/github.com-99de…
AndriyKalashnykov May 26, 2023
b7f52b6
fix: updates
AndriyKalashnykov May 26, 2023
9e1cdc0
fix: updates
AndriyKalashnykov May 26, 2023
ce833da
Update renovate.json
AndriyKalashnykov May 26, 2023
0943621
Merge pull request #10 from AndriyKalashnykov/AndriyKalashnykov-patch-1
AndriyKalashnykov May 26, 2023
d9a2f0e
Bump actions/setup-go from 3.5.0 to 4.0.1
dependabot[bot] May 26, 2023
c20a2af
Bump actions/checkout from 3.3.0 to 3.5.2
dependabot[bot] May 26, 2023
91469fd
chore(all): update actions/checkout action to v3.5.2
renovate[bot] May 27, 2023
72f39e9
Merge pull request #11 from AndriyKalashnykov/dependabot/github_actio…
AndriyKalashnykov May 28, 2023
0f45163
Merge pull request #12 from AndriyKalashnykov/dependabot/github_actio…
AndriyKalashnykov May 28, 2023
4f39ca8
Merge pull request #13 from AndriyKalashnykov/renovate/all
AndriyKalashnykov May 28, 2023
b7f553e
chore(all): update module github.com/go-chi/chi to v4
renovate[bot] May 28, 2023
37b804f
Merge pull request #14 from AndriyKalashnykov/renovate/major-all
AndriyKalashnykov May 28, 2023
397caa1
chore(all): update all
renovate[bot] Jun 13, 2023
7da1715
Merge pull request #15 from AndriyKalashnykov/renovate/all
AndriyKalashnykov Jun 20, 2023
487baf0
fix: updates
AndriyKalashnykov Jun 20, 2023
eaaf3e9
Bump actions/setup-go from 4.0.1 to 4.1.0
dependabot[bot] Aug 8, 2023
c0a50c9
Bump actions/checkout from 3.5.3 to 4.0.0
dependabot[bot] Sep 4, 2023
b76ae6e
Merge pull request #20 from AndriyKalashnykov/dependabot/github_actio…
AndriyKalashnykov Sep 9, 2023
d7da830
Merge pull request #18 from AndriyKalashnykov/dependabot/github_actio…
AndriyKalashnykov Sep 9, 2023
4d300f4
fix: updated deps
AndriyKalashnykov Sep 9, 2023
4d389f8
chore(all): update module github.com/go-chi/chi to v5
renovate[bot] Sep 9, 2023
b064304
Merge pull request #21 from AndriyKalashnykov/renovate/major-all
AndriyKalashnykov Sep 9, 2023
e3d3e8a
chore(all): update golang Docker tag to v1.21.1
renovate[bot] Sep 9, 2023
b889947
Merge pull request #17 from AndriyKalashnykov/renovate/all
AndriyKalashnykov Sep 9, 2023
79c353b
chore(all): update all
renovate[bot] Sep 20, 2023
d49a2c7
Merge pull request #22 from AndriyKalashnykov/renovate/all
AndriyKalashnykov Sep 22, 2023
14e2655
chore(all): update all
renovate[bot] Oct 5, 2023
7c49ba8
Merge pull request #23 from AndriyKalashnykov/renovate/all
AndriyKalashnykov Oct 7, 2023
819b250
fix: updated deps
AndriyKalashnykov Oct 7, 2023
4024988
fix: updated deps
AndriyKalashnykov Oct 7, 2023
63de17e
chore(all): update golang Docker tag to v1.21.3
renovate[bot] Oct 11, 2023
083c8ec
fix: updates
AndriyKalashnykov Oct 24, 2023
ceaa068
chore(all): update actions/setup-go action to v5
renovate[bot] Dec 7, 2023
c5b803f
chore(all): update all
renovate[bot] Dec 30, 2023
858fb5b
Merge pull request #26 from AndriyKalashnykov/renovate/major-all
AndriyKalashnykov Jan 7, 2024
4d6c776
Merge pull request #25 from AndriyKalashnykov/renovate/all
AndriyKalashnykov Jan 7, 2024
cdd859f
chore(all): update all
renovate[bot] Feb 6, 2024
5dd29d1
Merge pull request #27 from AndriyKalashnykov/renovate/all
AndriyKalashnykov Feb 7, 2024
1c83774
fix: updates
AndriyKalashnykov Feb 7, 2024
ef0e175
chore(all): update all
renovate[bot] Jul 5, 2024
9e68b3f
Merge pull request #28 from AndriyKalashnykov/renovate/all
AndriyKalashnykov Jul 26, 2024
c98bae7
fix: updates
AndriyKalashnykov Jul 26, 2024
e20300c
fix: updates
AndriyKalashnykov Jul 26, 2024
281de02
fix: updates
AndriyKalashnykov Aug 21, 2024
f8fe5fc
fix: updates
AndriyKalashnykov Sep 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI

on:
push:
pull_request:

jobs:

builds:
timeout-minutes: 5
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ 1.23 ]

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Generate
run: make generate

- name: Build
run: make build

tests:
strategy:
matrix:
name: [unit]
runs-on: ubuntu-latest
name: test (${{ matrix.name }})

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: "go.mod"
cache: true

- name: Run ${{ matrix.name }} tests
run: make test
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
.idea
.DS_Store
.bin/
.idea/
dist/
vendor/
graph/generated
graph/model
.vscode/
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Build the manager binary
FROM golang:1.23 AS builder

WORKDIR /source
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# Cache deps before building and copying source so that we don't need to re-download
# as much and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Copy source code
COPY server.go server.go
COPY graph/ graph/
COPY internal/ internal/
COPY pkg/ pkg/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o server server.go

FROM scratch
COPY --from=builder /source/internal /internal
COPY --from=builder /source/server /server
CMD ["/server"]
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
GOFLAGS="-mod=mod"

clean:
@rm -rf ./.bin/
@sudo rm -rf vendor/
@mkdir ./.bin/

generate: clean
@sudo rm -rf graph/generated
@sudo rm -rf graph/model
@export GO15VENDOREXPERIMENT=0; export GOFLAGS=$(GOFLAGS); go run github.com/99designs/gqlgen generate

test: generate
@export GO15VENDOREXPERIMENT=0; export GOFLAGS=$(GOFLAGS); go test -v ./...

run: test
@export GO15VENDOREXPERIMENT=0; export GOFLAGS=$(GOFLAGS); go run server.go

build: generate
@export GO15VENDOREXPERIMENT=0; export GOFLAGS=$(GOFLAGS); go build -o ./.bin/server server.go

image: generate
docker build -t graphql-golang .

get: clean
@export GO15VENDOREXPERIMENT=0; export GOFLAGS=$(GOFLAGS); go get .

deps:
@export GO15VENDOREXPERIMENT=0; export GOFLAGS=$(GOFLAGS); go install github.com/99designs/gqlgen@latest

update: clean
@export GO15VENDOREXPERIMENT=0; export GOFLAGS=$(GOFLAGS); go get -u; go mod tidy
206 changes: 161 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,19 @@
---
title: ‌Introduction To GraphQL Server With Golang
published: false
description: Introduction to GraphQL Server with Golang and Gqlgen.
tags: graphql, go, api, gqlgen
---
[![CI](https://github.com/AndriyKalashnykov/graphql-golang/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/AndriyKalashnykov/graphql-golang/actions/workflows/ci.yml)
[![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FAndriyKalashnykov%2Fgraphql-golang&count_bg=%2340C83D&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=hits&edge_flat=false)](https://hits.seeyoufarm.com)
[![Renovate enabled](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://app.renovatebot.com/dashboard#github/AndriyKalashnykov/graphql-golang)
# GraphQL Server (schema-first) With Golang

## Table Of Contents
- [Table Of Contents](#table-of-contents)
- [How to Run The Project](#how-to-run-project)
- [Motivation ](#motivation)
- [What is a GraphQL server?](#what-is-a-graphql-server)
- [Schema-Driven Development](#schema-driven-development)
- [Getting started ](#getting-started)
- [Project Setup](#project-setup)
- [Defining Our Schema](#defining-out-schema)
- [Queries](#queries)
- [What Is A Query](#what-is-a-query)
- [Simple Query](#simple-query)
- [Mutations](#mutations)
- [What Is A Mutation](#what-is-a-mutation)
- [A Simple Mutation](#a-simple-mutation)
- [Database](#database)
- [Setup MySQL](#setup-mysql)
- [Models and migrations](#models-and-migrations)
- [Create and Retrieve Links](#create-and-retrieve-links)
- [CreateLinks](#createlinks)
- [Links Query](#links-query)
- [Authentication](#authentication)
- [JWT](#jwt)
- [Setup](#setup)
- [Generating and Parsing JWT Tokens](#generating-and-parsing-jwt-tokens)
- [User Signup and Login Functionality](#user-signup-and-login-functionality)
- [Authentication Middleware](#authentication-middleware)
- [Continue Implementing schema](#continue-implementing-schema)
- [CreateUser](#createuser)
- [Login](#login)
- [RefreshToken](#refresh-token)
- [Completing Our App](#completing-our-app)
- [Summary](#summary)
- [Further Steps](#further-steps)


- [GraphQL Server (schema-first) With Golang](#graphql-server-schema-first-with-golang)
- [Table Of Contents](#table-of-contents)
- [How to Run The Project <a name="how-to-run-project"></a>](#how-to-run-the-project-)
- [createUser](#createuser)
- [loginUser](#loginuser)
- [createLink](#createlink)
- [findLinks](#findlinks)
- [refreshToken](#refreshtoken)
- [Tutorial](#tutorial)

### How to Run The Project <a name="how-to-run-project"></a>
First start mysql server with docker:
```bash
Expand All @@ -51,12 +25,154 @@ docker exec -it mysql bash
mysql -u root -p
CREATE DATABASE hackernews;
```
finally run the server:

Run the server:

```bash
make run
```
Navigate to https://localhost:8080 you can see graphiql playground and query the graphql server.

```bash
go run server/server.go
xdg-open http://localhost:8080/
```

#### createUser

Execute createUser mutation

```graphql
mutation createUser {
createUser(input: {username: "usr2", password: "pwd"})
}
```

Expected JSON result:

```json
{
"data": {
"createUser": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjI3MzgyNDQsInVzZXJuYW1lIjoidXNyMiJ9.z0yrV6ajZO8IqFBlEuTwAnKRP-C15MuL1REmjJ5YYU8"
}
}
```

#### loginUser

Execute loginUser mutation

```graphql
mutation loginUser {
login(input: {username: "usr2", password: "pwd"})
}
```

Expected JSON result:

```json
{
"data": {
"login": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjI3MzgyNDQsInVzZXJuYW1lIjoidXNyMiJ9.z0yrV6ajZO8IqFBlEuTwAnKRP-C15MuL1REmjJ5YYU8"
}
}
```

#### createLink

Set Authorization Header, use token from loginUser

```json
{
"Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjI3MzgyNDQsInVzZXJuYW1lIjoidXNyMiJ9.z0yrV6ajZO8IqFBlEuTwAnKRP-C15MuL1REmjJ5YYU8"
}
```

Execute createLink mutation


```graphql
mutation createLink {
createLink(input: {title: "real link!", address: "www.graphql.org"}) {
user {
name
}
}
}
```

Expected JSON result:

```json
{
"data": {
"createLink": {
"user": {
"name": "usr2"
}
}
}
}
```

#### findLinks

Execute findLinks query


```graphql
query findLinks {
links {
title
address
id
}
}
```

Expected JSON result:

```json
{
"data": {
"links": [
{
"title": "real link!",
"address": "www.graphql.org",
"id": "1"
},
{
"title": "real link!",
"address": "www.graphql.org",
"id": "2"
}
]
}
}
```


#### refreshToken

Execute refreshToken mutation, provide current token as an input


```graphql
mutation refreshToken{
refreshToken(input: {
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjI3MzgyNDQsInVzZXJuYW1lIjoidXNyMiJ9.z0yrV6ajZO8IqFBlEuTwAnKRP-C15MuL1REmjJ5YYU8"
})
}
```
Now navigate to https://localhost:8080 you can see graphiql playground and query the graphql server.

Expected JSON result:

```json
{
"data": {
"refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjI3Mzg5NjYsInVzZXJuYW1lIjoidXNyMiJ9.fKK07Zv6iuq6ep9FtV3CE7z_KDm7ljnZqRvePokSOEs"
}
}
```

### Tutorial
to see the latest version of tutorial visit https://www.howtographql.com/graphql-go/0-introduction/
to see the tutorial visit https://www.howtographql.com/graphql-go/0-introduction/
48 changes: 34 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
module github.com/glyphack/graphlq-golang
module github.com/AndriyKalashnykov/graphlq-golang

go 1.13
go 1.23

require (
github.com/99designs/gqlgen v0.11.3
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/99designs/gqlgen v0.17.51
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v1.13.1 // indirect
github.com/go-chi/chi/v5 v5.1.0
github.com/go-sql-driver/mysql v1.8.1
github.com/golang-migrate/migrate v3.5.4+incompatible
github.com/vektah/gqlparser/v2 v2.5.16
golang.org/x/crypto v0.27.0
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v20.10.17+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/go-chi/chi v3.3.2+incompatible
github.com/go-sql-driver/mysql v1.5.0
github.com/golang-migrate/migrate v3.5.4+incompatible
github.com/golang-migrate/migrate/v4 v4.12.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/urfave/cli v1.20.0 // indirect
github.com/vektah/gqlparser v1.3.1 // indirect
github.com/vektah/gqlparser/v2 v2.0.1
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.7.0 // indirect
github.com/sosodev/duration v1.3.1 // indirect
github.com/urfave/cli/v2 v2.27.4 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/tools v0.25.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading