GQL resolver: returning null

I’m calling data from an API and have a resolver that looks like this:

Query: {
    async getSomething(parent, args, ctx, info) {
      const kek = yada.aol.emails(function (err, res) {
            return[{name: "lmao"}]
            // returns null every time
        })
      return [{name: "lol"}]
      // returns correctly
    },
  },

However, the data I need to return would be the response within the function. I was wondering as to why anything I return that’s nested in that function returns null and how I can continue to shape the data appropriately?

Answer

The namecheap.domains.getList function is asynchronous. Your last return is executed before the callback function (err, res) {}. You need to return a Promise.

Either namecheap.domains.getList supports Promises and you can return it directly (but I don’t know what that is), or you can make the callback function resolve a Promise:

  Query: {
    getDomains(parent, args, ctx, info) {
      return new Promise((resolve, reject) => {
        namecheap.domains.getList((err, res) => {
          if (err) return reject(err);
          resolve(res);
        })
      });
    },
  },