mickhansen/dataloader-sequelize

Redundant / Duplicated IDs in generated query

schie opened this issue · 3 comments

schie commented

Issue

Generated queries will contain multiple instances of the same ID if there was an 1-to-M relationship between two tables, (Person hasMany Pet)

If I am getting the person belonging to a pet, the queries generated are akin to the following:

-- get all the Pets
SELECT id, name, ownerId, etc...
FROM Pet

-- get all their pet owners (Person)
SELECT id, name, etc...
FROM Person
WHERE id IN (1, 2, 1, 1) -- person1 has 3 pets

Unfortunately, since a number of the projects I work on use UUIDs, this can become cumbersome when sending a few thousand ids 'through the wire'.

Findings

I noticed that in a couple of places have caching disabled
https://github.com/mickhansen/dataloader-sequelize/blob/master/src/index.js#L118
https://github.com/mickhansen/dataloader-sequelize/blob/master/src/index.js#L169

After looking at the data loader docs, I've notice that disabling caching causes the above 'issue' graphql/dataloader#disabling-cache

While 'poking-and-hoping' in the source code, I've found that enabling the cache simplifies the query to the following:

-- get all the Pets
SELECT id, name, ownerId, etc...
FROM Pet

-- get all their pet owners (Person)
SELECT id, name, etc...
FROM Person
WHERE id IN (1, 2) -- person1 has 3 pets

Question

Before I make a pull request to resolve what I think is an issue, was / is there any reason why caching is disabled?

schie commented

@mickhansen, do you need anything else from me?

@schie PRs always welcome. Not really a showstopper, the code needs to be refactored in general to only work with contexts.