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 for each field #64

Closed
shakaIsReal opened this issue Dec 12, 2016 · 2 comments
Closed

Dataloader for each field #64

shakaIsReal opened this issue Dec 12, 2016 · 2 comments

Comments

@shakaIsReal
Copy link

shakaIsReal commented Dec 12, 2016

Please refer to: graphql/graphql-js#623 first, these issues are linked.
Let's say I'm querying this:

viewer { 
  posts(id:1) { 
   id
   title
   comments(first: 10) { 
    edges { 
    node { 
      text 
       name
        ...  
     }
   }
  }
 }
}

What happens on server:

  1. post id is fetched
  2. In "PostType", title is fetched ( through dataloader? ) and then comments
  3. etc..

How would you use dataloader for loading data for each field? Create one instance per field e.g. const title = new Dataloader( ...load posts's title... )?
But in this case title would be cached, and if user changes data - logic should be applied to update each loader separately (based on fields defined in the type that use dalataloader)

And where do you store all the loaders? It seems like a common practice to store them in an object e.g. const loaders = { .. loaders .. }

@leebyron @josephsavona


Relay/graphql boilerplate has code that lightly touches on this topic: https://github.com/codefoundries/UniversalRelayBoilerplate/blob/master/graphql/ObjectManager.js#L162 check it out.

It would be nice if devs could provide some docs on this topic

@leebyron
Copy link
Contributor

The answers to this are going to vary widely and depend a lot more on your storage model than your API design. Typically dataloaders correspond to individual "types" of information in storage - that's usually a table in SQL style storage. For most apps, a type in storage corresponds to a type in GraphQL, so I would expect to see a data loader for "Post" and a data loader for "Comment" unless storage was organized in a different way.

It's common to see a collection of loaders passed as part of the "context" variable in GraphQL so they're always available and unique per request.

You can read more about these patterns on the README for this repo on "Using with GraphQL" and "Common Patterns" https://github.com/facebook/dataloader#using-with-graphql

@hems
Copy link

hems commented Jun 13, 2017

I believe one of the great features of GraphQL is to know exactly which fields to fetch, so would be interest to have a standard way of fetching only the needed information from the database and not the whole document once the user is requesting only a few fields, i believe that is the motivation behind @shakaIsReal issue, am i right @shakaIsReal ?

i'm also trying to figure out a lean way of fetching the smallest amount of data from the database

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

No branches or pull requests

3 participants