Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataLoader arbitrary queries? #2

Open
lorensr opened this issue Apr 8, 2019 · 1 comment
Open

DataLoader arbitrary queries? #2

lorensr opened this issue Apr 8, 2019 · 1 comment
Labels
question Further information is requested

Comments

@lorensr
Copy link
Member

lorensr commented Apr 8, 2019

We could combine queries with an $or like in @pcorey's article:

http://www.petecorey.com/blog/2017/08/21/advanced-mongodb-query-batching-with-dataloader-and-sift/

How useful is that? We'd have fewer network requests to the server, but I imagine the bulk of the work is the query itself, which I don't imagine is actually getting simpler/faster. And memoization is great, but how likely is a repeat arbitrary query? In the case of findById it's clearly a common occurrence.

@lorensr lorensr added the question Further information is requested label Apr 8, 2019
@ecerroni
Copy link

ecerroni commented May 5, 2019

I think it would be useful. I implemented it myself adding this code block to cache.js:

const dataQuery = ({ queries }) => collection.find({ $or: queries })
      .toArray()
      .then(items => queries.map((query) => items.filter(sift(query))));

  const queryLoader = new DataLoader(queries => dataQuery({ queries }))

  // Only batching, no caching atm
  collection.findManyByQuery = async (query) => {
    const docs = await queryLoader.load(query)
    return docs
  }

I also modified cache.test.js:

const now = new Date()
const oneWeekAgo = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)

const docs = {
  id1: {
    _id: 'id1',
    createdAt: now
  },
  id2: {
    _id: 'id2',
    createdAt: oneWeekAgo
  },
  id3: {
    _id: 'id3',
    createdAt: oneWeekAgo
  }
}

....


  beforeEach(() => {
    collection = {
      collectionName,
      find: jest.fn((args) => ({
        toArray: () =>
          new Promise(resolve => {
            if (args.$or) {
              const { $or: queries } = args
              const siftDocs = Object.keys(docs).reduce((a, k) => [...a, docs[k]], [])
              setTimeout(() => resolve(queries.reduce((arr, query) => [...arr, ...siftDocs.filter(sift(query))], [])), 0)
            } else {
              const { _id: { $in: ids } } = args
              setTimeout(() => resolve(ids.map(id => docs[id])), 0)
            }            
          })
      }))
    }

    cache = new InMemoryLRUCache()

    setupCaching({ collection, cache })
  })

....

  it('finds two with queries batching', async () => {
    const foundDocs = await collection.findManyByQuery({
      createdAt: { $lte: oneWeekAgo }
    })
    expect(foundDocs[0]).toBe(docs.id2)
    expect(foundDocs[1]).toBe(docs.id3)
    expect(foundDocs.length).toBe(2)

    expect(collection.find.mock.calls.length).toBe(1)
  })

devileye98 referenced this issue in devileye98/apollov4-datasource-mongodb Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants