orbitjs / orbit

Composable data framework for ambitious web applications.
https://orbitjs.com
MIT License
2.33k stars 134 forks source link

Schema: Allow relationships to reference multiple types/permissive setting for "any"? #914

Closed bradjones1 closed 2 years ago

bradjones1 commented 2 years ago

In Drupal, content data is modeled such that there is an entity type (e.g., article) and an optional "bundle" - let's say, news. These are represented in json:api as resources in the form of article--news, with fields defined matching their configuration.

Entity relationship fields thus may be defined to reference any article, regardless of its bundle.

This is currently impossible to define in the Schema construction that only allows for one string type to be defined.

In particular, this creates an issue with automatic generation of Orbit schema from, say, openapi.

Related, it would be amazing to be able to create Orbit Schema from a json-api or openapi document, but that's beyond the scope of this narrow report.

Would it be possible to loosen the schema to allow for multiple target schemas, or (even better) some way to allow for making this totally permissive ("allow any")?

I know orbit !== json:api but this would be in the spirit of the json:api spec, in so far as relationship field linkage objects all contain the type for the link.

bradjones1 commented 2 years ago

So it looks like this might actually just be a documentation issue - looks like this can take an array of strings, even though it's not documented in Docusaurus. https://github.com/orbitjs/orbit/blob/9da431680e3bcedf9ae80e1fdd9a3407e3f3d169/packages/%40orbit/records/src/record-schema.ts#L31

That being said, I'm happy to PR the docs but I'm still unsure if setting this to an empty array is the same as saying, "allow any"? If someone could answer that I'll add this to the docs.

SafaAlfulaij commented 2 years ago

It was mentioned in one of the release docs: https://github.com/orbitjs/orbit/blob/2ee2683ceb00788251632f7c2cdb33539b4818c9/website/versioned_docs/version-0.16/whats-new.md#polymorphic-relationship-support (Of course, model now is type, and type now is kind)

Not sure of the "allow any" though. You can check, but I doubt it works. Perhaps you can after defining all the types, loop through them, and update fields that support polymorpic?

schema = {...};
schema.article.relationships.bundle.type = Object.keys(schema); // not sure if bundle is a field name or something else...
bradjones1 commented 2 years ago

OK thanks for the reference. And yeah, either Drupal or a module implementing an openapi -> orbit schema mapping could certainly set this up "properly," but the loose "any" ability would be helpful in the meantime (and who knows, nothing's broken yet, maybe it does work out of the box.

I'll PR the docs to make this clearer without having to find the old release notes.

dgeb commented 2 years ago

Closed via #920. Thanks @bradjones1!