graphql-nexus / nexus-plugin-prisma

Deprecated
MIT License
828 stars 118 forks source link

Suggestion: Don't refetch already included relations #416

Open luhagel opened 5 years ago

luhagel commented 5 years ago

Heya, first of all thanks a lot for a ll the hard work you are putting into nexus and nexus-prisma!

With the way things currently are, resolver performance has a tendency to go down the drain with nested queries, since the dataloader isn't here yet and prisma2 is only able to handle one request at a time.

One way I found to speed thing up a little is by overriding the most widely used relations resolve function to include a check whether root/parent already includes the relevant field ( if, for example they were already fetched by photons eager loading select/include), and manually fetch them otherwise.

import { objectType } from 'nexus'

export const Post = objectType({
  name: 'Post',
  definition(t) {
    t.model.id()
    t.model.createdAt()
    t.model.updatedAt()

    t.model.name()
    t.field('comments', {
      type: 'Event',
      list: true,
      resolve: async (root, args, { photon }) => {
        //@ts-ignore
        return root.events || photon.comments.findMany({ where: { post: { id: root.id } } })
      },
    })
  },
})

I think making this behaviour baseline could help bridging the gap until the prisma2 binary supports concurrency and both prismaa & nexus-prisma had time to implement their own dataloader.

If you could point me in the right direction I'd also be more than happy to take a look at this myself.

shigurenimo commented 4 years ago

@luhagel @Weakky What is the best solution now?