-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
172 lines (142 loc) · 3.92 KB
/
server.go
File metadata and controls
172 lines (142 loc) · 3.92 KB
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
package graphql
import (
"context"
"errors"
"fmt"
"log/slog"
"github.com/99designs/gqlgen/graphql/handler/apollotracing"
"github.com/go-modulus/modulus/http/errhttp"
"github.com/vektah/gqlparser/v2/ast"
"go.uber.org/fx"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/transport"
infraErrors "github.com/go-modulus/modulus/errors"
"github.com/ravilushqa/otelgqlgen"
"github.com/vektah/gqlparser/v2/gqlerror"
)
type PlaygroundConfig struct {
Enabled bool `env:"GQL_PLAYGROUND_ENABLED, default=true"`
Path string `env:"GQL_PLAYGROUND_URL, default=/playground"`
}
type Config struct {
ComplexityLimit int `env:"GQL_COMPLEXITY_LIMIT, default=200"`
Path string `env:"GQL_API_URL, default=/graphql"`
IntrospectionEnabled bool `env:"GQL_INTROSPECTION_ENABLED, default=true"`
TracingEnabled bool `env:"GQL_TRACING_ENABLED, default=false"`
ReturnCause bool `env:"GQL_RETURN_CAUSE, default=false"`
Playground PlaygroundConfig
}
type ErrorPresenterParams struct {
fx.In
ErrorPipeline *errhttp.ErrorPipeline `optional:"true"`
Config Config
}
type ServerParams struct {
fx.In
Config Config
Schema graphql.ExecutableSchema
LoadersInitializer *LoadersInitializer `optional:"true"`
Logger *slog.Logger
ErrorPresenter graphql.ErrorPresenterFunc
}
func NewGraphqlServer(
params ServerParams,
) *handler.Server {
var mb int64 = 1 << 20
config := params.Config
srv := handler.New(params.Schema)
srv.AddTransport(transport.Options{})
srv.AddTransport(transport.GET{})
srv.AddTransport(transport.POST{})
srv.AddTransport(
transport.MultipartForm{
MaxUploadSize: mb * 5,
MaxMemory: mb * 5,
},
)
srv.SetQueryCache(lru.New[*ast.QueryDocument](1000))
srv.Use(extension.AutomaticPersistedQuery{Cache: lru.New[string](1000)})
if config.IntrospectionEnabled {
srv.Use(extension.Introspection{})
} else {
srv.SetDisableSuggestion(true)
}
srv.Use(extension.FixedComplexityLimit(config.ComplexityLimit))
if params.LoadersInitializer != nil {
srv.Use(params.LoadersInitializer)
}
srv.Use(otelgqlgen.Middleware())
if config.TracingEnabled {
srv.Use(apollotracing.Tracer{})
}
srv.SetRecoverFunc(
func(ctx context.Context, p any) error {
return fmt.Errorf("panic: %v", p)
},
)
srv.SetErrorPresenter(
params.ErrorPresenter,
)
return srv
}
type ErrorPresenterFactory interface {
NewErrorPresenter() graphql.ErrorPresenterFunc
}
func NewErrorPresenter(params ErrorPresenterParams) graphql.ErrorPresenterFunc {
return func(ctx context.Context, err error) *gqlerror.Error {
var gqlErr *gqlerror.Error
path := graphql.GetPath(ctx)
if errors.As(err, &gqlErr) {
if gqlErr.Path == nil {
gqlErr.Path = path
} else {
path = gqlErr.Path
}
originalErr := gqlErr.Unwrap()
if originalErr == nil {
return gqlErr
}
err = originalErr
}
config := params.Config
if params.ErrorPipeline != nil {
err = params.ErrorPipeline.Process(ctx, err)
}
code := err.Error()
message := infraErrors.Hint(err)
if message == "" {
message = code
}
extra := make(map[string]any)
meta := infraErrors.Meta(err)
if meta != nil {
extra["meta"] = infraErrors.Meta(err)
}
if config.ReturnCause {
cause := infraErrors.Cause(err)
if cause != nil {
causeMap := map[string]interface{}{
"code": cause.Error(),
}
hint := infraErrors.Hint(cause)
if hint != "" {
causeMap["message"] = hint
}
metaCause := infraErrors.Meta(cause)
if metaCause != nil {
causeMap["meta"] = infraErrors.Meta(cause)
}
extra["cause"] = causeMap
}
}
extra["code"] = code
return &gqlerror.Error{
Message: message,
Path: path,
Extensions: extra,
}
}
}