From 2e0946431c8b58a4044bbf0d81aa3ba4e0e4b496 Mon Sep 17 00:00:00 2001 From: Zhizhen He Date: Sat, 21 Sep 2019 16:37:10 +0800 Subject: [PATCH] Update CRUD example --- examples/crud/Readme.md | 18 +++++++++--------- examples/crud/main.go | 32 +++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/examples/crud/Readme.md b/examples/crud/Readme.md index 9b5abb94..ab657516 100644 --- a/examples/crud/Readme.md +++ b/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}}` diff --git a/examples/crud/main.go b/examples/crud/main.go index bdd83504..dac20bcd 100644 --- a/examples/crud/main.go +++ b/examples/crud/main.go @@ -10,6 +10,7 @@ import ( "github.com/graphql-go/graphql" ) +// Product contains information about one product type Product struct { ID int64 `json:"id"` Name string `json:"name"` @@ -17,7 +18,26 @@ type Product struct { 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{ @@ -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)