Coder Social home page Coder Social logo

Comments (4)

pedro-surf avatar pedro-surf commented on May 3, 2024 1

Why did you close it without a reference nor a comment? @AlicanC provided some good info about connections setup, however this didn't answer the question, for me, at least.

from dataloader.

jgcmarins avatar jgcmarins commented on May 3, 2024 1

ideally you should not "loadAll", but have a connection which knows how to load each individually
check this: https://github.com/entria/graphql-mongoose-loader/blob/master/src/ConnectionFromMongoCursor.ts

from dataloader.

zuhair-naqvi avatar zuhair-naqvi commented on May 3, 2024

@jepezi do you mind please sharing your learnings here?

from dataloader.

AlicanC avatar AlicanC commented on May 3, 2024

On a root connection's resolve(), I just query the database for ids and return objects with only ids in them ([{ id: 1 }, { id: 2 }, ...]) and then in resolveNode() (where I receive those objects) I load the rest of the data with DataLoader.

At first, making two trips to the database (one trip for ids and another batched trip for complete data) seemed stupid, but if the ids are cached then the second trip doesn't happen at all and if they are not cached then you are caching them for use in another query.

The function I use to create root connections look like this:

export function createRootConnectionField({ nodeType, tableName }) {
  const { connectionType } = connectionDefinitions({
    nodeType,
    resolveNode: (edge) => getDataLoaderOfType(nodeType).load(edge.node.id),
    connectionFields: () => ({
      totalCount: {
        type: GraphQLInt,
        resolve: (connection) => connection.totalCount,
      },
    }),
  });

  return {
    type: connectionType,
    args: connectionArgs,
    resolve: async (viewer, args) => {
      const objects = await db(tableName).select('id');

      return {
        ...connectionFromArray(objects, args),
        totalCount: objects.count,
      };
    },
  };
}

On the other hand, if your cache gets invalidated often, then you will be making two trips all the time and this becomes a performance issue. If that's the case then you could easily select all columns in resolve() and remove resolveNode(). If you do that, you might also want to prime the cache using dataLoader.prime(key, value) in your resolve().

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.