-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
Internal Imports to Avoid Circular Dependencies #117
Conversation
One problem with this method is that it's not working so great with --isolatedModules in Typescript. facebook/create-react-app#6054. I think the easiest solution would be to simply export the Model files as |
Ok, it turned out to be harder than it sounds. |
@pvpshoot Can you try move all the "Enum's" to the top of the |
Yes, this solves the problem #117 (comment) |
can we merge it? or can we help you? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. But note that I am not very on top of this project, for more in depth reviews, I recommend tagging @chrisdrackett :)
Ok, @pvpshoot this seems to be working properly now. Not sure how to merge but should be OK to do so |
@RXminuS I don't see any updates to the readme. I'm assuming some are needed as part of this change? |
@RXminuS also, do any or all of the examples need updating? |
True, also I just realised...I'm still exporting a bunch of things as Type that are actually Interfaces. @pvpshoot , maybe you have time to look at that? |
@@ -7,6 +7,7 @@ const explorer = cosmiconfig("mst-gql") | |||
const defaultConfig = { | |||
excludes: [], | |||
force: false, | |||
keepInternalOrder: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does this option do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It reads in the existing internal.js/s
file and how things are ordered in it and then produces a new internal.js/ts
file that tries to keep modules in that same order (prepending new ones).
This very easily allows you to resolve circular import dependencies. I've put it behind a flag so that it doesn't break for people not using the feature because suddenly it will prepend any new modules that are added later (which is almost certainly wrong since they get imported before BaseModel)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe I'm still confused, but because this is a generated file can't we just always import things in an ideal order and forgo adding an option?
It's similar to what I did in |
@RXminuS i'm a noob in TS but i will try =) |
@RXminuS is this solved by the recently released |
@RXminuS going to close this for now, but if this is still valid please feel free to update and reopen! |
I still encounter circular import issues during webpack in mst-gql v1.22.4 |
Background
The nature of GraphQL is that it is very easy to end up with circular dependencies. Simply something as
can quickly become problematic. mstGQL currently doesn't allow any elegant way of controlling the import order resulting in some funky behaviour in some cases. In our case, a particular
ModelSelector
was being imported before all the models that were required had been loaded resulting in some unclear error messages.Solution
@mweststrate wrote an excellent blogpost with an elegant workaround that uses internal.js files.
This change simply makes sure that all the modules now use
import {...} from "./internal"
rather than individual files. It also generates anindex.js/ts
file that exports all the public modules. Finally by setting thekeepInternalOrder
flag the generator loads in the existinginternal.js/ts
file and preserves the order of any files in it. This allows you to re-order any of the modules giving the end-user the ability to resolve any circular dependencies.Final Thoughts
Although I'm working on a big refactor using the Gluegun framework I think it would be a good idea to either settle or discard the idea of
internal.js/ts
module loading now so that we can maintain that style in future versions.