Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/Questions/Quick solution to fix the error: cannot return null for non-nullable field
Next
Answered
Bryce White
  • 25
Bryce White
Asked: May 18, 20222022-05-18T21:09:35+00:00 2022-05-18T21:09:35+00:00In: javascript

Quick solution to fix the error: cannot return null for non-nullable field

  • 25

I don’t know what I’m doing wrong, but I’ve already lost a couple of days struggling with this. Here is my command line:

export default `
 
 type User {
  id: ID!
  name: String!
  email: String!
 }
 
 type Query {
  allUsers: [User]
  currentUser: User
 }
 
 type Mutation {
  createAccount(name: String!, email: String!, password: String!): User
  loginUser(email: String!, password: String!): User
  updatePassword(email: String!, password: String!, newPassword: String!): User
  deleteAccount(email: String!, password: String!): User
 }
 
 `
createAccount: async (
  parent,
  { name, email, password },
  { User },
  info
 ) => {
  try {
  // Check for invalid (undefined) credentials
  if (!name || !email || !password) {
  return 'Please provide valid credentials';
  }
 
  // Check if there is a user with the same email
  const foundUser = await User.findOne({ email });
 
  if (foundUser) {
  return 'Email is already in use';
  }
 
  // If no user with email create a new user
  const hashedPassword = await bcrypt.hash(password, 10);
  await User.insert({ name, email, password: hashedPassword });
 
  const savedUser = await User.findOne({ email });
 
  return savedUser;
  } catch (error) {
  return error.message;
  }
 },

This returns:

Cannot return null for non-nullable field

I don’t have any experience with the “cannot return null for non-nullable field.” In this case, how should I change?

cannot return null for non-nullable field
  • 1 1 Answer
  • 769 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-29T16:01:07+00:00Added an answer on June 29, 2022 at 4:01 pm

    The cause:

    The main issue with your resolver is that, in a variety of situations, it returns a string rather than a User object. According to your schema, createAccount can either return a User or null. Your resolver forces a returned string into an object because it was expecting one and then begins searching for User attributes on that object (such as email and name). As these are non-null properties on your User object, returning null or undefined for them causes an error because they don’t exist.

    Solution:

    Most likely, your resolver should just throw any errors that are required. They will then be included in the errors array that makes up your answer. For instance:

    // Check whether there is a user with the similar email or not
    const foundUser = await User.findOne({ email })
    
    if (foundUser) throw new Error('Email is already in use')
    
    // If there is no user with email, let's create a new user
    const hashedPassword = await bcrypt.hash(password, 10);
    await User.insert({ name, email, password: hashedPassword });
    
    const savedUser = await User.findOne({ email });
    
    return savedUser;

    The response will look like the following if you receive a duplicate email:

    {
    "data": {
    "createAccount": null
    },
    "errors": [
    {
    "message": "Email is already in use",
    "locations": [
    {
    "line": 4,
    "column": 3
    }
    ],
    "path": [
    "createAccount"
    ]
    }
    ]
    }

    Use the formatResponse configuration parameters or formatError for your middleware of Apollo server if you wish to change how errors are displayed to clients. Using custom errors, which let you add specific properties like code to more readily identify the problem type on the client side, is also a smart idea.

    Finally, because your schema already has these inputs tagged as non-null, GraphQL will not need to check whether email, name or password are specified inside your resolver and will instead return an error if any of them are missing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.