-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
148 lines (127 loc) · 5.08 KB
/
types.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
package connector
import (
"context"
"log/slog"
"github.com/hasura/ndc-sdk-go/schema"
)
// Connector abstracts an interface with required methods for the [NDC Specification].
//
// [NDC Specification]: https://hasura.github.io/ndc-spec/specification/index.html
type Connector[Configuration any, State any] interface {
// ParseConfiguration validates the configuration files provided by the user, returning a validated 'Configuration',
// or throwing an error to prevents Connector startup.
ParseConfiguration(ctx context.Context, configurationDir string) (*Configuration, error)
// TryInitState initializes the connector's in-memory state.
//
// For example, any connection pools, prepared queries,
// or other managed resources would be allocated here.
//
// In addition, this function should register any
// connector-specific metrics with the metrics registry.
TryInitState(ctx context.Context, configuration *Configuration, metrics *TelemetryState) (*State, error)
// HealthCheck checks the health of the connector.
//
// For example, this function should check that the connector
// is able to reach its data source over the network.
//
// Should throw if the check fails, else resolve.
HealthCheck(ctx context.Context, configuration *Configuration, state *State) error
// GetCapabilities get the connector's capabilities.
//
// This function implements the [capabilities endpoint] from the NDC specification.
//
// This function should be synchronous.
//
// [capabilities endpoint]: https://hasura.github.io/ndc-spec/specification/capabilities.html
GetCapabilities(configuration *Configuration) schema.CapabilitiesResponseMarshaler
// GetSchema gets the connector's schema.
//
// This function implements the [schema endpoint] from the NDC specification.
//
// [schema endpoint]: https://hasura.github.io/ndc-spec/specification/schema/index.html
GetSchema(ctx context.Context, configuration *Configuration, state *State) (schema.SchemaResponseMarshaler, error)
// QueryExplain explains a query by creating an execution plan.
// This function implements the [explain endpoint] from the NDC specification.
//
// [explain endpoint]: https://hasura.github.io/ndc-spec/specification/explain.html
QueryExplain(ctx context.Context, configuration *Configuration, state *State, request *schema.QueryRequest) (*schema.ExplainResponse, error)
// MutationExplain explains a mutation by creating an execution plan.
// This function implements the [explain endpoint] from the NDC specification.
//
// [explain endpoint]: https://hasura.github.io/ndc-spec/specification/explain.html
MutationExplain(ctx context.Context, configuration *Configuration, state *State, request *schema.MutationRequest) (*schema.ExplainResponse, error)
// Mutation executes a mutation.
//
// This function implements the [mutation endpoint] from the NDC specification.
//
// [mutation endpoint]: https://hasura.github.io/ndc-spec/specification/mutations/index.html
Mutation(ctx context.Context, configuration *Configuration, state *State, request *schema.MutationRequest) (*schema.MutationResponse, error)
// Query executes a query.
//
// This function implements the [query endpoint] from the NDC specification.
//
// [query endpoint]: https://hasura.github.io/ndc-spec/specification/queries/index.html
Query(ctx context.Context, configuration *Configuration, state *State, request *schema.QueryRequest) (schema.QueryResponse, error)
}
// the common serve options for the server.
type serveOptions struct {
logger *slog.Logger
logLevel slog.Level
metricsPrefix string
version string
serviceName string
withoutConfig bool
withoutRecovery bool
}
func defaultServeOptions() *serveOptions {
return &serveOptions{
logger: slog.Default(),
serviceName: "hasura-ndc-go",
version: "0.1.0",
withoutConfig: false,
withoutRecovery: false,
}
}
// ServeOption abstracts a public interface to update server options.
type ServeOption func(*serveOptions)
// WithLogger sets a custom logger option.
func WithLogger(logger *slog.Logger) ServeOption {
return func(so *serveOptions) {
so.logger = logger
}
}
// WithLoggerFunc sets a custom logger option with a constructor function.
func WithLoggerFunc(fn func(level slog.Level) *slog.Logger) ServeOption {
return func(so *serveOptions) {
so.logger = fn(so.logLevel)
}
}
// WithMetricsPrefix sets the custom metrics prefix option.
func WithMetricsPrefix(prefix string) ServeOption {
return func(so *serveOptions) {
so.metricsPrefix = prefix
}
}
// WithVersion sets the custom version option.
func WithVersion(version string) ServeOption {
return func(so *serveOptions) {
so.version = version
}
}
// WithDefaultServiceName sets the default service name option.
func WithDefaultServiceName(name string) ServeOption {
return func(so *serveOptions) {
so.serviceName = name
}
}
// WithoutRecovery disables recovery on panic.
func WithoutRecovery() ServeOption {
return func(so *serveOptions) {
so.withoutRecovery = true
}
}
func withLogLevel(level slog.Level) ServeOption {
return func(so *serveOptions) {
so.logLevel = level
}
}