Skip to content

Commit fb042f4

Browse files
committed
update
1 parent c8e82e1 commit fb042f4

File tree

9 files changed

+205
-2
lines changed

9 files changed

+205
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# golang-mysql-graphql
2-
Contoh Graphql Golang + MySQL
1+
# Golang-mysql-graphql
2+
Example Golang graphql api with mysql db.

config/dbConfig.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package config
2+
3+
import (
4+
"database/sql"
5+
_ "github.com/go-sql-driver/mysql"
6+
)
7+
8+
9+
func GetConnection() (*sql.DB,error){
10+
db,err := sql.Open("mysql","user:pass@tcp(xxx.xxx.xxx.xxx:PORT)/namedb")
11+
if err != nil {
12+
panic(err.Error())
13+
}
14+
err = db.Ping()
15+
if err != nil {
16+
panic(err.Error())
17+
}
18+
return db,err
19+
}

main.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"github.com/madasatya6/golang-mysql-graphql/schema/query"
5+
"encoding/json"
6+
"fmt"
7+
"github.com/graphql-go/graphql"
8+
"github.com/madasatya6/golang-mysql-graphql/schema/mutation"
9+
"net/http"
10+
)
11+
12+
func executeQuery(query string, schema graphql.Schema) *graphql.Result {
13+
result := graphql.Do(graphql.Params{
14+
Schema: schema ,
15+
RequestString: query,
16+
})
17+
if len(result.Errors) > 0 {
18+
fmt.Printf("wrong result, unexpected errors: %v", result.Errors)
19+
}
20+
return result
21+
}
22+
23+
func main(){
24+
var Schame,err = graphql.NewSchema(graphql.SchemaConfig{
25+
Query:query.RootQuery,
26+
Mutation: mutation.Mutation,
27+
})
28+
if err != nil{
29+
panic(err.Error())
30+
}
31+
http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
32+
result := executeQuery(r.URL.Query().Get("Query"), Schame)
33+
json.NewEncoder(w).Encode(result)
34+
})
35+
http.ListenAndServe(":8088", nil)
36+
37+
}

schema/mutation/mutation.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package mutation
2+
3+
import (
4+
"github.com/madasatya6/golang-mysql-graphql/schema/types"
5+
"github.com/graphql-go/graphql"
6+
)
7+
8+
var Mutation = graphql.NewObject(graphql.ObjectConfig{
9+
Name:"Mutation",
10+
Fields:graphql.Fields{
11+
"CreateProducts":&graphql.Field{
12+
Type:graphql.NewList(types.ProductTypes),
13+
//config param argument
14+
Args:graphql.FieldConfigArgument{
15+
"ID_PRO": &graphql.ArgumentConfig{
16+
Type:graphql.NewNonNull(graphql.String),
17+
},
18+
"PRO_NAME": &graphql.ArgumentConfig{
19+
Type:graphql.NewNonNull(graphql.String),
20+
},
21+
"QTE_IN_STOCK": &graphql.ArgumentConfig{
22+
Type:graphql.NewNonNull(graphql.Int),
23+
},
24+
"PRO_IMAGE": &graphql.ArgumentConfig{
25+
Type:graphql.NewNonNull(graphql.String),
26+
},
27+
},
28+
Resolve: CreateProductMutation ,
29+
},
30+
// untuk membuat object lainya tinggal di ulang
31+
32+
},
33+
})

schema/mutation/resolverMutation.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package mutation
2+
3+
import (
4+
"github.com/madasatya6/golang-mysql-graphql/config"
5+
"github.com/graphql-go/graphql"
6+
)
7+
8+
func CreateProductMutation(param graphql.ResolveParams) (interface{},error) {
9+
idpro := param.Args["ID_PRO"].(string)
10+
nama := param.Args["PRO_NAME"].(string)
11+
qty := param.Args["QTE_IN_STOCK"].(int)
12+
img := param.Args["PRO_IMAGE"].(string)
13+
db, err := config.GetConnection()
14+
if err != nil {
15+
panic(err.Error())
16+
}
17+
_ , err = db.Query("INSERT INTO Restaurant.Products values (?,?,?,?)",idpro,nama,qty,img)
18+
if err != nil {
19+
panic(err.Error())
20+
}
21+
22+
return nil, err
23+
}

schema/query/query.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package query
2+
3+
import (
4+
"github.com/madasatya6/golang-mysql-graphql/schema/types"
5+
"github.com/graphql-go/graphql"
6+
)
7+
8+
var RootQuery = graphql.NewObject(graphql.ObjectConfig{
9+
Name:"Query",
10+
Fields:graphql.Fields{
11+
"Products":&graphql.Field{
12+
Type:graphql.NewList(types.ProductTypes),
13+
//config param argument
14+
Args:graphql.FieldConfigArgument{
15+
"ID_PRO": &graphql.ArgumentConfig{
16+
Type:graphql.NewNonNull(graphql.String),
17+
},
18+
},
19+
Resolve: ProductResolve,
20+
},
21+
// untuk membuat object lainya tinggal di ulang
22+
23+
},
24+
})

schema/query/resolverQuery.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package query
2+
import (
3+
"github.com/graphql-go/graphql"
4+
"github.com/madasatya6/golang-mysql-graphql/schema/types"
5+
"github.com/madasatya6/golang-mysql-graphql/config"
6+
)
7+
8+
func ProductResolve(param graphql.ResolveParams) (interface{},error){
9+
var a types.Product
10+
var b []types.Product
11+
db ,err:= config.GetConnection()
12+
if err != nil {
13+
panic(err.Error())
14+
}
15+
b = b[:0]
16+
result,err := db.Query("select ID_PRO,PRO_NAME,QTE_IN_STOCK,ifnull(PRO_IMAGE,'') from Products")
17+
if err != nil{
18+
panic(err.Error())
19+
}
20+
21+
for result.Next(){
22+
err = result.Scan(&a.IdPro,&a.ProName,&a.QteStock,&a.ProImg)
23+
if err != nil{
24+
panic(err.Error())
25+
}
26+
b = append(b,a)
27+
28+
}
29+
30+
31+
return b , nil
32+
}

schema/types/Model.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package types
2+
3+
type Product struct {
4+
IdPro string `json:"ID_PRO"`
5+
ProName string `json:"PRO_NAME"`
6+
QteStock int `json:"QTE_IN_STOCK"`
7+
ProImg string `json:"PRO_IMAGE"`
8+
}

schema/types/types.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package types
2+
import (
3+
4+
"github.com/graphql-go/graphql"
5+
6+
)
7+
8+
var ProductTypes = graphql.NewObject(graphql.ObjectConfig{
9+
Name:"Products",
10+
Fields:graphql.Fields{
11+
"ID_PRO":&graphql.Field{
12+
Type:graphql.Int,
13+
},
14+
"PRO_NAME":&graphql.Field{
15+
Type:graphql.String,
16+
},
17+
"QTE_IN_STOCK":&graphql.Field{
18+
Type:graphql.Int,
19+
},
20+
"PRICE":&graphql.Field{
21+
Type:graphql.Float,
22+
},
23+
"PRO_IMAGE":&graphql.Field{
24+
Type:graphql.String,
25+
},
26+
},
27+
})

0 commit comments

Comments
 (0)