Type Error for When Creating Many-to-one Relation Field on GraphQL Nexus

1.5k Views Asked by At

I am using Next.js and its api routes, Graphql, Nexus, Prisma for my web app. I got type error when I am creating a many-to-one relation field for the schema with Nexus. Seems that the return type from resolver does not match what it want.

export const Product = objectType({
  name: "Product",
  definition(t) {
    t.int("pid");
    t.int("catid");
    t.string("name");
    t.float("price");
    t.string("description");
    t.nonNull.field("category", {
      type: "Category",
      resolve: (parent: any, _, ctx: Context) => {        // Error here on resolve
        return ctx.prisma.products
          .findUnique({
            where: {
              pid: parent.pid,
            },
            rejectOnNotFound: true,
          })
          .category();
      },
    });
  },
});

I am still be able to retrieve the categories from products when I execute with the following graphql query.

query Products {
  products {
    pid
    catid
    price
    name
    description
    category {
      catid
      name
    }
  }
}

My schema is straight forward. A simple e-commerce with some categories, each categories have its products, and a product has only one categories.

schema.prisma

model Categories {
  catid Int @id @default(autoincrement())
  name String
  products Products[]

  @@map("categories")
}

model Products {
  pid Int @id @default(autoincrement())
  category Categories @relation(fields: [catid], references: [catid], onDelete: Cascade)
  catid Int 
  name String 
  price Float
  description String

  @@map("products")
}

Here is the type error:

Type '(parent: any, _: {}, ctx: Context) => Prisma__CategoriesClient<Categories | null>' is not assignable to type 'FieldResolver<"Product", "category">'.
  Type 'Prisma__CategoriesClient<Categories | null>' is not assignable to type '{ catid?: number | null | undefined; name?: string | null | undefined; } | PromiseLike<{ catid?: number | null | undefined; name?: string | null | undefined; }> | { catid?: MaybePromise<...>; name?: MaybePromise<...>; } | PromiseLike<...>'.
    Type 'Prisma__CategoriesClient<Categories | null>' is not assignable to type 'PromiseLike<{ catid?: number | null | undefined; name?: string | null | undefined; } | { catid?: MaybePromise<number | null | undefined>; name?: MaybePromise<string | null | undefined>; }>'.
      Types of property 'then' are incompatible.
        Type '<TResult1 = Categories | null, TResult2 = never>(onfulfilled?: ((value: Categories | null) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | ... 1 more ... | undefined) => Promise<...>' is not assignable to type '<TResult1 = { catid?: number | null | undefined; name?: string | null | undefined; } | { catid?: MaybePromise<number | null | undefined>; name?: MaybePromise<string | null | undefined>; }, TResult2 = never>(onfulfilled?: ((value: { ...; } | { ...; }) => TResult1 | PromiseLike<...>) | ... 1 more ... | undefined, onre...'.
          Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
            Types of parameters 'value' and 'value' are incompatible.
              Type 'Categories | null' is not assignable to type '{ catid?: number | null | undefined; name?: string | null | undefined; } | { catid?: MaybePromise<number | null | undefined>; name?: MaybePromise<string | null | undefined>; }'.
                Type 'null' is not assignable to type '{ catid?: number | null | undefined; name?: string | null | undefined; } | { catid?: MaybePromise<number | null | undefined>; name?: MaybePromise<string | null | undefined>; }'.ts(2322)
definitionBlocks.d.ts(160, 5): The expected type comes from property 'resolve' which is declared here on type 'NexusOutputFieldConfig<"Product", "category"> & { resolve: FieldResolver<"Product", "category">; }'

I have read this issue but I have no clue to fix it
https://github.com/graphql-nexus/nexus/issues/914#issuecomment-887024267

0

There are 0 best solutions below