Generate graphql interfaces
valerii15298 opened this issue · comments
Is your feature request related to a problem? Please describe.
When using generated shema on frontend side we need lot of boilerplate for similar types(basically we cannot create graphql fragment
for two types, but graphql interfaces would allow it).
Describe the solution you'd like
https://typegraphql.com/docs/interfaces.html
Would be very useful for typegraphql-prisma
to generate graphql interfaces too.
Describe alternatives you've considered
An alternative is: create those interfaces manually on backend and use fragments on frontend side or
repeat code on frontend side.
I think that GraphQL interfaces are not designed to allow reducing boilerplate by having single fragment for common fields.
There are other ways to reduce the fragments boilerplate, like using gql
tag interpolation.
The main goal is to describe a common set of fields to then extend it by object types, so that queries can return the base interface and then we can try to fetch the additional fields for selected types, just like with union.
Aside from that, please describe in more details what is your desired output.
All I can say that scanning all the models to find a common set of fields and create an intefaces for it is not an option.
DBs nor Prisma doesn't have interfaces/base tables, so it's not 1:1 mapping.
About boilerplate: I am using pure .graphql
files with codegen without js. I am not sure if codegen(graphql code generator) is gonna execute JavaScript(typescript) to substitutute variables in gql via interpolation(probably not possible but I will try and check it). And even if it would be possible I will not have autocomplete, bcs common fields will be just a string of fields not related to Interface or type.
About desired output, I would imagine it to be like that: for every model we will have its interface with the same fields as in model @ObjectType. And object type will automatically implement its own interface.
Also maybe have option in generator itself to control if generate interfaces at all, not sure if everyone will need this feature by default.
for every model we will have its interface with all fields in interface by default
How this will help to have 1:1 copy of model with all its fields? You still can't create universal fragment to use it then for other types.
In my case I'm using custom resolver which outputs @ObjectType with few more additional fields extending already generated object type. So I will just implement interface for my custom object type.
But for it to work emitted object types should by default implement their own interface(which means no way for TypeGraphQL.omit(interface: false)
and generated interfaces will contain same fields as object types.
Have you tried just adding field resolvers to the generated base types? So that the base type have "few more additional fields", without creating new type "extending already generated object type".
https://prisma.typegraphql.com/docs/advanced/adding-fields
Hmm, I will modify my code to use FieldResolvers on generated types, seems it could solve my problem and I missed it. Thanks.
My intuition says there could be future use cases for generated interfaces, but for now I cannot give any use case or example, so you can close this.