Error: Cannot return null for non-nullable field Post.title. at completeValue (D:\graphql\Graphql-social-media-project\api\node_modules\graphql\execution\execute.js:594:13) at executeField (D:\graphql\Graphql-social-media-project\api\node_modules\graphql\execution\execute.js:489:19) at executeFields (D:\graphql\Graphql-social-media-project\api\node_modules\graphql\execution\execute.js:413:20) at completeObjectValue (D:\graphql\Graphql-social-media-project\api\node_modules\graphql\execution\execute.js:914:10) at completeValue (D:\graphql\Graphql-social-media-project\api\node_modules\graphql\execution\execute.js:635:12) at completeValue (D:\graphql\Graphql-social-media-project\api\node_modules\graphql\execution\execute.js:584:23) at D:\graphql\Graphql-social-media-project\api\node_modules\graphql\execution\execute.js:486:9 at processTicksAndRejections (node:internal/process/task_queues:96:5) at async graphqlMiddleware (D:\graphql\Graphql-social-media-project\api\node_modules\express-graphql\index.js:125:26)
When trying to update the values for post like that:
post.title = PostInput.title;
post.content = PostInput.content;
post.imageUrl = PostInput.imageUrl;
const result = post.save();
My PostInput
looked had [object null prototype] before but I got rid of it by:
PostInput = JSON.parse(JSON.stringify(PostInput));
Now, it looks like this:
{
title: 'eeesss',
content: 'some real content',
imageUrl: 'images/4423877f-a37f-46ce-b1bf-db3082d2fe36'
}
My graphql
schema look like this:
type Post {
_id: ID!
title: String!
content: String!
imageUrl: String!
creator: User!
createdAt: String!
updatedAt: String!
}
type User {
_id: ID!
name: String!
email: String!
password: String
status: String!
posts: [Post!]!
}
type AuthData {
token: String!
userId: String!
}
type PostData {
posts: [Post!]!
totalPosts: Int!
}
input UserInput {
email: String!
name: String!
password: String!
}
input PostInput {
title: String!
content: String!
imageUrl: String
}
type RootQuery {
login(email: String!, password: String!): AuthData!
posts(page: Int): PostData!
post(postId: String!): Post!
}
type RootMutation {
createUser(UserInput: UserInput): User!
createPost(PostInput: PostInput): Post!
updatePost(id: ID!, PostInput: PostInput): Post!
}
schema {
query: RootQuery
mutation: RootMutation
}
I don't understand why it says PostInput.title
is null
when it clearly not.
BTW, the code is completely working the post indeed gets updated but the error is thrown anyway.
I will be glad for any help :)
CodePudding user response:
You need to make sure postInput
reach the data, something like this:
if (PostInput) {
post.title = PostInput.title;
post.content = PostInput.content;
post.imageUrl = PostInput.imageUrl;
const result = post.save();
}
CodePudding user response:
It looks like the issue is with the Post
being returned, not PostInput
since the message says Cannot return null for non-nullable field Post.title
not PostInput.title
.
Ensure you code isn't trying to return a post object with a null title.