mickhansen / graphql-sequelize

GraphQL & Relay for MySQL & Postgres via Sequelize
MIT License
1.9k stars 172 forks source link

how to eagerly load an associated model when using resolver without include? #574

Closed tomprogers closed 4 years ago

tomprogers commented 6 years ago

I want to do this:

driver: {
    type: MyGQLTypes.Driver,
    resolve: resolver(Driver, {
        include: { model: Helmet }
    })
}

But include is now disallowed. What's the workaround?

Related stackoverflow.

mickhansen commented 6 years ago

include never worked like that, it was/is a boolean option that tried to predict what kind of relations you would want based on the subfields in the query.

You can add includes manually in the before hook if you absolutely need to (if it's a 1:M you almost always use for instance), but otherwise stick to using dataloader logic, the performance is more linear and predictable than includes are.

RoelRoel commented 5 years ago

How to use dataloader logic?

I got this mutation where I need the organisation of the user. How to load it. Why isn't it lazy loaded when I call the user.organisation property?

Part of user.mutation.ts:

forgetPassword: resolver(User, { include: [Organisation],
        before: async (findOptions, { email }) => {
            findOptions.where = { email };
            return findOptions;
        }, [..]

Part of user.model.ts

  @BelongsTo(() => Organisation)
  organisation: Organisation;
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.