/
schema.resolvers.go
99 lines (81 loc) · 2.75 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
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/execution/federationtesting/products/graph/generated"
"github.com/wundergraph/graphql-go-tools/execution/federationtesting/products/graph/model"
)
// SetPrice is the resolver for the setPrice field.
func (r *mutationResolver) SetPrice(ctx context.Context, upc string, price int) (*model.Product, error) {
panic(fmt.Errorf("not implemented: SetPrice - setPrice"))
}
// TopProducts is the resolver for the topProducts field.
func (r *queryResolver) TopProducts(ctx context.Context, first *int) ([]*model.Product, error) {
return hats[:len(hats)-1], 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):
product := hats[len(hats)-1]
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() {
var num int
for {
num++
select {
case <-ctx.Done():
return
case <-time.After(100 * time.Millisecond):
product.Price = num
updatedPrice <- product
}
}
}()
return updatedPrice, nil
}
// Mutation returns generated.MutationResolver implementation.
func (r *Resolver) Mutation() generated.MutationResolver { return &mutationResolver{r} }
// 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 mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }
type subscriptionResolver struct{ *Resolver }