Relation resolve type issues

Hi all,

I’ve recently noticed some TS errors in all of my services files, on what I believe are the relation resolvers (at the bottom of the files).

I upgraded RW (to 5.0.6) and force regenerated my SDL - this changed the resolver but now a new TS error is displayed!

For example, I have a Category model which has child categories:

type Category {
id: Int!
name: String!
childCategories: [Category]!
}

The resolver in services/categories.ts used to look like this:

export const Category: CategoryResolvers = {
childCategories: (_obj, { root }) =>
db.category.findUnique({ where: { id: root.id } }).childCategories(),
}

There were two TS errors:

Property ‘root’ does not exist on type ‘{ root: { transactions?: MakeRelationsOptional<Maybe[], AllMappedModels> | undefined; __typename?: “Category” | undefined; … 8 more …; level: number; }; context: RedwoodGraphQLContext; info: GraphQLResolveInfo; } | undefined’.ts(2339)

Type ‘PrismaPromise<Category[] | null>’ is not assignable to type ‘Maybe<ResolverTypeWrapper<{ transactions?: MakeRelationsOptional<Maybe[], AllMappedModels> | undefined; __typename?: “Category” | undefined; … 8 more …; level: number; }>>[] | Promise<…>’.ts(2322)

Following the update and force regenerate, the resolver now looks like this:

export const Category: CategoryRelationResolvers = {
childCategories: (_obj, { root }) => {
return db.category.findUnique({ where: { id: root?.id } }).childCategories()
},
}

The TS error is as follows:

Type ‘(_obj: {}, { root }: { root: { transactions?: MakeRelationsOptional<Maybe[], AllMappedModels> | undefined; … 9 more …; level: number; }; context: RedwoodGraphQLContext; info: GraphQLResolveInfo; }) => PrismaPromise<…>’ is not assignable to type ‘RequiredResolverFn<Maybe<ResolverTypeWrapper<{ transactions?: MakeRelationsOptional<Maybe[], AllMappedModels> | undefined; __typename?: “Category” | undefined; … 8 more …; level: number; }>>[], { …; }, RedwoodGraphQLContext, {}>’.
Type ‘PrismaPromise<Category[] | null>’ is not assignable to type ‘Maybe<ResolverTypeWrapper<{ transactions?: MakeRelationsOptional<Maybe[], AllMappedModels> | undefined; __typename?: “Category” | undefined; … 8 more …; level: number; }>>[] | Promise<…>’.ts(2322)
graphql.d.ts(594, 3): The expected type comes from property ‘childCategories’ which is declared here on type ‘CategoryRelationResolvers’

I’ve played around with a few things and tried to find help in the Prisma docs, but struggling a bit to work out the issue here.

Any help would be appreciated!

I’m slowly wrangling the project into shape to keep TS happy, and have managed to resolve this one.

I’ve stuck with the latest version (using CategoryRelationResolvers) and believe the issue was that the childCategories resolver might not find anything to return. Changing the sdl to make the array optional has removed the error:

type Category {
id: Int!
name: String!
childCategories: [Category!]
}

I actually made the contents of the array mandatory, so at least that’s got some typing support. I would have liked an empty array to be returned rather than null, but will need to dig into it further to figure out how to achieve that.