Giter Club home page Giter Club logo

Comments (2)

helfer avatar helfer commented on May 3, 2024

@Naoto-Ida yeah, you're not using it as intended. In your case, you're giving it a list of Artist IDs, and you're getting back a list of Article IDs for all of those artists mixed. You can either fetch articles by article ID, or do the batching/caching to fetch all articles for an artist per artist, in which case you would return not a list of articles, but a list of lists of articles, i.e. [[article1, article2, ...]]

from dataloader.

Naoto-Ida avatar Naoto-Ida commented on May 3, 2024

@helfer Thanks for the clarification!

I also have other types that contain an articles field.

So now I've made these loaders:

export const ArtistArticleIdsLoader = new DataLoader((ids) => new Promise((resolve, reject) => {
    DOQUERYTOGETARTICLEIDS.then((results) => {
        let res = results.map(result => result.id)
        resolve(res)
    )
}))

export const EventArticleIdsLoader = new DataLoader((ids) => new Promise((resolve, reject) => {
    DOQUERYTOGETARTICLEIDS.then((results) => {
        let res = results.map(result => result.id)
        resolve(res)
    )
}))

export const ArticleLoader = new DataLoader((ids) => new Promise((resolve, reject) => {
    DOQUERYTOGETARTICLES.then((results) => {
        resolve(results)
    )
}))

Then in the ArtistType:

export default new GraphQLObjectType({
    name: 'Artist',
    fields: () => ({
                ...
                articles: {
                       type: new GraphQLList(ArticleType),
                       resolve: (artist, args, ast, { rootValue }) =>  {
                               return ArtistArticleIdsLoader.load(artist.id)
                              .then((articleIds) => {
                                      const promises = articleIds.map(id => ArticleLoader.load(id))
                                      return Promise.all(promises).then(results => results})
                              })
                       }
                }
        })
})

The first two loaders bunch up the ids of articles associated to a certain Artist or Event,
the last loader bunches up the results for those articles.

from dataloader.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.