-
Notifications
You must be signed in to change notification settings - Fork 557
/
moddeps.go
241 lines (213 loc) · 6.79 KB
/
moddeps.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package core
import (
"context"
"encoding/json"
"fmt"
"sync"
"github.com/dagger/dagger/cmd/codegen/introspection"
"github.com/dagger/dagger/dagql"
dagintro "github.com/dagger/dagger/dagql/introspection"
"github.com/dagger/dagger/telemetry"
)
const (
modMetaDirPath = "/.daggermod"
modMetaOutputPath = "output.json"
ModuleName = "daggercore"
)
// We don't expose these types to modules SDK codegen, but
// we still want their graphql schemas to be available for
// internal usage. So we use this list to scrub them from
// the introspection JSON that module SDKs use for codegen.
var typesHiddenFromModuleSDKs = []dagql.Typed{
&Host{},
}
/*
ModDeps represents a set of dependencies for a module or for a caller depending on a
particular set of modules to be served.
*/
type ModDeps struct {
root *Query
Mods []Mod // TODO hide
// should not be read directly, call Schema and SchemaIntrospectionJSON instead
lazilyLoadedSchema *dagql.Server
lazilyLoadedIntrospectionJSON string
loadSchemaErr error
loadSchemaLock sync.Mutex
}
func NewModDeps(root *Query, mods []Mod) *ModDeps {
return &ModDeps{
root: root,
Mods: mods,
}
}
func (d *ModDeps) Prepend(mods ...Mod) *ModDeps {
deps := append([]Mod{}, mods...)
deps = append(deps, d.Mods...)
return NewModDeps(d.root, deps)
}
func (d *ModDeps) Append(mods ...Mod) *ModDeps {
deps := append([]Mod{}, d.Mods...)
deps = append(deps, mods...)
return NewModDeps(d.root, deps)
}
// The combined schema exposed by each mod in this set of dependencies
func (d *ModDeps) Schema(ctx context.Context) (*dagql.Server, error) {
schema, _, err := d.lazilyLoadSchema(ctx)
if err != nil {
return nil, err
}
return schema, nil
}
// The introspection json for combined schema exposed by each mod in this set of dependencies
func (d *ModDeps) SchemaIntrospectionJSON(ctx context.Context, forModule bool) (string, error) {
_, introspectionJSON, err := d.lazilyLoadSchema(ctx)
if err != nil {
return "", err
}
if !forModule {
return introspectionJSON, nil
}
var introspection introspection.Response
if err := json.Unmarshal([]byte(introspectionJSON), &introspection); err != nil {
return "", fmt.Errorf("failed to unmarshal introspection JSON: %w", err)
}
for _, typed := range typesHiddenFromModuleSDKs {
introspection.Schema.ScrubType(typed.Type().Name())
introspection.Schema.ScrubType(dagql.IDTypeNameFor(typed))
}
bs, err := json.Marshal(introspection)
if err != nil {
return "", fmt.Errorf("failed to marshal introspection JSON: %w", err)
}
return string(bs), nil
}
// All the TypeDefs exposed by this set of dependencies
func (d *ModDeps) TypeDefs(ctx context.Context) ([]*TypeDef, error) {
var typeDefs []*TypeDef
for _, mod := range d.Mods {
modTypeDefs, err := mod.TypeDefs(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get objects from mod %q: %w", mod.Name(), err)
}
typeDefs = append(typeDefs, modTypeDefs...)
}
return typeDefs, nil
}
func schemaIntrospectionJSON(ctx context.Context, dag *dagql.Server) (json.RawMessage, error) {
data, err := dag.Query(ctx, introspection.Query, nil)
if err != nil {
return nil, fmt.Errorf("introspection query failed: %w", err)
}
jsonBytes, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("failed to marshal introspection result: %w", err)
}
return json.RawMessage(jsonBytes), nil
}
func (d *ModDeps) lazilyLoadSchema(ctx context.Context) (loadedSchema *dagql.Server, loadedIntrospectionJSON string, rerr error) {
d.loadSchemaLock.Lock()
defer d.loadSchemaLock.Unlock()
if d.lazilyLoadedSchema != nil {
return d.lazilyLoadedSchema, d.lazilyLoadedIntrospectionJSON, nil
}
if d.loadSchemaErr != nil {
return nil, "", d.loadSchemaErr
}
defer func() {
d.lazilyLoadedSchema = loadedSchema
d.lazilyLoadedIntrospectionJSON = loadedIntrospectionJSON
d.loadSchemaErr = rerr
}()
dag := dagql.NewServer[*Query](d.root)
dag.Around(telemetry.AroundFunc)
// share the same cache session-wide
dag.Cache = d.root.Cache
dagintro.Install[*Query](dag)
var objects []*ModuleObjectType
var ifaces []*InterfaceType
for _, mod := range d.Mods {
err := mod.Install(ctx, dag)
if err != nil {
return nil, "", fmt.Errorf("failed to get schema for module %q: %w", mod.Name(), err)
}
// TODO support core interfaces types
if userMod, ok := mod.(*Module); ok {
defs, err := mod.TypeDefs(ctx)
if err != nil {
return nil, "", fmt.Errorf("failed to get type defs for module %q: %w", mod.Name(), err)
}
for _, def := range defs {
switch def.Kind {
case TypeDefKindObject:
objects = append(objects, &ModuleObjectType{
typeDef: def.AsObject.Value,
mod: userMod,
})
case TypeDefKindInterface:
ifaces = append(ifaces, &InterfaceType{
typeDef: def.AsInterface.Value,
mod: userMod,
})
}
}
}
}
// add any extensions to objects for the interfaces they implement (if any)
for _, objType := range objects {
obj := objType.typeDef
class, found := dag.ObjectType(obj.Name)
if !found {
return nil, "", fmt.Errorf("failed to find object %q in schema", obj.Name)
}
for _, ifaceType := range ifaces {
iface := ifaceType.typeDef
if !obj.IsSubtypeOf(iface) {
continue
}
objType := objType
ifaceType := ifaceType
asIfaceFieldName := gqlFieldName(fmt.Sprintf("as%s", iface.Name))
class.Extend(
dagql.FieldSpec{
Name: asIfaceFieldName,
Description: fmt.Sprintf("Converts this %s to a %s.", obj.Name, iface.Name),
Type: &InterfaceAnnotatedValue{TypeDef: iface},
Module: ifaceType.mod.IDModule(),
},
func(ctx context.Context, self dagql.Object, args map[string]dagql.Input) (dagql.Typed, error) {
inst, ok := self.(dagql.Instance[*ModuleObject])
if !ok {
return nil, fmt.Errorf("expected %T to be a ModuleObject", self)
}
return &InterfaceAnnotatedValue{
TypeDef: iface,
Fields: inst.Self.Fields,
UnderlyingType: objType,
IfaceType: ifaceType,
}, nil
},
)
}
}
introspectionJSON, err := schemaIntrospectionJSON(ctx, dag)
if err != nil {
return nil, "", fmt.Errorf("failed to get schema introspection JSON: %w", err)
}
return dag, string(introspectionJSON), nil
}
// Search the deps for the given type def, returning the ModType if found. This does not recurse
// to transitive dependencies; it only returns types directly exposed by the schema of the top-level
// deps.
func (d *ModDeps) ModTypeFor(ctx context.Context, typeDef *TypeDef) (ModType, bool, error) {
for _, mod := range d.Mods {
modType, ok, err := mod.ModTypeFor(ctx, typeDef, false)
if err != nil {
return nil, false, fmt.Errorf("failed to get type from mod %q: %w", mod.Name(), err)
}
if !ok {
continue
}
return modType, true, nil
}
return nil, false, nil
}