mst-gql icon indicating copy to clipboard operation
mst-gql copied to clipboard

Circular dependencies with union types resulting in run-time crash

Open emckay opened this issue 3 years ago • 9 comments

Union types can easily result in circular dependencies that result in models being undefined at runtime.

Here is an example: https://github.com/mobxjs/mst-gql/commit/6aff548486dbb9c058fbd8e55f7e957a0adab250

emckay avatar Mar 10 '21 00:03 emckay

I made a fork and provided a workaround for circular dependencies. The fix I used is a ruby script that post-processes the .base models into one single file. Then I created models.ts which exports all the leaf model classes. See the examples/5-nestjs for the script.

In addition I fixed a number of TS errors, updated package versions, and other bugs in the runtime lib.

Here's the repo.

jkolyer avatar May 29 '21 16:05 jkolyer

Wow this is great! Any chance you're looking for a job or open to contracting? I work for Primer (withprimer.com) and we're looking for JS devs who know how to go deep on things like this :). Feel free to email me: [email protected]

emckay avatar May 29 '21 20:05 emckay

@emckay Glad you like it. I still need to fix some lingering dependency issues and other tweaks. Should get to it next day or two. I'll reach out on email.

jkolyer avatar May 31 '21 16:05 jkolyer

@jkolyer I've tried looking at the repo you've linked but their arent' any commits from you.

krstns avatar Mar 01 '22 15:03 krstns

@jkolyer I've tried looking at the repo you've linked but their arent' any commits from you.

@krstns I had to separate my changes to unblock people. There were packages upgrades and bug fixes already merged.

The other batch of changes are more substantial however. These address circular dependences and moving React dependencies out. At this stage it's more of a workaround than something PR-ready. The examples are not all functional, for instance (but they aren't in this repo either), and I'm using a ruby script to post-process generated code.

If you're interested in these changes I can tidy it up, but if you just need packages upgrades, they're already merged in here.

jkolyer avatar Mar 02 '22 17:03 jkolyer

@jkolyer thanks a lot for taking interest. I have a very simple set of APIs with circular references and unfortunately the generated models are unusable. Here's an example project: https://stackblitz.com/edit/jasmine-in-angular-uf3yhc?file=src%2Flib%2Fdata-manager.service.spec.ts

The apis are available in here: https://apitest.aji.digital If i can provide any additional help, please let me know.

The crucial part:

  • Animal Species one to many relation with Animal Breed which has many to many reference to Animal Species
  • Customer many to many relation with Animal

I have tried to generate models using mst-gql from the branch you have there with additional commits but the generated models were missing types (or at least that's the error I got). I didn't get to analyse it further but now that you have mentioned the use of additional post process scripts it starts to make sense.

Is there any manual action i could do on my end to create a working set of models? I've chosen mst-gql for the main purpose of having the models generated automatically in the future as the app progresses and circular dependencies are going to be common.

krstns avatar Mar 03 '22 08:03 krstns

Also interested in your script @jkolyer, as mst-gql completely breaks on some of the relationships our models have. I've been fixing stuff manually but looking for a more scalable solution...

giaset avatar Mar 18 '22 23:03 giaset

Here's the ruby script I wrote for workaround

I'm going to prioritize fixing my fork so this isn't necessary. It works for me using nestjs, hope you all can get some mileage out of it.

jkolyer avatar Mar 19 '22 17:03 jkolyer

hi @jkolyer. I know it's been quite a while but finally I've tried your script. I've tried it with last versions of mst-gql and with 0.14.1 and it doesn't work with them. I think some measures were taken to fight the circular dependencies and the format of base models changed and they cannot just be merged automatically into a single file. Those measures didn't work for us :/ could you let me know with which version of mst-gql your script should work?

krstns avatar May 04 '22 08:05 krstns