/
schema.resolvers.go
116 lines (96 loc) · 2.88 KB
/
schema.resolvers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package graph
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
// Code generated by github.com/99designs/gqlgen version v0.17.22
import (
"context"
"fmt"
"math/rand"
"time"
"github.com/wundergraph/graphql-go-tools/v2/examples/federation/products/graph/generated"
"github.com/wundergraph/graphql-go-tools/v2/examples/federation/products/graph/model"
)
// TopProducts is the resolver for the topProducts field.
func (r *queryResolver) TopProducts(ctx context.Context, first *int) ([]*model.Product, error) {
return hats, nil
}
// UpdatedPrice is the resolver for the updatedPrice field.
func (r *subscriptionResolver) UpdatedPrice(ctx context.Context) (<-chan *model.Product, error) {
updatedPrice := make(chan *model.Product)
go func() {
for {
select {
case <-ctx.Done():
return
case <-time.After(updateInterval):
rand.Seed(time.Now().UnixNano())
product := hats[0]
if randomnessEnabled {
product = hats[rand.Intn(len(hats)-1)]
product.Price = rand.Intn(maxPrice-minPrice+1) + minPrice
updatedPrice <- product
continue
}
product.Price = currentPrice
currentPrice += 1
updatedPrice <- product
}
}
}()
return updatedPrice, nil
}
// UpdateProductPrice is the resolver for the updateProductPrice field.
func (r *subscriptionResolver) UpdateProductPrice(ctx context.Context, upc string) (<-chan *model.Product, error) {
updatedPrice := make(chan *model.Product)
var product *model.Product
for _, hat := range hats {
if hat.Upc == upc {
product = hat
break
}
}
if product == nil {
return nil, fmt.Errorf("unknown product upc: %s", upc)
}
go func() {
for {
select {
case <-ctx.Done():
return
case <-time.After(time.Second):
rand.Seed(time.Now().UnixNano())
min := 10
max := 1499
product.Price = rand.Intn(max-min+1) + min
updatedPrice <- product
}
}
}()
return updatedPrice, nil
}
// Stock is the resolver for the stock field.
func (r *subscriptionResolver) Stock(ctx context.Context) (<-chan []*model.Product, error) {
stock := make(chan []*model.Product)
go func() {
for {
select {
case <-ctx.Done():
return
case <-time.After(2 * time.Second):
rand.Seed(time.Now().UnixNano())
randIndex := rand.Intn(len(hats))
if hats[randIndex].InStock > 0 {
hats[randIndex].InStock--
}
stock <- hats
}
}
}()
return stock, nil
}
// Query returns generated.QueryResolver implementation.
func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} }
// Subscription returns generated.SubscriptionResolver implementation.
func (r *Resolver) Subscription() generated.SubscriptionResolver { return &subscriptionResolver{r} }
type queryResolver struct{ *Resolver }
type subscriptionResolver struct{ *Resolver }