typegraphql-prisma icon indicating copy to clipboard operation
typegraphql-prisma copied to clipboard

Don't select the whole table

Open nitedani opened this issue 3 years ago • 9 comments

Is your feature request related to a problem? Please describe. The following query pulls the whole posts table in memory: query{ posts{ id } } If there are a large number of posts with large content, this can be an issue.

Describe the solution you'd like Transform the selected graphql fields into prisma select and pass it to the prisma client.

Additional context https://github.com/paljs/prisma-tools/blob/main/packages/plugins/src/select.ts

nitedani avatar May 05 '22 15:05 nitedani

This can be only opt-in because you may have field resolvers that depend on some not requested fields.

MichalLytek avatar May 06 '22 06:05 MichalLytek

@MichalLytek would you consider an exclude feature or something? I have one table which is large but I only need a small bit of it. Trying to find best way to exclude a certain field from the prisma query

tylergets avatar Jun 28 '23 12:06 tylergets

You can always eject and use your custom resolver instead of the single generated one.

MichalLytek avatar Jun 28 '23 12:06 MichalLytek

You may use following approach to query only for choosen fields in your custom resolver. It works fine till you work on one table.

import graphqlFields from 'graphql-fields';
...

@TypeGraphQL.Resolver(_of => SomeTable)
...
// Build prisma select object based on graphql query fileds
const select = Object.keys(
    graphqlFields(info, {}, { excludedFields: ['__typename'] }),
  ).reduce(
    (acc, key) => ({
      ...acc,
      ...{ [key]: true },
    }),
    {},
  );

return getPrismaFromContext(ctx).sometable.findMany({
    ...args,
    select
});

SebastianGrzymala avatar Jul 05 '23 12:07 SebastianGrzymala