graphql-nexus / nexus-plugin-prisma

Deprecated
MIT License
829 stars 118 forks source link

Prisma relation can't be resolved: Missing the following unique identifiers: id #711

Open aaronwng opened 4 years ago

aaronwng commented 4 years ago

I have no idea why the relation can't be solved.

Graphql Error

{
  "errors": [
    {
      "message": "Resolver group.user is missing the following unique identifiers: id",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "groups",
        0,
        "user"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Resolver group.user is missing the following unique identifiers: id",
            "    at originalResolve (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/nexus-prisma/dist/builder.js:300:35)",
            "    at /Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/@nexus/schema/dist/plugins/fieldAuthorizePlugin.js:73:32",
            "    at Function.completeValue (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/@nexus/schema/dist/plugin.js:16:12)",
            "    at /Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/@nexus/schema/dist/plugins/fieldAuthorizePlugin.js:71:40",
            "    at lastResolver (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/@nexus/schema/dist/plugin.js:32:20)",
            "    at field.resolve (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/graphql-extensions/dist/index.js:134:26)",
            "    at field.resolve (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/apollo-server-core/dist/utils/schemaInstrumentation.js:52:26)",
            "    at resolveFieldValueOrError (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/graphql/execution/execute.js:467:18)",
            "    at resolveField (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/graphql/execution/execute.js:434:16)",
            "    at executeFields (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/graphql/execution/execute.js:275:18)",
            "    at collectAndExecuteSubfields (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/graphql/execution/execute.js:713:10)",
            "    at completeObjectValue (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/graphql/execution/execute.js:703:10)",
            "    at completeValue (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/graphql/execution/execute.js:591:12)",
            "    at completeValue (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/graphql/execution/execute.js:557:21)",
            "    at completeValueCatchingError (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/graphql/execution/execute.js:495:19)",
            "    at /Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/graphql/execution/execute.js:618:25",
            "    at Array.forEach (<anonymous>)",
            "    at forEach (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/iterall/index.js:83:25)",
            "    at completeListValue (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/graphql/execution/execute.js:614:24)",
            "    at completeValue (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/graphql/execution/execute.js:573:12)",
            "    at completeValue (/Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/graphql/execution/execute.js:557:21)",
            "    at /Users/Aaron/Local/Ryke/coding/shadowsocks-manager/node_modules/graphql/execution/execute.js:492:16"
          ]
        }
      }
    }
  ],
  "data": null
}

Query

query{
  groups{
    user{
      username
    }
  }
}

schema.prisma

model group {
  comment      String?
  id           Int     @default(autoincrement()) @id
  name         String?
}

model user {
  comment           String? @default("")
  crisp             String?
  email             String?
  group             Int?    @default(0)
  groupInfo         group?  @relation(fields:[group], references:[id])
  id                Int     @default(autoincrement()) @id
  lastLogin         Int?
 username:     String?
}

I just follow https://github.com/graphql-nexus/nexus-schema-plugin-prisma/issues/339#issuecomment-609782477 to set the schema.js

aaronwng commented 4 years ago

generated/nexus.graphql

type group {
  comment: String
  id: Int!
  name: String
  user(cursor: userWhereUniqueInput, skip: Int, take: Int): [user!]!
}

type user {
  comment: String
  crisp: String
  email: String
  group: Int
  groupInfo: group
  id: Int!
 lastLogin: Int!
 username:     String?
}
aaronwng commented 4 years ago

I finally found out that's because the unique identifier id starting from 0. If I run Query with id: 1,

query{
  group(where:{id:1}){
    user{
      username
    }
  }
}

it works fine, while if I run Query with id: 0, it just throws out this error

query{
  group(where:{id:0}){
    user{
      username
    }
  }
}

I thought we need to keep it normal even thought the id starting from 0.

aaronwng commented 4 years ago

https://github.com/graphql-nexus/nexus-schema-plugin-prisma/blob/81a53b7e48ce42b3a25de86b73f38d2b087f6620/src/constraints.ts#L47

I thought we may need to change this statement to

if(!data.hasownproperty(indentifier)){

So that we don't even miss situation when data[identifier] equals false

julia-dizhak commented 2 years ago

I have the same problem with nexus-plugin-prisma@0.35.0

 throw new Error(`Unable to resolve a unique identifier for the Prisma model: ${model.name}`)
styxlab commented 2 years ago

I ran into the same issue and was able to solve it by adding a @@unique command to the failing schema. Not a solution, but something to experiment with.

KetanKudikyal commented 2 years ago

@julia-dizhak did you find any solution to the problem?

yspreen commented 2 years ago

It's affecting us too, @styxlab's comment was a fix for us too. It seems to create a new unique index in an SQL migration, but that shouldn't be needed and seems redundant. Would be great to hear if this is actually an intended update or if it'll get fixed. Thanks!