Need Help How to Implement Prisma Query Optimization Look Ahead Graphql

Update : I already found some way .

In this code is there any way we could only query in prisma the needed fields per what graphql client requested.

PostsCell.tsx
export const QUERY = gql`
  query FindPosts {
    posts {
      id
      title
      body
      createdAt
    }
  }
`
//api/src/services/posts/post.ts
//Default Example

export const posts = async () => {
  const posts = await db.post.findMany()
  return posts
}

//I want this
export const posts = async (dynamicRequestedFields ) => {
// {
//   id: true,
//    title: true,
//    body: true,
//    createdAt: true,
//  }
  const posts = await db.post.findMany({ select: dynamicRequestedFields})
  return posts
}

Hey :wave:

I’ve never done this with Prisma so I can’t help you directly, but it could be possible to do this optimisation, but the results are probably negligible if you’re already using the findUnique loader Prisma has with relations. Since code-first libraries like Pothos & Nexus know the schema of Prisma & GraphQL, it might make more sense to see if there is anyway to bridge those with Redwood somehow.

I did once build a function that “projected” a GraphQL query onto Mongo that used the query selectionSet.selections and filtered through any Field/Fragments etc and built the appropriate query but it didn’t really provide any meaningful improvements.

I know this doesn’t really help, but your best bet for true optimisation here is having some kind of bridge (like Pothos/Nexus) that can project the query onto GraphQL properly.

1 Like

Thanks, @notrab, and welcome!

@masterbater One other important consideration is what comes back from the service here has to match up with all the required fields from your SDL. So, they will have to at least match on that level.

Also, someone recommend this video: https://www.youtube.com/watch?v=2dAtmHuT-30&t=2096s

It’s GitHub - hayes/pothos: Pothos GraphQL is library for creating GraphQL schemas in typescript using a strongly typed code first approach and has some Prisma support for:

A plugin for more efficient integration with prisma that can help solve n+1 issues and more efficienty resolve queries

BTW Everyone and anyone who wants to learn more about GraphQL should definitely checkout Jaime’s https://graphql.wtf

1 Like

Thanks for replying I found a plugin where it helps me perfectly format it as a prisma query when selecting fields

It converts this query

query {
  findUniqueUser(where: { id: 1 }) {
    id
    email
    name
    posts(where: { title: { contains: "a" } }, orderBy: { createdAt: asc }, first: 10, skip: 5) {
      id
      title
      comments(where: { contain: { contains: "a" } }) {
        id
        contain
      }
    }
  }
}

into this as a prisma query. It would be nice if redwood team will add this little helper.

const result = {
  select: {
    id: true,
    email: true,
    name: true,
    posts: {
      select: {
        id: true,
        title: true,
        comments: {
          select: { id: true, contain: true },
          where: { contain: { contains: 'a' } },
        },
      },
      where: { title: { contains: 'a' } },
      orderBy: { createdAt: 'asc' },
      first: 10,
      skip: 5,
    },
  },
};

I did found a way using graphql-fields or graphql-parse-resolve-info, But found a plugin where it can do this and I rewritten the code to be much more concise. The original plugin is here Prisma Select Convert | Pal.js Start your NodeJs, Prisma, GraphQL, React project it did still use graphql-fields but has a code that helps traversing and formatting the resolverInfo into select object even supports deeply nested relation.

@notrab @dthyresson thanks for the reply I also read pothos its also great. Hope redwoodjs can have some convention to support code first third party libraries like typegraphql , nexus.