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

Fluent API Issue When Using rejectOnNotFound #8929

Open
rtnolan opened this issue Aug 25, 2021 · 2 comments
Open

Fluent API Issue When Using rejectOnNotFound #8929

rtnolan opened this issue Aug 25, 2021 · 2 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: fluent api topic: rejectOnNotFound / *OrThrow

Comments

@rtnolan
Copy link

rtnolan commented Aug 25, 2021

Bug description

I'm attempting to use rejectOnNotFound to return an Error when findUnique() doesn't return an instance for a given model. My understanding is that rejectOnNotFound is concerned with the main model and not it's relations, so if a relation is null, I'd expect no Error to be thrown. I believe it's an issue with the Fluent API. Discussion here.

How to reproduce

Have a repo here: https://github.com/tnolan8/urql-prisma

The problem stems from the following setup:

const prisma = new PrismaClient({
  rejectOnNotFound: {
    findUnique: {
      User: (err) => new Error("Could not find user."),
    },
  },
});

// Field Resolver of Info relation to User
@TypeGraphQL.Resolver(_of => User)
export class UserRelationsResolver {
  @TypeGraphQL.FieldResolver(_type => Info, {
    nullable: true
  })
  async Info(@TypeGraphQL.Root() user: User, @TypeGraphQL.Ctx() ctx: any): Promise<Info | null> {
    return getPrismaFromContext(ctx).user.findUnique({
      where: {
        id: user.id,
      },
    }).Info({});
  }
}

In the above if I call findUnique() with a User that exists but doesn't have an Info object associated it throws and error, resulting in something like:

{
  "errors": [
    {
      "message": "Could not find user.",
      "locations": [
        {
          "line": 5,
          "column": 5
        }
      ],
      "path": [
        "user",
        "Info"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "clientVersion": "2.29.0",
          "stacktrace": [
            "Error: Could not find user.",
            "    at User (/Users/tomnolan/code/aikido/urql-prisma/src/index.ts:15:22)",
            "    at throwIfNotFound (/Users/tomnolan/code/aikido/urql-prisma/node_modules/@prisma/client/runtime/index.js:35822:13)",
            "    at cb (/Users/tomnolan/code/aikido/urql-prisma/node_modules/@prisma/client/runtime/index.js:35923:9)",
            "    at processTicksAndRejections (internal/process/task_queues.js:93:5)"
          ]
        }
      }
    }
  ],
  "data": {
    "user": {
      "id": "cksr9csqe0009tlmlbezm013p",
      "infoId": null,
      "Info": null
    }
  }
}

Expected behavior

Field Resolver returns with no Errors when User exists but doesn't have an associated relation.

Prisma information

model User {
  id     String @id @default(cuid())
  email  String @unique
  infoId String? @unique
  Info   Info?  @relation(fields: [infoId], references: [id])
}

model Info {
  id   String @id @default(cuid())
  User User?
}

Environment & setup

  • OS: macOS Big Sur 11.5.1
  • Database: MySQL
  • Node.js version: 14.5.1

Prisma Version

prisma               : 2.29.0
@prisma/client       : 2.29.0
Current platform     : darwin
Query Engine         : query-engine 1be4cd60b89afa04b192acb1ef47758a39810f3a (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli 1be4cd60b89afa04b192acb1ef47758a39810f3a (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 1be4cd60b89afa04b192acb1ef47758a39810f3a (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt 1be4cd60b89afa04b192acb1ef47758a39810f3a (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 1be4cd60b89afa04b192acb1ef47758a39810f3a
Studio               : 0.419.0
@rtnolan rtnolan added the kind/bug A reported bug. label Aug 25, 2021
@rtnolan rtnolan changed the title Fluent API Issue When Using rejectOnNoFound Fluent API Issue When Using rejectOnNotFound Aug 31, 2021
@pantharshit00 pantharshit00 reopened this Sep 2, 2021
@pantharshit00
Copy link
Contributor

I can reproduce this behavior. We should carefully define behaviour here. Marking this as confirmed bug for now.

@pantharshit00 pantharshit00 added bug/2-confirmed Bug has been reproduced and confirmed. team/client Issue for team Client. topic: rejectOnNotFound / *OrThrow labels Sep 6, 2021
@ripry
Copy link

ripry commented Nov 16, 2022

Perhaps this issue has been fixed in #10642.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: fluent api topic: rejectOnNotFound / *OrThrow
Projects
None yet
Development

No branches or pull requests

4 participants