Skip to content

Commit d49db70

Browse files
author
lmtr0
committed
new: graphql server with eco
1 parent 915182c commit d49db70

File tree

9 files changed

+210
-0
lines changed

9 files changed

+210
-0
lines changed

golang-echo-graphql/go.mod

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module server
2+
3+
go 1.19
4+
5+
require (
6+
github.com/graphql-go/graphql v0.8.0 // indirect
7+
github.com/graphql-go/handler v0.2.3 // indirect
8+
github.com/labstack/echo/v4 v4.8.0 // indirect
9+
github.com/labstack/gommon v0.3.1 // indirect
10+
github.com/mattn/go-colorable v0.1.11 // indirect
11+
github.com/mattn/go-isatty v0.0.14 // indirect
12+
github.com/valyala/bytebufferpool v1.0.0 // indirect
13+
github.com/valyala/fasttemplate v1.2.1 // indirect
14+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
15+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect
16+
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
17+
golang.org/x/text v0.3.7 // indirect
18+
)

golang-echo-graphql/go.sum

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/graphql-go/graphql v0.8.0 h1:JHRQMeQjofwqVvGwYnr8JnPTY0AxgVy1HpHSGPLdH0I=
4+
github.com/graphql-go/graphql v0.8.0/go.mod h1:nKiHzRM0qopJEwCITUuIsxk9PlVlwIiiI8pnJEhordQ=
5+
github.com/graphql-go/handler v0.2.3 h1:CANh8WPnl5M9uA25c2GBhPqJhE53Fg0Iue/fRNla71E=
6+
github.com/graphql-go/handler v0.2.3/go.mod h1:leLF6RpV5uZMN1CdImAxuiayrYYhOk33bZciaUGaXeU=
7+
github.com/labstack/echo/v4 v4.8.0 h1:wdc6yKVaHxkNOEdz4cRZs1pQkwSXPiRjq69yWP4QQS8=
8+
github.com/labstack/echo/v4 v4.8.0/go.mod h1:xkCDAdFCIf8jsFQ5NnbK7oqaF/yU1A1X20Ltm0OvSks=
9+
github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o=
10+
github.com/labstack/gommon v0.3.1/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
11+
github.com/mattn/go-colorable v0.1.11 h1:nQ+aFkoE2TMGc0b68U2OKSexC+eq46+XwZzWXHRmPYs=
12+
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
13+
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
14+
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
15+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
16+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
17+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
18+
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
19+
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
20+
github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4=
21+
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
22+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
23+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
24+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY=
25+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
26+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
27+
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
28+
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b h1:1VkfZQv42XQlA/jchYumAnv1UPo6RgF9rJFkTgZIxO4=
29+
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
30+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
31+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
32+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
33+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
34+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package fields
2+
3+
import (
4+
model "server/models"
5+
6+
"github.com/graphql-go/graphql"
7+
)
8+
9+
var user = graphql.NewObject(
10+
graphql.ObjectConfig{
11+
Name: "User",
12+
Fields: graphql.Fields{
13+
"id": &graphql.Field{Type: graphql.ID},
14+
"name": &graphql.Field{Type: graphql.String},
15+
"age": &graphql.Field{Type: graphql.String},
16+
},
17+
Description: "Users data",
18+
},
19+
)
20+
21+
func NewUsers() *graphql.Field {
22+
return &graphql.Field{
23+
Type: graphql.NewList(user),
24+
Resolve: func(p graphql.ResolveParams) (i interface{}, e error) {
25+
var u []*model.User = []*model.User{
26+
{
27+
Id: "id1",
28+
Name: "User 1",
29+
Age: 15,
30+
},
31+
{
32+
Id: "id2",
33+
Name: "User 2",
34+
Age: 32,
35+
},
36+
{
37+
Id: "id2",
38+
Name: "User 3",
39+
Age: 24,
40+
},
41+
}
42+
43+
return u, nil
44+
},
45+
Description: "user",
46+
}
47+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package graphql
2+
3+
import (
4+
"log"
5+
6+
"github.com/labstack/echo/v4"
7+
)
8+
9+
func Route(e *echo.Echo) {
10+
h, err := NewHandler()
11+
if err != nil {
12+
log.Fatal(err)
13+
}
14+
15+
e.POST("/gql", echo.WrapHandler(h))
16+
e.GET("/gql", echo.WrapHandler(h))
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package graphql
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/graphql-go/graphql"
7+
"github.com/graphql-go/handler"
8+
)
9+
10+
func NewHandler() (*handler.Handler, error) {
11+
schema, err := graphql.NewSchema(
12+
graphql.SchemaConfig{
13+
Query: newQuery(),
14+
},
15+
)
16+
if err != nil {
17+
fmt.Println("Failed to initialize graphql server")
18+
return nil, err
19+
}
20+
21+
fmt.Println("initialized graphql server")
22+
return handler.New(&handler.Config{
23+
Schema: &schema,
24+
Pretty: true,
25+
// GraphiQL: true,
26+
Playground: true,
27+
}), nil
28+
}

golang-echo-graphql/graphql/query.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package graphql
2+
3+
import (
4+
"server/graphql/fields"
5+
6+
"github.com/graphql-go/graphql"
7+
)
8+
9+
func newQuery() *graphql.Object {
10+
return graphql.NewObject(graphql.ObjectConfig{
11+
Name: "Query",
12+
Fields: graphql.Fields{
13+
"users": fields.NewUsers(),
14+
},
15+
})
16+
}

golang-echo-graphql/handler.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/labstack/echo/v4"
7+
)
8+
9+
func Welcome() echo.HandlerFunc {
10+
return func(c echo.Context) error {
11+
return c.String(http.StatusOK, "Welcome!")
12+
}
13+
}

golang-echo-graphql/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"server/graphql"
7+
8+
"github.com/labstack/echo/v4"
9+
)
10+
11+
func main() {
12+
// Echo instance
13+
e := echo.New()
14+
15+
// Routes
16+
e.GET("/", Welcome())
17+
18+
// graphql handler
19+
fmt.Println("Graphql Endpoint")
20+
graphql.Route(e)
21+
fmt.Println("Ended Graphql Endpoint")
22+
23+
// Start server
24+
e.Logger.Fatal(e.Start(":3000"))
25+
}
26+
27+
// Handler
28+
func hello(c echo.Context) error {
29+
return c.String(http.StatusOK, "Hello, World!")
30+
}

golang-echo-graphql/models/user.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package model
2+
3+
type User struct {
4+
Id string `json:"id"`
5+
Name string `json:"name"`
6+
Age int64 `json:"age"`
7+
}

0 commit comments

Comments
 (0)