-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
42 lines (32 loc) · 846 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"fmt"
"github.com/gin-gonic/gin"
graphql "github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/relay"
)
type gopherHelloWorldResolver struct{}
func (r *gopherHelloWorldResolver) Hello() string {
return "world"
}
var gopherSchema = graphql.MustParseSchema(`
schema {
query: Query
}
type Query {
hello: String!
}
`, &gopherHelloWorldResolver{})
func gophersGraphQLHandler() gin.HandlerFunc {
r := &relay.Handler{Schema: gopherSchema}
return func(c *gin.Context) {
r.ServeHTTP(c.Writer, c.Request)
}
}
func main() {
r := gin.New()
r.POST("/graphql", gophersGraphQLHandler())
fmt.Println("Now server is running on port 8080")
fmt.Println("Test with POST : curl -g --request POST 'http://localhost:8080/graphql?query={hello}'")
r.Run() // listen and serve on 0.0.0.0:8080
}