-
-
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
Generate types #110
Generate types #110
Conversation
The issue here is a bit over my head since I'm not a real TS user, so I can't give much review. My only concern is the growing number of example projects we need to maintain & keep up-to-date. Is there a way we can combine this into one of the existing examples? (And possibly go even further and consolidate examples into two or theee that demonstrate all features/behaviors (which I would be happy to work on)? |
I would say this does not need to stay an example outside this PR. I just added it as a playground so we can discuss and work on this feature. Once we have it working I think we can trash the example pre-merge |
@chrisdrackett at https://github.com/godness84/mst-gql/tree/fix-circular-refs you can find my attempt to solve the circular refs issue. Instead of defining exhaustively each type, I leverage the My attempt consists of:
Moreover, I cast the Results:
The bad:
I really appreciate your feedback and the feedback given by the people that will venture into my fork! |
Wow, I'm really excited whether this could solve #45. I did a quick trial and it mostly seems OK, although I still get 'any' for some of my complex models. One thing I noticed is that you generate two interface definitions with the same name for each model here:
Is this correct? |
@beepsoft yes that's correct! https://www.typescriptlang.org/docs/handbook/declaration-merging.html In which cases do you get |
@godness84 I made another PR based on your branch just so I could see the changes. I didn't get done playing with it, but it was looking good so far! I'll spend more time tomorrow :) |
@godness84 I have this Hasura generated schema: https://gist.github.com/beepsoft/84a64102a80a3ac7eb1f656b64fc2af8 It is quite complicated, but the main entities are choices and polls:
I generated the model like so:
And I get errors like:
|
@beepsoft I tried your schema at https://gist.github.com/beepsoft/84a64102a80a3ac7eb1f656b64fc2af8 and I don't see any error. Few questions:
|
If it works for you, It could very well be some problem with my environment. Could there be any tsconfig.json value that may effect the compilation? |
If I set |
Yep, I can see that Anyway, I think your solution will work, hopefully @chrisdrackett can merge and publish it soon. |
@beepsoft the Another solution could be to use an function as(self: any) { return self as unknown as MyModelType }
const MyModelType = BaseModelType
.actions(self => ({
doSomething() {
const childName = as(self).child.name // we account there's a child reference with name field.
// do whatever you want with your child name
}
}) |
@godness84 I merged your changes with my example and I'm still running into issues around:
can you take a look and make sure I didn't miss anything? |
@chrisdrackett The issue comes from the usage of Moreover, I added better typing support to arrays of referenced models using a I updated my branch (https://github.com/godness84/mst-gql/tree/fix-circular-refs), should I push it somewhere else? |
no, I can pull it into #128 and do some more testing! |
@godness84 moving discussion on this issue to #128 so its more directly linked to the code being discussed! |
This is a quick proof of concept based on this comment: #45 (comment)
I've created a 6th example that just takes the first example and adds a
User
to create a circular dependency. I then created types for bothTodo
andUser
:https://github.com/mobxjs/mst-gql/blob/feature/typeGeneration/examples/6-typegen/src/app/models/TodoModel.ts#L18
https://github.com/mobxjs/mst-gql/blob/feature/typeGeneration/examples/6-typegen/src/app/models/UserModel.ts#L16
These seem to work as expected when used (https://github.com/mobxjs/mst-gql/blob/feature/typeGeneration/examples/6-typegen/src/app/components/Todo.tsx#L7, https://github.com/mobxjs/mst-gql/blob/feature/typeGeneration/examples/6-typegen/src/app/Home.tsx#L19)
Things I'm not (yet) sure how to do:
toggle
onTodo
in this example)