Coder Social home page Coder Social logo

Comments (5)

BartKrol avatar BartKrol commented on May 3, 2024

Any update on that?

from dataloader.

alexandrebodin avatar alexandrebodin commented on May 3, 2024

Hi.

Although that's a good point. We shouldn't change that behaviour as it would braek a lot of code using loadMany expecting to be able to catch after an error.

As stated in the doc loadMany is just a utility fonction and it serves as a dumb shortcut and It would be very easy for you to handle this case yourself.

Looking at loadMany implementation:

return Promise.all(keys.map(key => this.load(key)));

You can just do your own version of it like so

function myLoadMany(keys) {
  return Promise.all(
    keys.map(key => loader.load(key).catch(err => new Error(err))
  );
}

Hope it helped

from dataloader.

leebyron avatar leebyron commented on May 3, 2024

Hey @BartKrol - I think this is an interesting idea, but it's true that it would be a breaking change to the API.

Perhaps it was a mistake to include loadMany from the beginning since it was such a small convenience wrapper on Promise.all - I agree that it's perhaps more interesting if it provides behavior different from the more typical Promise.all usage.

Another challenge to consider is how to detect the difference between an Error object and a value - right now the library is using instanceof Error - but this is well known to be fragile if your environment spans JS realms. Not always a real problem, but can be in edge cases, right now an edge case not exposed.

from dataloader.

BartKrol avatar BartKrol commented on May 3, 2024

I think we are talking about two separate problems here.

First one is a problem with loadMany method, which, as you explained, is just a wrapper around Promise.all. I guess you are right and this shouldn't be changed.

The second problem is something different. I consider a new Error in a load method as a perfectly valid response - after all I'm returning an error and not throwing it. Here's a code example:

function createByIdLoader(loadFunc) {
  return new DataLoader(
    ids => Promise.all(
      ids.map(loadFunc)
    ),
    {
      cache: false
    }
  )
}

describe('DataLoader', () => {
  it('should return an error', () => {  // Failing
    const value = 'fail'
    const stub = sinon.stub().returns(Promise.resolve(new Error(value)))
    const loader = createByIdLoader(stub)

    return loader.load(1).then(error => {
      expect(error).to.deep.equal(new Error(value))
    })
    .catch((err) => {
      console.log('Should never be here')
      throw err
    })
  })
})

from dataloader.

leebyron avatar leebyron commented on May 3, 2024

Sorry for letting this issue sit, I'll be closing it since it's starting to age.

Unfortunately DataLoader needs to use the Error returning behavior to determine mixed results from a Batch. Throwing an error would indicate that the entire batch request failed, however in the case that the batch request itself succeeded but some of the values within it failed, then an appropriate result is to return the list of resolved values where some values at indexes are Error instances instead of values.

from dataloader.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.