prisma-nestjs-graphql icon indicating copy to clipboard operation
prisma-nestjs-graphql copied to clipboard

[Question] Is there any way to use Args for relation queries?

Open nonamich opened this issue 2 years ago • 2 comments

✌️ First of all, thank you for a wonderful tool.

Everything works fine, but when I try to use "skip", "take", "where" arguments for relation queries, it doesn't give the desired result. Is there any way to solve this issue if at all possible?

The GraphQL query:

query {
  userFindMany {
    id
    followers( take: 1 ) {
      id
    }
  }
}

Error Log:

Show Error Log
{
  "error": {
    "errors": [
      {
        "message": "Unknown argument \"take\" on field \"User.followers\".",
        "locations": [
          {
            "line": 4,
            "column": 15
          }
        ],
        "extensions": {
          "code": "GRAPHQL_VALIDATION_FAILED",
          "stacktrace": [
            "GraphQLError: Unknown argument \"take\" on field \"User.followers\".",
            "    at Object.Argument (/usr/src/api/node_modules/graphql/validation/rules/KnownArgumentNamesRule.js:46:11)",
            "    at Object.enter (/usr/src/api/node_modules/graphql/language/visitor.js:301:32)",
            "    at Object.enter (/usr/src/api/node_modules/graphql/utilities/TypeInfo.js:391:27)",
            "    at visit (/usr/src/api/node_modules/graphql/language/visitor.js:197:21)",
            "    at validate (/usr/src/api/node_modules/graphql/validation/validate.js:91:24)",
            "    at processGraphQLRequest (/usr/src/api/node_modules/@apollo/server/src/requestPipeline.ts:245:38)",
            "    at internalExecuteOperation (/usr/src/api/node_modules/@apollo/server/src/ApolloServer.ts:1290:12)",
            "    at runHttpQuery (/usr/src/api/node_modules/@apollo/server/src/runHttpQuery.ts:232:27)",
            "    at runPotentiallyBatchedHttpQuery (/usr/src/api/node_modules/@apollo/server/src/httpBatching.ts:85:12)",
            "    at ApolloServer.executeHTTPGraphQLRequest (/usr/src/api/node_modules/@apollo/server/src/ApolloServer.ts:1065:14)"
          ]
        }
      }
    ]
  }
}

Resolver:

import { GraphQLResolveInfo } from 'graphql';
import { Resolver, Query, Args, Info } from '@nestjs/graphql';
import { PrismaSelect } from '@paljs/plugins';
import { PrismaService } from '~/prisma.service';

import { FindManyUserArgs, User } from '~/graphql/types/user';

@Resolver()
export class UsersResolver {
  constructor(private readonly prisma: PrismaService) {}

  @Query(() => [User])
  userFindMany(@Args() args: FindManyUserArgs, @Info() info: GraphQLResolveInfo) {
    const select = new PrismaSelect(info).value;

    return this.prisma.user.findMany({ ...args, ...select });
  }
}

nonamich avatar May 07 '23 11:05 nonamich

Prisma for folllowers property has User$followersArgs but graphql:

@Field(() => [User], { nullable: true })
followers?: Array<User>;

I need to check, but I suspect it will not be easy to generate property similar to User$followersArgs

unlight avatar Jun 03 '23 17:06 unlight

I think I found a working way to do that, using @resolveField. I have 2 entity user and messages

  @Query(() => User, { name: 'user' })
  findOne(@Args() query: FindUniqueUserArgs, @Info() info: GraphQLResolveInfo) {
    return this.userService.findOne(query.where, info);
  }

  @ResolveField(() => [Message], { name: 'messages' })
  public getMessageProperty(
    @Parent() user: User,
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    @Args() _findManyMessageArgs?: FindManyMessageArgs, // event if not used in function need to be here to allow us to use params when selecting user.messages like take, skip, ...
  ) {
    return user.messages;
  }

no need to use the message service, everything will be fetched and filtered in the user query, it just allow to have the messages args

florianrusso avatar Aug 18 '23 12:08 florianrusso