Skip to content
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

Question: Splitting resolvers fails type check #305

Open
bglgwyng opened this issue Jul 3, 2022 · 1 comment
Open

Question: Splitting resolvers fails type check #305

bglgwyng opened this issue Jul 3, 2022 · 1 comment

Comments

@bglgwyng
Copy link

bglgwyng commented Jul 3, 2022

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?

@bglgwyng
Copy link
Author

bglgwyng commented Jul 4, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant