Question: Splitting resolvers fails type check
bglgwyng opened this issue · comments
The following code passed type check. The definition of IResolvers
is generated by mercurius-codegen
.
const resolvers: IResolvers = {
User: {
// resolver code
},
Query,
Mutation
}
I wanted to seperate the User
resolver because it's quite large.
const User: UserResolvers = {
// resolver code
}
const resolvers: IResolvers = {
User,
Query,
Mutation
}
But then, the type check failed with this message.
Type '{ User: UserResolvers<MercuriusContext, User>; Query: QueryResolvers<MercuriusContext, {}>; Mutation: MutationResolvers<...>; }' is not assignable to type 'IResolvers<any, MercuriusContext>'.
Property 'User' is incompatible with index signature.
Type 'UserResolvers<MercuriusContext, User>' is not assignable to type 'GraphQLScalarType<unknown, unknown> | IEnumResolver | (() => any) | IResolverObject<any, MercuriusContext, any> | IResolverOptions<...> | undefined'.
Type 'UserResolvers<MercuriusContext, User>' is not assignable to type 'IResolverObject<any, MercuriusContext, any>'.
Property 'isTypeOf' is incompatible with index signature.
Type 'IsTypeOfResolverFn<User, MercuriusContext>' is not assignable to type 'IResolverObject<any, MercuriusContext, any> | IResolverOptions<any, MercuriusContext, any> | IFieldResolver<...> | undefined'.
Type 'IsTypeOfResolverFn<User, MercuriusContext>' is not assignable to type 'IFieldResolver<any, MercuriusContext, any>'.
Types of parameters 'info' and 'context' are incompatible.
Type 'MercuriusContext' is missing the following properties from type 'GraphQLResolveInfo': fieldName, fieldNodes, returnType, parentType, and 6 more.ts(2322)
When I switched UserResolvers
to IResolvers["User"]
, the same message appeared.
How can I fix this problem?
The error message above sounds like there is a mismatch of the position of context
argument between IsTypeOfResolverFn
and IFieldResolver
.
So I added parent: {}
to IsTypeOfResolverFn
and the error got resolved.
Now my new IsTypeOfResolverFn
definition reads as follows.
export type IsTypeOfResolverFn<T = {}, TContext = {}> = (
parent: {}, // prepended
obj: T,
context: TContext,
info: GraphQLResolveInfo
) => boolean | Promise<boolean>;
Is this the correct solution?