Skip to content

feat: make module types id-able - #6102

Merged
sipsma merged 3 commits into
dagger:mainfrom
jedevc:module-id-types
Nov 20, 2023
Merged

sipsma merged 3 commits into
dagger:mainfrom
jedevc:module-id-types

Conversation

@jedevc

@jedevc jedevc commented Nov 13, 2023

Copy link
Copy Markdown
Contributor

⚠️ Depends on #6057 (marking as draft until that one is merged)

This feature allows all module declared objects to be converted to a respective ID-type using the "id" field, and converted from an ID-type using the "loadFromID" helper. This effectively makes these types fully idable.

IDs are essentially the contents of the module type's data, JSON-encoded, and then base64-encoded, similar (but not identical) to standard IDs. At the moment, they don't share exactly the same codepaths to normal modules, but this could be adapted to be so in the future. We also include a note of what type generated this ID - this is so we can prevent type confusion.

Essentially, this enables a number of previously impossible use cases:

  1. Returning a custom object from another module from a function

    • This case is the simplest - we just extend our conversion that unpacks all returned IDs from functions into their underlying types (so while a module returns an XID, it can be queried as if it was X).
  2. Accepting a custom object as a parameter to a function

    • Because graphql doesn't support creating inline objects, we get around this in the core api by instead passing IDs everywhere. All the code-gen is setup to expect this, but module types weren't IDable, so we failed during schema-checking.

      Additionally, we need to transparently convert the ID from the graphql request into the underlying storage data for the module.

  3. Accepting a custom object from another module as a parameter to a function

    • This is the same case as above, but also involves ensuring that we stitch in the type declarations from the module into our own schema so that we have the right type.

      However, here, we don't decode the ID into its underlying data, it's a foreign type, so the module should only see the ID.

So, I'm not actually sure about the actual method by which this encodes the module data into the ID - I think this is the right approach? It's pretty much the easiest way to store the state, but we could also randomly assign IDs and simply cache the results (potentially useful if the IDs start getting really long), or we could do something similar to the core API and just store the parents (though I'm not 100% sure how this would work, I think this might be the thing that needs IDs v2?).

@jedevc
jedevc requested review from sipsma and vito November 13, 2023 16:48
@jedevc
jedevc marked this pull request as draft November 13, 2023 16:52
@jedevc
jedevc force-pushed the module-id-types branch 2 times, most recently from 6487d81 to 8ffb343 Compare November 14, 2023 12:52
@jedevc
jedevc marked this pull request as ready for review November 15, 2023 06:11
@sipsma

sipsma commented Nov 15, 2023

Copy link
Copy Markdown
Contributor

Justin is on vacation so I'm working on a few updates.

One important implication to all this is that proper support for other module objects as inputs + outputs means that we do need to selectively include transitive deps in the schema presented to callers.

So if you have a chain of module deps A->B->C ("->" meaning "depends on") like:

A:

func (m *A) Fn() ObjB

B:

func (m *B) Fn() ObjB

type ObjB struct {}
func (o *ObjB) FnB() ObjC

C:

func (m *C) Fn() ObjC

type ObjC struct {}

Then a caller who loads module A also needs schema loaded for module B, which in turn needs schema loaded for module C.

This is worth explicitly noting since it does diverge from the previous rule that when you load a given module you only got its schema. This makes the problem of conflicting versions of the same module in the dependency DAG arise again, but only in certain situations.

I don't see any obvious ways of avoiding that unless we prevented modules from using another module's types as input/output, which isn't viable. So we'll just need to accept that going forward and deal with it as needed.

That being said, I am working on an update to this code so that we only include a dep in the schema presented to callers if that dep actually appears as an input/output/field. This at least allows us to avoid including deps when they are only used in the implementation of functions, which I'd suspect probably ends up being the majority of the cases.

In theory we could probably go even further and trace the precise parts of dep schemas that the caller actually needs (any "path" that can lead to the types referred to by the depender in input/output/field), but I think the complication of doing so greatly outweighs the marginal reward, so not pursuing that right now.

cc @vito

@vito

vito commented Nov 15, 2023

Copy link
Copy Markdown
Contributor

@sipsma Good point, definitely worth surfacing. It seems like a reasonable and expected outcome, really; if your module directly exposes another module's types, it becomes part of your external contract too.

I think this would be much scarier in the previous iteration where modules could extend other types, but right now it seems under control. Just something to keep in mind if/when we consider bringing that back in some form.

@sipsma

sipsma commented Nov 15, 2023

Copy link
Copy Markdown
Contributor

@vito after writing more code and thinking through I'm realizing this actually has some very immediate implications in non-obscure cases.

If you bear with me a sec, would appreciate a double-check that I'm not overthinking or missing anything obvious 🙂


Example of the problem:

B and C are two separate modules:

B:

type B struct{}
func (b *B) WithObjD(d *ObjD) *B {}

C:

type C struct{}
func (c *C) WithObjD(d *ObjD) *C {}

ObjD is an object from another module D that both B and C depend on

So far so good. But now it turns out that B depends on version 1.0 of D and C depends on version 2.0 of D.

  • We obviously don't actually have semver yet, just using this as shorthand for incompatible versions of module D

This creates problems. Say you want to now write a module A that depends on both B and C:

type A struct{}
func (a *A) Fn() error {
  d := dag.D().GetObjD("abc", 123)
  c := dag.C().WithObjD(d)
  b := dag.B().WithObjD(d)
  // ...
}

The code for A's Fn is constructing an ObjD and then giving it to both B and C. A can only have a dependency on one version of D, but B and C depend on different versions of D.

If the different versions of D are major (in semver terms) then things can break fairly catastrophically. We'll be giving versions of ObjD that either B or C may not be able to load as expected and get undefined behavior.


This is my current best idea for a short term solution:

  1. (mentioned in my previous comment) avoid the problem entirely when a dep is only used in the implementation rather than appears in the type signature of a module.
  2. When creating a schema to be served to a caller, detect if there's two objects with different sets of fields that would both need to appear in the schema. Error out if so.
    • I think it's only strictly crucial to check the sets of fields since that's what impacts the ID of the module object, which is the main source of potential catastrophic undefined behavior

We would not be handling every case here yet, but I think it would at least cover the worst case ones? While still enabling this (pretty important) feature and not forcing everyone writing modules to get every single one of their deps all aligned on the exact same version (i.e. git commit) of a module.

In the longer term, I think the solution should be at least actual semver for modules, including both auto-detection of breaking changes to the schema and manual bumping of major version in case the implementation changes in some breaking way. I know you were thinking about that already @vito.

I'm hoping we nonetheless can still keep this problem relatively simple and not have to go all the way to the NP-Complete problem 😅 (https://research.swtch.com/version-sat).

cc @helderco @shykes since there's some potentially wide-ranging implications here. IIRC Helder I think you did a lot of research into these sorts of issues back in the CUE days (?), so appreciate any insights you may have from that if so.

@vito

vito commented Nov 15, 2023 •

Copy link
Copy Markdown
Contributor

@sipsma Oof, yeah that's complicated.

Since it feels like we're slipping down a slope leading to General Purpose Programming Language Problems, I want to play devil's advocate on this point:

I don't see any obvious ways of avoiding that unless we prevented modules from using another module's types as input/output, which isn't viable. So we'll just need to accept that going forward and deal with it as needed.

You mention this isn't viable, but I want to pretend it is: what if we had the constraint that modules can use other modules, but they can only return their own types and/or core types? i.e., encapsulate or bust

Do you have a killer example of a module needing to accept/return another module's types? Aside from the fact that we've considered making the core types themselves a "core" module?

I also want to point out that most of the time even in Go you're not using modules that are so tightly coupled together that they directly consume or return another module's types. I'm kind of going out on a limb here; there are probably a ton of counterexamples, but it's not really apples-to-apples, and it still feels right. 🤔

The reason I ask is it feels like you can already get quite far by relying on the core types (containers, files, directories) combined with whatever programming language libraries/dependencies you want to use natively. I'm worried that we're pulling on a thread that will just bring all the complexity in the "high-fidelity" languages into a world that can otherwise be "low-fidelity" glue code. I'm wondering if there should be conscious limitations like this to keep it from becoming twice the mental overhead. Don't want get too preoccupied with whether we could, before wondering whether we should. :P

To be clear, making module types ID-able would still make sense in this world, since it a module's API might want to accept its own types as inputs.

@sipsma

sipsma commented Nov 15, 2023

Copy link
Copy Markdown
Contributor

Since it feels like we're slipping down a slope leading to General Purpose Programming Language Problems, I want to play devil's advocate on this point:

Yeah this has been bouncing around in my head too. I was attempting to avoid it because:

  1. Like you said, we'd like core to be a versioned module someday too. At which point we're back to the same problem.
  2. Forcing users to wrap everything that shows up in a type signature is one of those things that leaks implementation concerns up to the actual UX/DX. Like a user would reasonably ask "why can't I just use the type I want to accept/return" and the only explanation we could give is an insane rant about diamond dependency graphs with incompatible major version (slightly exaggerating here, but essentially true). To the extent possible, they shouldn't have to care about things like that.

But yeah, I was probably avoiding it a bit too hard; it should definitely be on the table.


Thinking about it some more, I suppose another option would be to automatically do the encapsulation for the user. So if module B puts ObjD in a type signature, someone depending on B would have to call dag.B().GetObjD()...

I don't like that... but it would technically solve the problem I think. Maybe there's something along these lines that's good...?

@sipsma

sipsma commented Nov 15, 2023

Copy link
Copy Markdown
Contributor

@vito I talked with @aweris about his use case for using objects from other modules as input/output of Functions, was super helpful.

It made me realize that his use case and most likely many others could also be satisfied by adding support for interfaces (both on the graphql schema level and in SDKs).

In his case he wants to compose different modules in a pattern like:

dag.Gale().WithStep(dag.Foo().Step()).WithStep(dag.Bar().Step())

where Gale, Foo and Bar are all different modules, but Foo and Bar return a common Step type that is originally defined in Gale.

For a lot of use cases, it makes total sense to just use core types like Container/Directory/etc. as the common data type, but for his and many others trying to coerce structured data into one of those just becomes a mess+hack.

If the WithStep function could accept an interface representing "anything that looks like a Step" as opposed to just the literal Step object as defined in Gale, then I think we can avoid needing to add support for out-of-module-objects as inputs+outputs and the associated version dependency hell problems (for now).

  • Basically, Foo and Bar would just return a object that has a some set of fields, but it would be their own object, not Gale's Step object. Then when a caller tries to pass them to WithStep, they'd get a compilation error if it doesn't match the interface
  • To be clear, this is absolutely a compromise; there would still be advantages if Foo and Bar could just return Gale's Step object, but I think it may be "good enough" given the tradeoffs

I need to give this one more round of thought to verify it's sound (and please chime in if anyone has thoughts), but if so my plan will be:

  1. Update this PR to remove the support for out-of-module objects as input/output/field. We'll still have general object ID-ability and the new ability to use your own objects as inputs to functions.
  2. As a follow-up, try out interface support; at a minimum get a sense of how large an effort it would be (and hopefully just implement it)
    • I think we can safely scope it to just Go+Python at the moment, which is much more manageable than "every SDK"
    • Interface support has also come up a ton of times in all sorts of different contexts, so it's not like it would just be for this one use case; it should reap lots of benefits in the longer haul too.

@vito

vito commented Nov 15, 2023

Copy link
Copy Markdown
Contributor

@sipsma Love that idea - my mind also went to interfaces, but I put the thought on hold when I noticed GraphQL interfaces aren't supported as inputs (which makes sense - objects in general aren't). But if we find a way to figure it out, I really like the semantics of accepting + returning interfaces instead of concrete types.

@sipsma

sipsma commented Nov 16, 2023

Copy link
Copy Markdown
Contributor

But if we find a way to figure it out, I really like the semantics of accepting + returning interfaces instead of concrete types.

Yeah we'll have to fit this into the ID system, similar to how we don't use input types. Not 100% sure yet but taking inspiration from the current approach maybe we'll need a field under query for each interface like:

loadFooInterfaceFromID(id: String!) FooInterface!

Where it accepts any object's ID and will return the interface representation of it if the object matches that interface, error otherwise. I think we have most of the scaffolding in place to support that at this point so hopefully shouldn't be too huge a wrench 🤞

@sipsma

sipsma commented Nov 17, 2023

Copy link
Copy Markdown
Contributor

Pushed an additional commit w/ the aforementioned changes here: 2027bc9

@sipsma
sipsma requested review from helderco and vito and removed request for vito November 17, 2023 21:51

@vito vito left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple nitpicks, otherwise LGTM!

Comment thread core/schema/module.go Outdated
Comment on lines 1314 to 1318

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Seems like this sanity check should happen in DecodeModule for consistency with (ID).Decode.

Suggested change
typeName, value, err := resourceid.DecodeModule(id)
if err != nil {
return a, nil
}
if typeName != typeDef.AsObject.Name {
// if we hit this, the module is misbehaving - it's
// declared it returns a different module type than it's
// actually returned
return nil, fmt.Errorf("invalid type name %q, expected %q", typeName, typeDef.AsObject.Name)
}
return value, nil
return resourceid.DecodeModule(id, typeDef.AsObject.Name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good call (here and related comment below), made this update.

It also got rid of a case where we ignored the error from resourceid.DecodeModule, we just always return an error from that if received here now. AFAICT that's the desirable behavior and the tests pass, so think we're good to go.

Comment thread core/resourceid/module_id.go Outdated

@vito vito Nov 20, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: These names feel a little confusing, since they sound like they encode/decode the module itself, but they actually encode/decode a typed object from a module.

Maybe something like this? (building on previous comment)

Suggested change
func DecodeModule(rest string) (string, any, error) {
func DecodeModuleID(id string, expectedType string) (any, error) {

(and EncodeModuleID in turn, not so picky about what we call it, I could see DecodeModuleObject or something too)

jedevc and others added 3 commits November 20, 2023 09:59
This feature allows all module declared objects to be converted to a
respective ID-type using the "id" field, and converted from an ID-type
using the "loadFromID" helper. This effectively makes these types
fully idable.

IDs are essentially the contents of the module type's data,
JSON-encoded, and then base64-encoded, similar (but not identical) to
standard IDs. At the moment, they don't share exactly the same codepaths
to normal modules, but this could be adapted to be so in the future. We
also include a note of *what type* generated this ID - this is so we can
prevent type confusion.

Essentially, this enables a number of previously impossible use cases:

1. Returning a custom object from another module from a function

   - This case is the simplest - we just extend our conversion that
     unpacks all returned IDs from functions into their underlying types
     (so while a module returns an XID, it can be queried as if it was X).

2. Accepting a custom object as a parameter to a function

   - Because graphql doesn't support creating inline objects, we get
     around this in the core api by instead passing IDs everywhere. All
     the code-gen is setup to expect this, but module types weren't
     IDable, so we failed during schema-checking.

     Additionally, we need to transparently convert the ID from the
     graphql request into the underlying storage data for the module.

3. Accepting a custom object from another module as a parameter to a
   function

   - This is the same case as above, but also involves ensuring that we
     stitch in the type declarations from the module into our own schema
     so that we have the right type.

     However, here, we **don't** decode the ID into its underlying
     data, it's a foreign type, so the module should only see the ID.

Signed-off-by: Justin Chadwell <[email protected]>
While we now give all module objects ID-ability and enable a module to
accept its own objects as function inputs, we are not yet going to allow
modules to use objects from other (non-core) modules as function args,
function returns or object fields.

This is because allowing that results in a caller who depends on a given
module A to now not only have access to module A's types but also the
types of transitive dependencies. This in turn opens the doors to
dependency hell; if two versions of a given module show up multiple
times in the full dependency DAG we need some fairly complex dependency
resolution logic around which of those versions is exposed to the
caller.

That problem is famously difficult and while we may need to deal with it
someday, it's preferable to avoid it for as long as possible.

The idea is to instead add support for interfaces in follow ups, which
should unblock many of the use cases that call for interoperability
between different module types.

The main changes in this commit are to detect when a user tries to do
this and give them a descriptive error message in that case. Other
related changes are:
* an additional integ test case for the situation where there's
  conflicting versions of a give module in the transitive dependency
  DAG, which is expected to continue working.
* tracking of which module a given schema type was loaded from, which
  allows us to distinguish between use of a type from core (which is
  still allowed) and a type from a dependency module.
* removal of "Dependencies" from the ExecutableSchema interface we use
  internally, which overlapped with some of the other changes here and
  was completely unused (leftover from ancient days during the Cloak
  project).

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma merged commit 2a6342c into dagger:main Nov 20, 2023
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* feat: make module types id-able

This feature allows all module declared objects to be converted to a
respective ID-type using the "id" field, and converted from an ID-type
using the "loadFromID" helper. This effectively makes these types
fully idable.

IDs are essentially the contents of the module type's data,
JSON-encoded, and then base64-encoded, similar (but not identical) to
standard IDs. At the moment, they don't share exactly the same codepaths
to normal modules, but this could be adapted to be so in the future. We
also include a note of *what type* generated this ID - this is so we can
prevent type confusion.

Essentially, this enables a number of previously impossible use cases:

1. Returning a custom object from another module from a function

   - This case is the simplest - we just extend our conversion that
     unpacks all returned IDs from functions into their underlying types
     (so while a module returns an XID, it can be queried as if it was X).

2. Accepting a custom object as a parameter to a function

   - Because graphql doesn't support creating inline objects, we get
     around this in the core api by instead passing IDs everywhere. All
     the code-gen is setup to expect this, but module types weren't
     IDable, so we failed during schema-checking.

     Additionally, we need to transparently convert the ID from the
     graphql request into the underlying storage data for the module.

3. Accepting a custom object from another module as a parameter to a
   function

   - This is the same case as above, but also involves ensuring that we
     stitch in the type declarations from the module into our own schema
     so that we have the right type.

     However, here, we **don't** decode the ID into its underlying
     data, it's a foreign type, so the module should only see the ID.

Signed-off-by: Justin Chadwell <[email protected]>

* disallow use of external module types in function+field signatures

While we now give all module objects ID-ability and enable a module to
accept its own objects as function inputs, we are not yet going to allow
modules to use objects from other (non-core) modules as function args,
function returns or object fields.

This is because allowing that results in a caller who depends on a given
module A to now not only have access to module A's types but also the
types of transitive dependencies. This in turn opens the doors to
dependency hell; if two versions of a given module show up multiple
times in the full dependency DAG we need some fairly complex dependency
resolution logic around which of those versions is exposed to the
caller.

That problem is famously difficult and while we may need to deal with it
someday, it's preferable to avoid it for as long as possible.

The idea is to instead add support for interfaces in follow ups, which
should unblock many of the use cases that call for interoperability
between different module types.

The main changes in this commit are to detect when a user tries to do
this and give them a descriptive error message in that case. Other
related changes are:
* an additional integ test case for the situation where there's
  conflicting versions of a give module in the transitive dependency
  DAG, which is expected to continue working.
* tracking of which module a given schema type was loaded from, which
  allows us to distinguish between use of a type from core (which is
  still allowed) and a type from a dependency module.
* removal of "Dependencies" from the ExecutableSchema interface we use
  internally, which overlapped with some of the other changes here and
  was completely unused (leftover from ancient days during the Cloak
  project).

Signed-off-by: Erik Sipsma <[email protected]>

* check expected module type name direct in decode

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Co-authored-by: Erik Sipsma <[email protected]>
Signed-off-by: Christian Schlatter <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* feat: make module types id-able

This feature allows all module declared objects to be converted to a
respective ID-type using the "id" field, and converted from an ID-type
using the "loadFromID" helper. This effectively makes these types
fully idable.

IDs are essentially the contents of the module type's data,
JSON-encoded, and then base64-encoded, similar (but not identical) to
standard IDs. At the moment, they don't share exactly the same codepaths
to normal modules, but this could be adapted to be so in the future. We
also include a note of *what type* generated this ID - this is so we can
prevent type confusion.

Essentially, this enables a number of previously impossible use cases:

1. Returning a custom object from another module from a function

   - This case is the simplest - we just extend our conversion that
     unpacks all returned IDs from functions into their underlying types
     (so while a module returns an XID, it can be queried as if it was X).

2. Accepting a custom object as a parameter to a function

   - Because graphql doesn't support creating inline objects, we get
     around this in the core api by instead passing IDs everywhere. All
     the code-gen is setup to expect this, but module types weren't
     IDable, so we failed during schema-checking.

     Additionally, we need to transparently convert the ID from the
     graphql request into the underlying storage data for the module.

3. Accepting a custom object from another module as a parameter to a
   function

   - This is the same case as above, but also involves ensuring that we
     stitch in the type declarations from the module into our own schema
     so that we have the right type.

     However, here, we **don't** decode the ID into its underlying
     data, it's a foreign type, so the module should only see the ID.

Signed-off-by: Justin Chadwell <[email protected]>

* disallow use of external module types in function+field signatures

While we now give all module objects ID-ability and enable a module to
accept its own objects as function inputs, we are not yet going to allow
modules to use objects from other (non-core) modules as function args,
function returns or object fields.

This is because allowing that results in a caller who depends on a given
module A to now not only have access to module A's types but also the
types of transitive dependencies. This in turn opens the doors to
dependency hell; if two versions of a given module show up multiple
times in the full dependency DAG we need some fairly complex dependency
resolution logic around which of those versions is exposed to the
caller.

That problem is famously difficult and while we may need to deal with it
someday, it's preferable to avoid it for as long as possible.

The idea is to instead add support for interfaces in follow ups, which
should unblock many of the use cases that call for interoperability
between different module types.

The main changes in this commit are to detect when a user tries to do
this and give them a descriptive error message in that case. Other
related changes are:
* an additional integ test case for the situation where there's
  conflicting versions of a give module in the transitive dependency
  DAG, which is expected to continue working.
* tracking of which module a given schema type was loaded from, which
  allows us to distinguish between use of a type from core (which is
  still allowed) and a type from a dependency module.
* removal of "Dependencies" from the ExecutableSchema interface we use
  internally, which overlapped with some of the other changes here and
  was completely unused (leftover from ancient days during the Cloak
  project).

Signed-off-by: Erik Sipsma <[email protected]>

* check expected module type name direct in decode

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Co-authored-by: Erik Sipsma <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* feat: make module types id-able

This feature allows all module declared objects to be converted to a
respective ID-type using the "id" field, and converted from an ID-type
using the "loadFromID" helper. This effectively makes these types
fully idable.

IDs are essentially the contents of the module type's data,
JSON-encoded, and then base64-encoded, similar (but not identical) to
standard IDs. At the moment, they don't share exactly the same codepaths
to normal modules, but this could be adapted to be so in the future. We
also include a note of *what type* generated this ID - this is so we can
prevent type confusion.

Essentially, this enables a number of previously impossible use cases:

1. Returning a custom object from another module from a function

   - This case is the simplest - we just extend our conversion that
     unpacks all returned IDs from functions into their underlying types
     (so while a module returns an XID, it can be queried as if it was X).

2. Accepting a custom object as a parameter to a function

   - Because graphql doesn't support creating inline objects, we get
     around this in the core api by instead passing IDs everywhere. All
     the code-gen is setup to expect this, but module types weren't
     IDable, so we failed during schema-checking.

     Additionally, we need to transparently convert the ID from the
     graphql request into the underlying storage data for the module.

3. Accepting a custom object from another module as a parameter to a
   function

   - This is the same case as above, but also involves ensuring that we
     stitch in the type declarations from the module into our own schema
     so that we have the right type.

     However, here, we **don't** decode the ID into its underlying
     data, it's a foreign type, so the module should only see the ID.

Signed-off-by: Justin Chadwell <[email protected]>

* disallow use of external module types in function+field signatures

While we now give all module objects ID-ability and enable a module to
accept its own objects as function inputs, we are not yet going to allow
modules to use objects from other (non-core) modules as function args,
function returns or object fields.

This is because allowing that results in a caller who depends on a given
module A to now not only have access to module A's types but also the
types of transitive dependencies. This in turn opens the doors to
dependency hell; if two versions of a given module show up multiple
times in the full dependency DAG we need some fairly complex dependency
resolution logic around which of those versions is exposed to the
caller.

That problem is famously difficult and while we may need to deal with it
someday, it's preferable to avoid it for as long as possible.

The idea is to instead add support for interfaces in follow ups, which
should unblock many of the use cases that call for interoperability
between different module types.

The main changes in this commit are to detect when a user tries to do
this and give them a descriptive error message in that case. Other
related changes are:
* an additional integ test case for the situation where there's
  conflicting versions of a give module in the transitive dependency
  DAG, which is expected to continue working.
* tracking of which module a given schema type was loaded from, which
  allows us to distinguish between use of a type from core (which is
  still allowed) and a type from a dependency module.
* removal of "Dependencies" from the ExecutableSchema interface we use
  internally, which overlapped with some of the other changes here and
  was completely unused (leftover from ancient days during the Cloak
  project).

Signed-off-by: Erik Sipsma <[email protected]>

* check expected module type name direct in decode

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Co-authored-by: Erik Sipsma <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* feat: make module types id-able

This feature allows all module declared objects to be converted to a
respective ID-type using the "id" field, and converted from an ID-type
using the "loadFromID" helper. This effectively makes these types
fully idable.

IDs are essentially the contents of the module type's data,
JSON-encoded, and then base64-encoded, similar (but not identical) to
standard IDs. At the moment, they don't share exactly the same codepaths
to normal modules, but this could be adapted to be so in the future. We
also include a note of *what type* generated this ID - this is so we can
prevent type confusion.

Essentially, this enables a number of previously impossible use cases:

1. Returning a custom object from another module from a function

   - This case is the simplest - we just extend our conversion that
     unpacks all returned IDs from functions into their underlying types
     (so while a module returns an XID, it can be queried as if it was X).

2. Accepting a custom object as a parameter to a function

   - Because graphql doesn't support creating inline objects, we get
     around this in the core api by instead passing IDs everywhere. All
     the code-gen is setup to expect this, but module types weren't
     IDable, so we failed during schema-checking.

     Additionally, we need to transparently convert the ID from the
     graphql request into the underlying storage data for the module.

3. Accepting a custom object from another module as a parameter to a
   function

   - This is the same case as above, but also involves ensuring that we
     stitch in the type declarations from the module into our own schema
     so that we have the right type.

     However, here, we **don't** decode the ID into its underlying
     data, it's a foreign type, so the module should only see the ID.

Signed-off-by: Justin Chadwell <[email protected]>

* disallow use of external module types in function+field signatures

While we now give all module objects ID-ability and enable a module to
accept its own objects as function inputs, we are not yet going to allow
modules to use objects from other (non-core) modules as function args,
function returns or object fields.

This is because allowing that results in a caller who depends on a given
module A to now not only have access to module A's types but also the
types of transitive dependencies. This in turn opens the doors to
dependency hell; if two versions of a given module show up multiple
times in the full dependency DAG we need some fairly complex dependency
resolution logic around which of those versions is exposed to the
caller.

That problem is famously difficult and while we may need to deal with it
someday, it's preferable to avoid it for as long as possible.

The idea is to instead add support for interfaces in follow ups, which
should unblock many of the use cases that call for interoperability
between different module types.

The main changes in this commit are to detect when a user tries to do
this and give them a descriptive error message in that case. Other
related changes are:
* an additional integ test case for the situation where there's
  conflicting versions of a give module in the transitive dependency
  DAG, which is expected to continue working.
* tracking of which module a given schema type was loaded from, which
  allows us to distinguish between use of a type from core (which is
  still allowed) and a type from a dependency module.
* removal of "Dependencies" from the ExecutableSchema interface we use
  internally, which overlapped with some of the other changes here and
  was completely unused (leftover from ancient days during the Cloak
  project).

Signed-off-by: Erik Sipsma <[email protected]>

* check expected module type name direct in decode

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Co-authored-by: Erik Sipsma <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* feat: make module types id-able

This feature allows all module declared objects to be converted to a
respective ID-type using the "id" field, and converted from an ID-type
using the "loadFromID" helper. This effectively makes these types
fully idable.

IDs are essentially the contents of the module type's data,
JSON-encoded, and then base64-encoded, similar (but not identical) to
standard IDs. At the moment, they don't share exactly the same codepaths
to normal modules, but this could be adapted to be so in the future. We
also include a note of *what type* generated this ID - this is so we can
prevent type confusion.

Essentially, this enables a number of previously impossible use cases:

1. Returning a custom object from another module from a function

   - This case is the simplest - we just extend our conversion that
     unpacks all returned IDs from functions into their underlying types
     (so while a module returns an XID, it can be queried as if it was X).

2. Accepting a custom object as a parameter to a function

   - Because graphql doesn't support creating inline objects, we get
     around this in the core api by instead passing IDs everywhere. All
     the code-gen is setup to expect this, but module types weren't
     IDable, so we failed during schema-checking.

     Additionally, we need to transparently convert the ID from the
     graphql request into the underlying storage data for the module.

3. Accepting a custom object from another module as a parameter to a
   function

   - This is the same case as above, but also involves ensuring that we
     stitch in the type declarations from the module into our own schema
     so that we have the right type.

     However, here, we **don't** decode the ID into its underlying
     data, it's a foreign type, so the module should only see the ID.

Signed-off-by: Justin Chadwell <[email protected]>

* disallow use of external module types in function+field signatures

While we now give all module objects ID-ability and enable a module to
accept its own objects as function inputs, we are not yet going to allow
modules to use objects from other (non-core) modules as function args,
function returns or object fields.

This is because allowing that results in a caller who depends on a given
module A to now not only have access to module A's types but also the
types of transitive dependencies. This in turn opens the doors to
dependency hell; if two versions of a given module show up multiple
times in the full dependency DAG we need some fairly complex dependency
resolution logic around which of those versions is exposed to the
caller.

That problem is famously difficult and while we may need to deal with it
someday, it's preferable to avoid it for as long as possible.

The idea is to instead add support for interfaces in follow ups, which
should unblock many of the use cases that call for interoperability
between different module types.

The main changes in this commit are to detect when a user tries to do
this and give them a descriptive error message in that case. Other
related changes are:
* an additional integ test case for the situation where there's
  conflicting versions of a give module in the transitive dependency
  DAG, which is expected to continue working.
* tracking of which module a given schema type was loaded from, which
  allows us to distinguish between use of a type from core (which is
  still allowed) and a type from a dependency module.
* removal of "Dependencies" from the ExecutableSchema interface we use
  internally, which overlapped with some of the other changes here and
  was completely unused (leftover from ancient days during the Cloak
  project).

Signed-off-by: Erik Sipsma <[email protected]>

* check expected module type name direct in decode

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Co-authored-by: Erik Sipsma <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* feat: make module types id-able

This feature allows all module declared objects to be converted to a
respective ID-type using the "id" field, and converted from an ID-type
using the "loadFromID" helper. This effectively makes these types
fully idable.

IDs are essentially the contents of the module type's data,
JSON-encoded, and then base64-encoded, similar (but not identical) to
standard IDs. At the moment, they don't share exactly the same codepaths
to normal modules, but this could be adapted to be so in the future. We
also include a note of *what type* generated this ID - this is so we can
prevent type confusion.

Essentially, this enables a number of previously impossible use cases:

1. Returning a custom object from another module from a function

   - This case is the simplest - we just extend our conversion that
     unpacks all returned IDs from functions into their underlying types
     (so while a module returns an XID, it can be queried as if it was X).

2. Accepting a custom object as a parameter to a function

   - Because graphql doesn't support creating inline objects, we get
     around this in the core api by instead passing IDs everywhere. All
     the code-gen is setup to expect this, but module types weren't
     IDable, so we failed during schema-checking.

     Additionally, we need to transparently convert the ID from the
     graphql request into the underlying storage data for the module.

3. Accepting a custom object from another module as a parameter to a
   function

   - This is the same case as above, but also involves ensuring that we
     stitch in the type declarations from the module into our own schema
     so that we have the right type.

     However, here, we **don't** decode the ID into its underlying
     data, it's a foreign type, so the module should only see the ID.

Signed-off-by: Justin Chadwell <[email protected]>

* disallow use of external module types in function+field signatures

While we now give all module objects ID-ability and enable a module to
accept its own objects as function inputs, we are not yet going to allow
modules to use objects from other (non-core) modules as function args,
function returns or object fields.

This is because allowing that results in a caller who depends on a given
module A to now not only have access to module A's types but also the
types of transitive dependencies. This in turn opens the doors to
dependency hell; if two versions of a given module show up multiple
times in the full dependency DAG we need some fairly complex dependency
resolution logic around which of those versions is exposed to the
caller.

That problem is famously difficult and while we may need to deal with it
someday, it's preferable to avoid it for as long as possible.

The idea is to instead add support for interfaces in follow ups, which
should unblock many of the use cases that call for interoperability
between different module types.

The main changes in this commit are to detect when a user tries to do
this and give them a descriptive error message in that case. Other
related changes are:
* an additional integ test case for the situation where there's
  conflicting versions of a give module in the transitive dependency
  DAG, which is expected to continue working.
* tracking of which module a given schema type was loaded from, which
  allows us to distinguish between use of a type from core (which is
  still allowed) and a type from a dependency module.
* removal of "Dependencies" from the ExecutableSchema interface we use
  internally, which overlapped with some of the other changes here and
  was completely unused (leftover from ancient days during the Cloak
  project).

Signed-off-by: Erik Sipsma <[email protected]>

* check expected module type name direct in decode

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Co-authored-by: Erik Sipsma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants