Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arbitrary late type support #424

Open
airhorns opened this issue Mar 5, 2022 · 4 comments
Open

Arbitrary late type support #424

airhorns opened this issue Mar 5, 2022 · 4 comments
Labels
❔ question General question

Comments

@airhorns
Copy link
Contributor

airhorns commented Mar 5, 2022

For big projects that have a number of circular references, types.model(() => SomeType) is great for breaking the cycle and allowing types to reference themselves by not strictly requiring them to do so at require time. Right now AFAICT keystone only supports this behaviour for model types and object types, but not for other types like enums or unions.

mobx-state-tree supports types.late(() => ...) for arbitrary late type support such that you can late-reference an enum type or a union type that maybe has model references within it that cause circular import issues.

Is this something you fine folks have considered for keystone, and would it be possible? Happy to look into it but would love to know if there are any hard blockers.

@xaviergonz
Copy link
Owner

recursive and cross referenced models are supported, see for example the test cases in https://github.com/xaviergonz/mobx-keystone/blob/master/packages/lib/test/model/recursive.test.ts

@xaviergonz
Copy link
Owner

the only requirement is that if you use types.model for those cases you have to include the type in the generic, like types. model<M>(() =>M)

@xaviergonz xaviergonz added the ❔ question General question label Mar 6, 2022
@airhorns
Copy link
Contributor Author

airhorns commented Mar 6, 2022

Right, that's working well for me, but as far as I can tell you can't do the same thing with a union of models that then cross reference / recurse, because the union doesn't accept a function, it only accepts already defined values. MST's API allows this because you can do

const outer = types.union(
  types.late(() => types.model({thing: outer}), 
  types.late(() => types.model({otherThing: outer}))
)

or what have you. My question is would you be open to adding something like types.late to mobx-keystone, or extending the other type definition things to accept functions instead of concrete types the same way types.model and types.object do?

@xaviergonz
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
❔ question General question
Projects
None yet
Development

No branches or pull requests

2 participants