Skip to content

Commit

Permalink
Merge pull request #512 from hezhizhen/update_crud_example
Browse files Browse the repository at this point in the history
Update CRUD example
  • Loading branch information
chris-ramon committed Sep 22, 2019
2 parents 199d20b + 2e09464 commit b06cae7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
18 changes: 9 additions & 9 deletions examples/crud/Readme.md
@@ -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
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

0 comments on commit b06cae7

Please sign in to comment.