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

Adding Dataloader, PersonType, and some data to test it. #1303

Closed
wants to merge 4 commits into from
Closed

Adding Dataloader, PersonType, and some data to test it. #1303

wants to merge 4 commits into from

Conversation

DaveyEdwards
Copy link

@DaveyEdwards DaveyEdwards commented Jun 10, 2017

Here is a pull request adding Dataloader to the project. If you log the response coming out of the getPerson you can see that it is only getting each person once.

Only thing I am unsure of is on large nested queries it starts to slowdown quite abit, but I think it is the way the 'seed/people' are being referenced in my getPerson function. Would appreciate any input.

When I query my db of Google Datastore with this same setup I get amazing performance on 700 lined nested queries.

Here is a gif demonstrating the queries/different sizes with what I have in the pull request
(last query takes around 20seconds to display and about 5min before I can scroll through the data. It is around 57mb in size)
2017-06-09_12-54-11

@tim-soft
Copy link

This is great, I was just planning on doing something very similar, so thank you!

What is a little confusing for me here is that you aren't retrieving people by querying Sequelize, which would be the whole point of using DataLoader in practice?

@DaveyEdwards
Copy link
Author

It never crossed my mind lol. I haven't used Sequelize in over a year and couldn't see any docs on setting the project up with it. Unfortunately I will be busy for the next month doing video production work though so I won't be able to get it up and running anytime soon. If you were wanting to set it up and have questions feel free to ask.

I have a project in heavy development ontop of Google Cloud Platform's Datastore + relay-modern/ graphql connections if your interested in looking at as well (my pull request is pretty much a copy of how dataloader is being used in it).

That repo is here: https://github.com/DaveyEdwards/myiworlds

@tim-soft
Copy link

Sequelize and an empty sqlite3 database actually come packaged with RSK these days, which is why I brought it up

I posted some code for accessing the db the other day via Sequelize issues/1296 for similar purposes

Querying the database responsibly is something I will need to sort out one way or another. Thanks for the links!

@DaveyEdwards
Copy link
Author

DaveyEdwards commented Jun 13, 2017

Implementing this wasn't anywhere near as hard as I had thought, if it helps here is a run down of adding new dataloaders or how you would integrate it with sequelize:

  1. Create the new loader inside your loaders folder and put your call to your database inside of it. Replace the getPerson() function with the function for your database.
export default {
  personLoader: new DataLoader(people => Promise.all(people.map(_id => getPerson(_id)) ))
}
  1. Add a new loader to your loaders/index.js. This is just to pull in your new loader which your sever.js file grabs to have access to all your loaders.
 // Import your new loader
import personLoader from './personLoader';

 // All your dataloaders
 const allLoaders = {
  // Add your new loader here
   ...personLoader
 }
 
 export default function loaders () {
   return allLoaders
 }

This is where in the server.js file it is getting those loaders and using them.

  pretty: __DEV__,
   context: {
     loaders: loaders()
   },
  })));
  1. Add dataloader to your schema types. Your database function is no longer in here but back in your dataloader.
   friends: {
      type: new List( PersonType ),
      resolve: async( person, args, { loaders }) => {
        return await loaders.personLoader.loadMany( person.friends );
      }
    }

That's it. Some documentation like this would have been extremely helpful for people like me just last week lol. If this gets accepted or gets some changes I think I will re write this with better examples to add to the docs, maybe do a video to

"dataloader": "^1.3.0",
"express": "^4.15.2",
"express-graphql": "^0.6.4",
"dataloader": "^1.3.0",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dataloader two times

@ulani
Copy link
Member

ulani commented May 27, 2021

@DaveyEdwards thank you very much for this PR! Unfortunately, we have close it due to inactivity. Feel free to re-open it or join our Discord channel for discussion.

NOTE: The main branch has been updated with React Starter Kit v2, using JAM-style architecture.

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

Successfully merging this pull request may close these issues.

5 participants