Skip to content

Commit b6d2bbc

Browse files
authored
Merge branch 'master' into louis/fix-scalar-issue
2 parents f52d8fa + b06cae7 commit b6d2bbc

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

examples/crud/Readme.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
# Go GraphQL CRUD example
22

3-
Implementation create, read, update and delete on Go
3+
Implement create, read, update and delete on Go.
44

5-
To run the program, go to the directory
6-
`cd examples/crud`
5+
To run the program:
76

8-
Run the example
9-
`go run main.go`
7+
1. go to the directory: `cd examples/crud`
8+
2. Run the example: `go run main.go`
109

1110
## Create
11+
1212
`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}}`
1313

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

18-
Get product list
19-
`http://localhost:8080/product?query={list{id,name,info,price}}`
16+
* Get single product by id: `http://localhost:8080/product?query={product(id:1){name,info,price}}`
17+
* Get product list: `http://localhost:8080/product?query={list{id,name,info,price}}`
2018

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

2423
## Delete
24+
2525
`http://localhost:8080/product?query=mutation+_{delete(id:1){id,name,info,price}}`

examples/crud/main.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,34 @@ import (
1010
"github.com/graphql-go/graphql"
1111
)
1212

13+
// Product contains information about one product
1314
type Product struct {
1415
ID int64 `json:"id"`
1516
Name string `json:"name"`
1617
Info string `json:"info,omitempty"`
1718
Price float64 `json:"price"`
1819
}
1920

20-
var products []Product
21+
var products = []Product{
22+
{
23+
ID: 1,
24+
Name: "Chicha Morada",
25+
Info: "Chicha morada is a beverage originated in the Andean regions of Perú but is actually consumed at a national level (wiki)",
26+
Price: 7.99,
27+
},
28+
{
29+
ID: 2,
30+
Name: "Chicha de jora",
31+
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)",
32+
Price: 5.95,
33+
},
34+
{
35+
ID: 3,
36+
Name: "Pisco",
37+
Info: "Pisco is a colorless or yellowish-to-amber colored brandy produced in winemaking regions of Peru and Chile (wiki)",
38+
Price: 9.95,
39+
},
40+
}
2141

2242
var productType = graphql.NewObject(
2343
graphql.ObjectConfig{
@@ -204,17 +224,7 @@ func executeQuery(query string, schema graphql.Schema) *graphql.Result {
204224
return result
205225
}
206226

207-
func initProductsData(p *[]Product) {
208-
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}
209-
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}
210-
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}
211-
*p = append(*p, product1, product2, product3)
212-
}
213-
214227
func main() {
215-
// Primary data initialization
216-
initProductsData(&products)
217-
218228
http.HandleFunc("/product", func(w http.ResponseWriter, r *http.Request) {
219229
result := executeQuery(r.URL.Query().Get("query"), schema)
220230
json.NewEncoder(w).Encode(result)

0 commit comments

Comments
 (0)