Could anybody here help me with `graphql-code-gene...
# orm-help
g
Could anybody here help me with
graphql-code-generator
and
prisma
type mismatch on returning promise. That is, for this field resolver:
Copy code
customer: async (root, args, context) => {
    try {
      await ifCustomerOwnsThisResource(context, {
        cartItems_some: {
          id: root.id
        }
      });
      return await context.prisma.customerCartItem({ id: root.id }).customer();
    } catch (error) {
      console.error(error);
    }
  },
I am getting this error:
Copy code
src/resolvers/cartResolvers.ts:25:3 - error TS2322: Type '(root: CustomerCartItem, args: {}, context: IContext) => Promise<Customer>' is not assignable to type 'Resolver<Customer, CustomerCartItem, IContext, {}>'.
  Type '(root: CustomerCartItem, args: {}, context: IContext) => Promise<Customer>' is not assignable to type 'ResolverFn<Customer, CustomerCartItem, IContext, {}>'.
    Type 'Promise<Customer>' is not assignable to type 'Customer | Promise<Customer>'.
      Type 'Promise<import("G:/graphql/prod/graphql-server/src/generated/prisma-client-ts/index").Customer>' is not assignable to type 'Promise<import("G:/graphql/prod/graphql-server/src/generated/resolvers-types").Customer>'.
From what I get,
prisma
returns
interface CustomerPromise
for chained query, while
Promise<Customer>
is expected as return expression by
graphql-code-generator
. How to fix that?
d
Please raise an issue if you think that this is a bug here: https://github.com/prisma/graphqlgen/issues/new?template=bug_report.md
In the mean time, you should be able to write a script to rewrite the generated types maybe! Ideally, TS should map them similarly if the structure is same but this would need more investigation.
g
Hello master @divyendu - I am not using
graphqlgen
but
graphql-code-generator
(had struggles with
graphqlgen
2 months ago, didn't try it since. - I think second suggestion is local to
graphqlgen
too!
👍 1
h
@Gaurav Why are you await the promise? Ideally you can directly return the promise from a resolver. try changing
return await context.prisma.customerCartItem({ id: root.id }).customer();
to
return context.prisma.customerCartItem({ id: root.id }).customer();
g
Hello @Harshit Many a times I need responses from prisma client to be used later on in a resolver, there I think using
await
is necessary. (Correct me if wrong) Here in the return statement, I do understand what you're saying, and will correct to eliminate
await
in those places. But I don't think that solves this issue, since
prisma-client
returns a promise only. Doesn't it?