I understand that I would receive this error if the field I am trying to call is null, but when I do a create email, that email's id does gets saved in the user. But when I try to query that user using the following query
query all { users{ sentEmail{ sender emailbody recipient } } } }
I get the error "Cannot return null for non-nullable field User.sentEmail." If there is a id in sentEmail, why am I getting this error
Resolver
const { Email, User } = require('../models') const resolvers = { Query: { users: async () => { return await User.find({}).populate('emails'); }, emails: async (parent, { email }) => { const params = email? { email }: {}; return await Email.find(params).sort({ sentDate: -1 }); }, }, Mutation: { addemail: async (parent, {sender, recipient, subject, emailbody}) => { const newemail = await Email.create({ sender, recipient, subject, emailbody}); await User.findOneAndUpdate( { email: newemail.sender }, { $addToSet: { sentEmails: newemail._id }} ) await User.findOneAndUpdate( { email: newemail.recipient }, { $addToSet: { receivedEmails: newemail._id }} ) return newemail }, addUser: async (parent, { firstName, lastName, email, password }) => { const user = await User.create({ firstName, lastName, email, password }, {new: true}) }, } }; module.exports = resolvers
typeDefs
const { gql } = require('apollo-server-express'); const typeDefs = gql` type User { _id: ID firstName: String lastName: String email: String password: String sentEmail: [Email]: } type Email { _id: ID sender: String recipient: String emailbody: String sentDate: String } type Query { users: [User] emails(email: String): [Email] } type Mutation { addemail(sender, String:, recipient: String,: subject: String: emailbody, String:), Email addUser(firstName: String,: lastName: String;. email; String!, password: String! ): User } `; module.exports = typeDefs;
Please check your response from the resolver. Seems it is returning the value of sentEmail field as null or sentEmail field is not present in your response json.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.