Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in Autogenerated Resolver for Many-to-Many Relations #464

Open
naccio8 opened this issue Aug 28, 2024 · 0 comments
Open

Error in Autogenerated Resolver for Many-to-Many Relations #464

naccio8 opened this issue Aug 28, 2024 · 0 comments
Labels
bug Something isn't working community Something initiated by the community

Comments

@naccio8
Copy link

naccio8 commented Aug 28, 2024

Issue Description

There seems to be a bug in the generation of automatic resolvers for many-to-many relationships.

Details:

  • I haven't verified if this issue is specific to MongoDB or if it also occurs with relational databases.
  • The problem arises specifically with many-to-many relationships. One-to-many relationships work fine.

Example:

Given the following schema:

model Ready {
  id                String                  @id @map("_id")
  destinazioneId    String?
  destinazione      Standstills?            @relation("ReadyToDestinazione", fields: [destinazioneId], references: [id])
  intermedi         ReadyIntermedi[]        @relation("ReadyToIntermedi")
  partenzaId        String
  partenza          Standstills?            @relation("ReadyToPartenza", fields: [partenzaId], references: [id])
}

model Standstills {
  id                      String                    @id @map("_id")
  indirizzo               String
  readyPartenza           Ready[]                   @relation("ReadyToPartenza")
  readyDestinazione       Ready[]                   @relation("ReadyToDestinazione")
  readyIntermediStandstill ReadyIntermedi[]         @relation("IntermediToStandstill")
}

model ReadyIntermedi {
  id            String      @id @map("_id")
  readyId       String
  ready         Ready       @relation("ReadyToIntermedi", fields: [readyId], references: [id])
  standstillId  String
  standstill    Standstills @relation("IntermediToStandstill", fields: [standstillId], references: [id])
  order         Int         // To maintain the order of intermediaries

  @@unique([readyId, standstillId])
}

If I query:

query Readies($take: Int) {
  readies(take: $take) {
    id
    partenza {
      indirizzo
    }
    intermedi {
      order
      readyId
      standstillId
    }
  }
}

It works fine with the correct relation on partenza.

However, if I modify the query and add:

standstill {
    indirizzo
  }

at intermedi and obtain this query:

query Readies($take: Int) {
  readies(take: $take) {
    id
    partenza {
      indirizzo
    }
    intermedi {
      order
      readyId
      standstillId
      standstill {
        indirizzo
      }
    }
  }
}

I get the following error:

"code": "INTERNAL_SERVER_ERROR",
"stacktrace": [
  "NotFoundError: No ReadyIntermedi found",
]

Analysis

In the generated TypeScript resolvers, I found:

@TypeGraphQL.Resolver(_of => ReadyIntermedi)
export class ReadyIntermediRelationsResolver {
  @TypeGraphQL.FieldResolver(_type => Ready, {
    nullable: false
  })
  async ready(@TypeGraphQL.Root() readyIntermedi: ReadyIntermedi, @TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Info() info: GraphQLResolveInfo): Promise<Ready> {
    const { _count } = transformInfoIntoPrismaArgs(info);
    return getPrismaFromContext(ctx).readyIntermedi.findUniqueOrThrow({
      where: {
        id: readyIntermedi.id,
      },
    }).ready({
      ...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
    });
  }

  @TypeGraphQL.FieldResolver(_type => Standstills, {
    nullable: false
  })
  async standstill(@TypeGraphQL.Root() readyIntermedi: ReadyIntermedi, @TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Info() info: GraphQLResolveInfo): Promise<Standstills> {
    const { _count } = transformInfoIntoPrismaArgs(info);
    return getPrismaFromContext(ctx).readyIntermedi.findUniqueOrThrow({
      where: {
        id: readyIntermedi.id,
      },
    }).standstill({
      ...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
    });
  }
}

This is incorrect. The correct resolvers should be:

@TypeGraphQL.Resolver(_of => ReadyIntermedi)
export class ReadyIntermediRelationsResolver {
  @TypeGraphQL.FieldResolver(_type => Ready, {
    nullable: false
  })
  async ready(
    @TypeGraphQL.Root() readyIntermedi: ReadyIntermedi, 
    @TypeGraphQL.Ctx() ctx: any, 
    @TypeGraphQL.Info() info: GraphQLResolveInfo
  ): Promise<Ready> {
    const { _count } = transformInfoIntoPrismaArgs(info);
    return getPrismaFromContext(ctx).ready.findUniqueOrThrow({
      where: {
        id: readyIntermedi.readyId,  // Correct access via readyId
      },
      ...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
    });
  }

  @TypeGraphQL.FieldResolver(_type => Standstills, {
    nullable: false
  })
  async standstill(
    @TypeGraphQL.Root() readyIntermedi: ReadyIntermedi, 
    @TypeGraphQL.Ctx() ctx: any, 
    @TypeGraphQL.Info() info: GraphQLResolveInfo
  ): Promise<Standstills> {
    const { _count } = transformInfoIntoPrismaArgs(info);
    return getPrismaFromContext(ctx).standstills.findUniqueOrThrow({
      where: {
        id: readyIntermedi.standstillId,  // Correct access via standstillId
      },
      ...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
    });
  }
}

Environment & setup

  • OS: Windows
  • Database: MongoDB v7.0.9
  • Node.js version: v20.10.0

Prisma Version

{
"dependencies": {
    "@prisma/client": "^5.18.0",
  },
  "devDependencies": {
    "@types/node": "^20.12.10",
    "prisma": "^5.18.0",
    "typegraphql-prisma": "^0.28.0",
    "typescript": "^5.4.5"
  }
}
@MichalLytek MichalLytek added bug Something isn't working community Something initiated by the community labels Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working community Something initiated by the community
Projects
None yet
Development

No branches or pull requests

2 participants