Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

test: Demonstrate issue with dataloader #2

Open
jlengstorf opened this issue Dec 23, 2017 · 4 comments
Open

test: Demonstrate issue with dataloader #2

jlengstorf opened this issue Dec 23, 2017 · 4 comments

Comments

@jlengstorf
Copy link
Member

@timrs2998 commented on Fri Nov 10 2017

@jlengstorf Attached is example code to demonstrate gramps-graphql/gramps-express#47

Problem

The purpose of the dataloader is to enable per-request caching. If I fetch an item by id "1234" twice in the same request, there should only be one request to the backend. However, fetching an item by id twice results in multiple backend calls.

Example

Start the backend server in one terminal:

$ node server.js
Example app listening on port 3000!

Start the GraphQL server using live data:

$ yarn live-data
..
============================================================
    GrAMPS is running in live mode on port 8080

    GraphiQL: http://localhost:8080/graphiql
============================================================

Submit the following GraphQL request:

query {
  hello:YourDataSource(id:"1234") {
    lucky_numbers
  }
  world:YourDataSource(id:"1234") {
    lucky_numbers
  }
}

And since the dataloader should cache the "1234" entity, I expect only one backend call and I expect the lucky_numbers arrays to be the same, even though lucky_numbers is an array of random numbers.

However, in server.js's logs, I can see two requests:

Request number #0
Request number #1

and in the JSON response:

{
  "data": {
    "hello": {
      "lucky_numbers": [
        7844,
        9953,
        1779,
        7452,
        5061,
        8062,
        7354
      ]
    },
    "world": {
      "lucky_numbers": [
        6489,
        6526,
        1814,
        5528,
        8614,
        4666,
        4819
      ]
    }
  }
}

Solution

I believe the problem is that a new dataloader is constructed every time connector.get() is called, rather than once for every GraphQL request. I'm thinking connector's lifecycle needs to change to accommodate this.


@timrs2998 commented on Fri Nov 10 2017

⚠️ don't merge, of course; feel free to close

@timrs2998
Copy link

Thanks for moving this. The default connector probably shouldn't include the dataloader with this behavior. With the new api changes, it's now possible to create dataloaders per request:

export default {
  ..,
  context: req => new Model(new Connector()), // <-- problem solved
}

Maybe it just needs to be documented now.

@jlengstorf
Copy link
Member Author

Have you tested that working?

@timrs2998
Copy link

yes, it's working for us

@idibidiart
Copy link

@jlengstorf

For a different but similar purpose (to solve the N+1 query proliferation problem), I've used this and it works well: https://github.com/calebmer/graphql-resolve-batch

It doesn't address the use case described here but it would be interesting to see if DataLoader can deal with the same cases as graphql-resolve-batch

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants