0

Cannot return null for non-nullable field.

I have a graphQL resolver going through exerciseEntry, and it hits the user ID, where I then try to find the user info if you query it. playgound show the error above.

I've tried the setting the object results to match the user data but it's still not getting returned.

Resolvers:

Query: {
  getExerciseEntries: async (root, args, ctx) => {
    const entries = await ExerciseEntry.getAll();
    console.log(entries);
    return entries;
  },

  getExerciseEntryBy: async (root, args, ctx) => {
    const entry = await ExerciseEntry.findBy(args.filter);

    return entry;
  },
  getExerciseEntryById: async (root, args, ctx) => {
    const entry = await ExerciseEntry.findById(args.id);
    return entry;
  }
},
ExerciseEntry: {
  user: async (root, args, ctx, info) => {
    const user = await Users.findBy({id: root.exercise_entry_user_id})
    console.log("seeing users", user)
    return user
  }
}

TypeDefs:

type Query {
  getExerciseEntries: [ExerciseEntry!]!
  getExerciseEntryBy(filter: String!): ExerciseEntry!
  getExerciseEntryById(id: ID!): ExerciseEntry!
}

type ExerciseEntry {
  id: ID!
  date: String!
  name: String!
  caloriesBurned: Int!
  user: User!
}

type User {
  id: ID!
  firstName: String!
  lastName: String!
  username: String!
  email: String!
  userType: String!
  calorieGoal: Int!
  weight: Int
  foodEntries: [FoodEntry!]!
  exerciseEntries: [ExerciseEntry!]!
}

I expect the query to work where data is shown as such:

{
 "data": {
   "getExerciseEntries": [
     {
       "id": "1",
       "caloriesBurned": 100
       "user": {
         "id": 1,
         "calorieGoal": 2000   
       }  
     }
   ]
 }
}

or something similar.

Marco Daniel
  • 5,467
  • 5
  • 28
  • 36
dp_chua
  • 123
  • 1
  • 2
  • 10

2 Answers2

1

The error is stating that it got null for a field that is marked as non-nullable (on the server side). Not sure which field it's complaining about but you could start debugging by removing all of the Non-Nullables from your schema (remove the ! on each field) or remove all the fields from your query and add them back one at a time until you find the culprit.

If it's the getExerciseEntries.user field then the line of code here const user = await Users.findBy({id: root.exercise_entry_user_id}) is returning null. Or it could be getExerciseEntries.user.calorieGoal since it's also marked as Non-Nullable.

csm232s
  • 1,660
  • 4
  • 32
  • 60
0

We had two methods, the findBy and findById. If you're using findById, you can just do

 ExerciseEntry: {
  exercise_entry_user_id: async (root, args, cxt, info) => {
   const user = await Users.findById(root.exercise_entry_user_id);
   return user;
  }
 } 

If you're using findBy, an example could be:

User: {
 exerciseEntries: async (root, args, ctx, info) => {
  const entries = await ExerciseEntry.findBy({ exercise_entry_user_id: root.id });
  return entries;
 }
}

You just have to make sure you're actually calling the right data from the backend so names mater a lot. Also your typedefs have to match the data you want returned.

Our exerciseEntry Typedefs were:

  type ExerciseEntry {
    id: ID!
    exerciseEntryDate: Date!
    exerciseName: String!
    caloriesBurned: Int!
    exercise_entry_user_id: User!
  }
dp_chua
  • 123
  • 1
  • 2
  • 10