返回枚举 - 无法返回无效字段的null

发布于 2025-01-25 21:33:48 字数 862 浏览 3 评论 0原文

我正在使用Apollo-Server-Lambda,Prisma Orm和GraphQl-Codegen。

我有一个查询(getBookById),它返回bookbook包含一个名为bookstatus的枚举。我希望能够在操场上的枚举中返回,但我会得到错误:

无法返回null for nullable field book.bookstatus。

bookstatus -typedef

enum BookStatus {
  OPEN
  DRAFT
  CLOSED
}

book -typedef

type Book {
  id: ID!
  title: String!
  bookStatus: BookStatus!
}

getBookBookbyId -typedef

type Query {
  getBookById(getBookByIdInput: GetBookByIdInput): Book
}

“

I am using apollo-server-lambda, Prisma ORM and graphql-codegen.

I have a query (getBookById) that returns a Book. Book contains an enum called BookStatus. I want to be able to return in ENUM in the playground but I get the error:

Cannot return null for non-nullable field Book.bookStatus.

BookStatus - TypeDef

enum BookStatus {
  OPEN
  DRAFT
  CLOSED
}

Book - TypeDef

type Book {
  id: ID!
  title: String!
  bookStatus: BookStatus!
}

getBookById - TypeDef

type Query {
  getBookById(getBookByIdInput: GetBookByIdInput): Book
}

PLAYGROUND ERROR

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病毒体 2025-02-01 21:33:48

解决:

问题是我正在使用Prisma作为ORM。
当我得到一本新书时,我正在编写代码:

  const foundBook = await prisma.book.findUnique({
    where: {
      id: bookId
    }
  });

问题是该书是使用此代码创建的:

  const newBook = await prisma.book.create({
    data: {
      user_id: userId,
      title: title,
      author: author,
      publication_year: publicationYear,
      isbn: isbn,
      photos: photos,
      book_condition: bookCondition,
      exchange_yype: exchangeType,
      book_status: bookStatus,
    },
  });
      
  return newBook;

};

Snake_case和Camelcase的组合创建了问题。
一旦一切都是骆驼,问题就解决了。

SOLVED:

The issue was that I was using Prisma as ORM.
When I was getting a new book, I was writing the code:

  const foundBook = await prisma.book.findUnique({
    where: {
      id: bookId
    }
  });

The issue is that the book was created using this code:

  const newBook = await prisma.book.create({
    data: {
      user_id: userId,
      title: title,
      author: author,
      publication_year: publicationYear,
      isbn: isbn,
      photos: photos,
      book_condition: bookCondition,
      exchange_yype: exchangeType,
      book_status: bookStatus,
    },
  });
      
  return newBook;

};

The mix of snake_case and camelCase created the issue.
Once everything was camelCase, the problem got solved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文