The result here is expected. Even tho my prisma model doesn’t include a title, it’s returned from the gql request because of the resolver.
However, there is a typescript error raised here on the query defined above in the service file because title doesn’t exist there.
Error: Property 'title' is missing in type ....
I’ve re-run the yarn rw g types and still get this TS error. Any way to resolve this error without having to manually fill this field in the query (which sort of defeats the purpose of having the resolver)?
This is an unfortunate solution for me tho because in strict mode this will still complain since null is not a string. I could of course just return an empty string here I guess but then I’d have to do this for all of the different queries I have that return this (or a list of this) type.
I had assumed there was a solution for this built into RW but maybe there isn’t.
My suggestion was originally from a case where the relationresolvers fetch user metadata from an authentication provider ( the table just stored the userId foreign key but without a user table in prisma).
I fully agree that even though this solution works for relations which aren’t prisma types in some cases… it can quickly become tedious.
I’ve created a simple repository to showcase the typescript issues.
Omitted prisma fields would only help if you ‘removed a field from the sdl which is persisted in prisma and you don’t want the resolvers to handle it’.
We’re trying to ‘add a field to the sdl which isn’t persisted in prisma and we specifically want the resolvers to handle it’.
As far as we know the relation resolvers are the way to do that in redwood.
This approach works very well as you can see in the repository above …
but typescript complains very much about it.
as for the interface… that’d change the sdl spec to an optional field because it then becomes
type Query {
journal(id: Int!): JournalSimple @requireAuth
}
but if you know that every Journal has a title… and if you query it from the frontend knowing it will never be null or undefined … the api spec should reflect that.
Thanks for the reproduction here @xmaxcooking . This is great.
@dthyresson I appreciate your input above but is there a less heavyhanded way to do this if you know that 100% of the time you’re going to return the required field on the sdl when it doesn’t exist on the model?
An example of why this is helpful/important would be the idea of a computed field. For example, you have a date field and you want to return a relative date string to now (ie. ‘Yesterday’ or ‘5 hours ago’).
I was mainly suggesting a generic approach because the team is actively adding new functionality to the context object like the realtime livequery and subscriptions. Even third parties like zenstack used it to simplify code passthroughs and I can’t really imagine them shipping a setup with that kind of typescript ‘patch’ … and there may be others like them in the future.
If a generic is only extending the base of the graphql context though via RedwoodGraphQLContext then the right answer really is to extend the interface, this is similar to how CurrentUser is done in Redwood: