Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
djangulo authored Sep 27, 2019
2 parents ef54081 + 5fa0612 commit cbea67d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
18 changes: 9 additions & 9 deletions examples/crud/Readme.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# Go GraphQL CRUD example

Implementation create, read, update and delete on Go
Implement create, read, update and delete on Go.

To run the program, go to the directory
`cd examples/crud`
To run the program:

Run the example
`go run main.go`
1. go to the directory: `cd examples/crud`
2. Run the example: `go run main.go`

## Create

`http://localhost:8080/product?query=mutation+_{create(name:"Inca Kola",info:"Inca Kola is a soft drink that was created in Peru in 1935 by British immigrant Joseph Robinson Lindley using lemon verbena (wiki)",price:1.99){id,name,info,price}}`

## Read
Get single product by id
`http://localhost:8080/product?query={product(id:1){name,info,price}}`

Get product list
`http://localhost:8080/product?query={list{id,name,info,price}}`
* Get single product by id: `http://localhost:8080/product?query={product(id:1){name,info,price}}`
* Get product list: `http://localhost:8080/product?query={list{id,name,info,price}}`

## Update

`http://localhost:8080/product?query=mutation+_{update(id:1,price:3.95){id,name,info,price}}`

## Delete

`http://localhost:8080/product?query=mutation+_{delete(id:1){id,name,info,price}}`
32 changes: 21 additions & 11 deletions examples/crud/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,34 @@ import (
"github.com/graphql-go/graphql"
)

// Product contains information about one product
type Product struct {
ID int64 `json:"id"`
Name string `json:"name"`
Info string `json:"info,omitempty"`
Price float64 `json:"price"`
}

var products []Product
var products = []Product{
{
ID: 1,
Name: "Chicha Morada",
Info: "Chicha morada is a beverage originated in the Andean regions of Perú but is actually consumed at a national level (wiki)",
Price: 7.99,
},
{
ID: 2,
Name: "Chicha de jora",
Info: "Chicha de jora is a corn beer chicha prepared by germinating maize, extracting the malt sugars, boiling the wort, and fermenting it in large vessels (traditionally huge earthenware vats) for several days (wiki)",
Price: 5.95,
},
{
ID: 3,
Name: "Pisco",
Info: "Pisco is a colorless or yellowish-to-amber colored brandy produced in winemaking regions of Peru and Chile (wiki)",
Price: 9.95,
},
}

var productType = graphql.NewObject(
graphql.ObjectConfig{
Expand Down Expand Up @@ -204,17 +224,7 @@ func executeQuery(query string, schema graphql.Schema) *graphql.Result {
return result
}

func initProductsData(p *[]Product) {
product1 := Product{ID: 1, Name: "Chicha Morada", Info: "Chicha morada is a beverage originated in the Andean regions of Perú but is actually consumed at a national level (wiki)", Price: 7.99}
product2 := Product{ID: 2, Name: "Chicha de jora", Info: "Chicha de jora is a corn beer chicha prepared by germinating maize, extracting the malt sugars, boiling the wort, and fermenting it in large vessels (traditionally huge earthenware vats) for several days (wiki)", Price: 5.95}
product3 := Product{ID: 3, Name: "Pisco", Info: "Pisco is a colorless or yellowish-to-amber colored brandy produced in winemaking regions of Peru and Chile (wiki)", Price: 9.95}
*p = append(*p, product1, product2, product3)
}

func main() {
// Primary data initialization
initProductsData(&products)

http.HandleFunc("/product", func(w http.ResponseWriter, r *http.Request) {
result := executeQuery(r.URL.Query().Get("query"), schema)
json.NewEncoder(w).Encode(result)
Expand Down
2 changes: 1 addition & 1 deletion scalars.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ var Float = NewScalar(ScalarConfig{
return floatValue
}
case *ast.IntValue:
if floatValue, err := strconv.ParseFloat(valueAST.Value, 32); err == nil {
if floatValue, err := strconv.ParseFloat(valueAST.Value, 64); err == nil {
return floatValue
}
}
Expand Down

0 comments on commit cbea67d

Please sign in to comment.