-
Notifications
You must be signed in to change notification settings - Fork 0
/
review.pb.gql.go
201 lines (180 loc) · 5.11 KB
/
review.pb.gql.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Code generated by protoc-gen-graphql-gateway. DO NOT EDIT.
// source: example/federation/proto/review/review.proto
/*
Package review is a reverse proxy.
It translates gRPC into GraphQL.
*/
package review
import (
"context"
"github.com/graphql-go/graphql"
"github.com/grpc-custom/graphql-gateway/runtime"
"github.com/grpc-custom/graphql-gateway/runtime/cache"
"github.com/grpc-custom/graphql-gateway/runtime/errors"
"github.com/grpc-custom/graphql-gateway/runtime/scalar"
"golang.org/x/sync/singleflight"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
)
var (
reviewType = graphql.NewObject(graphql.ObjectConfig{
Name: "Review",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: scalar.String,
},
"body": &graphql.Field{
Type: scalar.String,
},
},
})
listUserReviewsRequestType = graphql.NewObject(graphql.ObjectConfig{
Name: "ListUserReviewsRequest",
Fields: graphql.Fields{
"userId": &graphql.Field{
Type: graphql.NewNonNull(scalar.String),
},
"first": &graphql.Field{
Type: graphql.NewNonNull(scalar.Int32),
},
},
})
listUserReviewsResponseType = graphql.NewObject(graphql.ObjectConfig{
Name: "ListUserReviewsResponse",
Fields: graphql.Fields{
"reviews": &graphql.Field{
Type: graphql.NewList(graphql.NewNonNull(reviewType)),
},
},
})
listProductReviewsRequestType = graphql.NewObject(graphql.ObjectConfig{
Name: "ListProductReviewsRequest",
Fields: graphql.Fields{
"productId": &graphql.Field{
Type: graphql.NewNonNull(scalar.String),
},
},
})
listProductReviewsResponseType = graphql.NewObject(graphql.ObjectConfig{
Name: "ListProductReviewsResponse",
Fields: graphql.Fields{
"reviews": &graphql.Field{
Type: graphql.NewList(graphql.NewNonNull(reviewType)),
},
},
})
)
func RegisterGQLObjectTypes(mux *runtime.ServeMux) {
mux.AddObjectType(reviewType)
}
type extendUserReviews interface {
GetId() string
}
type extendProductReviews interface {
GetUpc() string
}
type reviewServiceResolver struct {
client ReviewServiceClient
group singleflight.Group
c cache.Cache
}
func newReviewServiceResolver(client ReviewServiceClient) *reviewServiceResolver {
return &reviewServiceResolver{
client: client,
group: singleflight.Group{},
c: cache.New(100),
}
}
func (r *reviewServiceResolver) extendUserReviews() *graphql.Field {
return &graphql.Field{
Type: graphql.NewList(graphql.NewNonNull(reviewType)),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
ctx := runtime.Context(p.Context)
src, ok := p.Source.(extendUserReviews)
if !ok {
return nil, errors.ErrWrongType
}
args := p.Args
args["userId"] = src.GetId()
return r.resolveListUserReviews(ctx, args)
},
}
}
func (r *reviewServiceResolver) resolveListUserReviews(ctx context.Context, args map[string]interface{}) (interface{}, error) {
in := new(ListUserReviewsRequest)
valueUserId, ok := args["userId"].(string)
if !ok {
valueUserId = ""
}
in.UserId = valueUserId
valueFirst, ok := args["first"].(int32)
if !ok {
valueFirst = 5
}
in.First = valueFirst
out, err := r.client.ListUserReviews(ctx, in)
if err != nil {
return nil, errors.ToGraphQLError(err)
}
return out.Reviews, nil
}
func (r *reviewServiceResolver) extendProductReviews() *graphql.Field {
return &graphql.Field{
Type: graphql.NewList(graphql.NewNonNull(reviewType)),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
ctx := runtime.Context(p.Context)
src, ok := p.Source.(extendProductReviews)
if !ok {
return nil, errors.ErrWrongType
}
args := p.Args
args["productId"] = src.GetUpc()
return r.resolveListProductReviews(ctx, args)
},
}
}
func (r *reviewServiceResolver) resolveListProductReviews(ctx context.Context, args map[string]interface{}) (interface{}, error) {
in := new(ListProductReviewsRequest)
valueProductId, ok := args["productId"].(string)
if !ok {
valueProductId = ""
}
in.ProductId = valueProductId
out, err := r.client.ListProductReviews(ctx, in)
if err != nil {
return nil, errors.ToGraphQLError(err)
}
return out.Reviews, nil
}
func RegisterReviewServiceFromEndpoint(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error) {
conn, err := grpc.DialContext(ctx, endpoint, opts...)
if err != nil {
return
}
defer func() {
if err != nil {
if e := conn.Close(); e != nil {
grpclog.Infof("Failed to close conn to %s: %v", endpoint, e)
}
return
}
go func() {
<-ctx.Done()
if e := conn.Close(); e != nil {
grpclog.Infof("Failed to close conn to %s: %v", endpoint, e)
}
}()
}()
return RegisterReviewServiceHandler(mux, conn)
}
func RegisterReviewServiceHandler(mux *runtime.ServeMux, conn *grpc.ClientConn) error {
return RegisterReviewServiceHandlerClient(mux, NewReviewServiceClient(conn))
}
func RegisterReviewServiceHandlerClient(mux *runtime.ServeMux, client ReviewServiceClient) error {
svc := newReviewServiceResolver(client)
// extend User.reviews
mux.AddField("User", "reviews", svc.extendUserReviews())
// extend Product.reviews
mux.AddField("Product", "reviews", svc.extendProductReviews())
return nil
}