0

I need to check if a query is null in my graphql resolver. I dont think knowing graphql matters all that much in this question, when I call a dataSource query it has not response and I get an error that an id doesnt exist.

This is the resolver:

Alarm: {
    location: async (alarm, args, { dataSources }) => {

      const equipment = await dataSources.api.getEquipment(alarm.equipmentId);
      console.log(equipment);

      // if (equipment !== null) {
      //   return dataSources.api.getLocation(equipment.locationId);
      // } else {
      //   return null;
      // }
    },
  },

The equipment is just empty when I log it and I get an error in my graphql playground that the equipment doesnt exist for that equipmentId. I think this may be just basic null checking? Please let me know. Also I cant use ? operator it has to be &&

ousmane784
  • 147
  • 6
  • If `equipment` is an `Object` your problem is that `Boolean({ }) = true`, you can try something like `equipment && typeof equipment === 'object' && Object.keys(equipment).length !== 0` [Check this answer](https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object) – Dupocas Feb 08 '21 at 18:25

0 Answers0