0

我正在为我的网络应用程序使用 Next.js 及其 api 路由、Graphql、Nexus、Prisma。使用 Nexus 为架构创建多对一关系字段时出现类型错误。似乎解析器的返回类型与它想要的不匹配。

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();
      },
    });
  },
});

当我使用以下 graphql 查询执行时,我仍然能够从产品中检索类别。

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

我的模式是直截了当的。一个简单的电子商务,有一些品类,每个品类都有它的产品,一个产品只有一个品类。

模式棱镜

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")
}

这是类型错误:

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">; }'

我已经阅读了这个问题,但我不知道如何解决它
https://github.com/graphql-nexus/nexus/issues/914#issuecomment-887024267

4

0 回答 0