-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[RFC] Typed GraphQL #418
Comments
Any chance we could mitigate this with some namespacing of the queries? |
@olance good idea! I like that. Maybe even deeper like |
I can understand why you would like to place it into the I will eventually rename it to At the moment it only deals with aws lambda style functions, but it is architected to support additional environments. (My pending PR that adds support for additional sides makes this clearer.) I think we should include an additional concept in between the CLI ( This will give us a natural place to include something like this (and another place for others to include something like this too), separates the concerns, and let's us think about graphql-generation in a separated way. |
Hi @mohsen1 really excited to see this issue and I believe this is exactly the great out-of-the-box developer experience Redwood offers very well.
To your above comment, I know this approach by the Prisma team triggered some mixed emotions on HN. In this context, I think it makes sense to follow the same principle as Prisma, where even if the generated types are generated in the Do you have any thought on the following point?
Im inclined to opt for the file to be generated since it allows us to analyse this is on changes, which i find extremely powerful - see GraphQL Inspector |
Functionally, I believe this is a good proposal and should be considered. But I will focus on a tangential discussion that emerges from this issue: TypeScript and Code Generation (There is a previous discussion around TypeScript and Redwood in general, but assuming that gets sorted out, then the code generation discussion will immediately follow) TypeScript and Code Generation
Now, with this in mind, let's look at some of the issues: Where to store generated files?
When to run code-generation?
From the discussion:
|
Have you guys seen https://gqless.dev/? It basically skips the entire manual process of writing queries and instead "automagically" generates them (along with TypeScript definitions) behind the scenes from the fields a component actually uses. It doesn't seem to be fully complete yet (e.g. mutations) but I think it shows a pretty elegant way of reducing boilerplate in RoR's spirit. |
@petrbela I really like the gqless.dev project! I've been thinking a lot about it and would love a way to simplify fetching data from the GraphQL server in the long term, in the short term, I think having generated types is a really decent way forward. |
Types help the discovery of field in the response. If we go from usage of the response to types we lose that nice discoverability |
We've added the GraphQL VSCode plugin which works very well during development time - I think we can improve it by dumping the SDL to the disk instead of requiring the api-server to be running.
What we often want is the ability to have types for the |
For anyone still tracking this #2485 is up that implements this (finally!) and has a bunch of other nice things to go along with it. |
There is interest in making Redwood GraphQL operations type-safe. Since Redwood is full-stack, we can offer a much better integration with code-generators and React components than existing solutions.
Core of my proposal is that type code generation and usage is invisible to the user.
State of the art
Currently using code generators such as Apollo or graphql-code-generator requires user to setup a watcher that watches graphql schema and existing query/mutation files, then it writes down resulting generated code. The second step is to import those generated types to make GraphQL operations type safe.
oof! So much boilerplate and importing just to get that
data
typed properly.The graphql-code-generator typescript-react-apollo offers generating code that includes those imports so using the
GetUserProfile
query is as simple as followingThis hook is strongly typed and the resulting
data
will have proper typing.The generated code is living alongside of the source code which makes code structure extremely polluted.
Proposal
The code generation should be done as part of Redwood dev server process and be completely invisible to the user.
Put generated code in node_modules
Learning from Prisma, I think this generated code can go straight into
@redwood/web
so we can avoid team debates on "should we check in the generated code or not?"!!All of generated code can live under
/types
directoryUse TypeScript Namespaces
This will allow access to deeper interfaces using
.
instead of typical bracket notation.GetBlogPosts.Success
looks better thanGetBlogPosts['Success']
Example
We can imagine generating a Cell for
BlogPosts
from the tutorial would look like:Notes about example
React.FC
type.Implementation details
Work to do this requires changes in the core and default template. Why the template is not in the monorepo?
graphql-code-generator
We would probably require to write a custom plugin for graphql-code-generator tailored to Redwood use case.
Possible challenges
The text was updated successfully, but these errors were encountered: