'Return ENUM - Cannot return null for non-nullable field
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
}
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |