如何使用 prisma nexus 解析自定义嵌套的 extendType 解析器?

How resolve a custom nested extendType resolver with prisma nexus?

我需要从 prisma 类型解析自定义解析器。但在这种情况下,我无法访问子项 coursesubjects,我的 person 类型只能解析一级上的数据。

部门:

"nexus": "0.11.7",
"nexus-prisma": "0.3.7",
"prisma-client-lib": "1.30.0",
...

数据模型:

type Person {
  id: ID! @unique
  firstName: String!
  secondName: String
  firstSurname: String!
  secondSurname: String
  sex: SexTypes
  address: String
  commune: Commune
  region: Region
  course: Course
  phone: String
  state: StudentStateTypes @default(value: ACTIVE)
  user: User! @relation(name: "UserPerson", onDelete: CASCADE)
  birthday: DateTime
  registrationNumber: String
  listNumber: Int
  previousSchool: OtherSchool
  scholarship: Scholarship
  createdAt: DateTime!
  updatedAt: DateTime!
}

type Course {
  id: ID! @unique
  client: Client
  name: String!
  person: [Person!]!
  teacher: User
  subjects: [Subject!]!
  evaluations: [Evaluation!]!
  createdAt: DateTime!
  updatedAt: DateTime!
}

type Subject {
  id: ID! @unique
  client: Client
  name: String!
  date: DateTime
  isEvaluable: Boolean
  order: Int
  course: [Course!]!
  evaluations: [Evaluation!]!
  createdAt: DateTime!
  updatedAt: DateTime!
}

type Evaluation {
  id: ID! @unique
  client: Client
  name: String!
  date: DateTime!
  period: Int!
  description: String
  coefficientTwo: Boolean
  course: Course!
  subject: Subject
  qualifications: [Qualification!]! @relation(name: "EvaluationQualification", onDelete: CASCADE)
  createdAt: DateTime!
  updatedAt: DateTime!
}

type Qualification {
  id: ID! @unique
  client: Client
  value: Float!
  evaluation: Evaluation! @relation(name: "EvaluationQualification", onDelete: SET_NULL)
  person: Person
  createdAt: DateTime!
  updatedAt: DateTime!
}

我的解析器:

export const query = extendType({
  type: 'Query',
  definition (t: any) {
    t.field('qualificationsCustom', {
      type: 'Person',
      args: {
        id: idArg()
      },
      resolve: async (parent: any, args: any, ctx: any) => {
        const person = await ctx.prisma.person({ id: args.id })

        const qualifications = person && person.course.subjects.reduce((acc: any, subject: any) => {
          acc.push({
            subject: subject.name
          })
          return acc
        })
        console.log(qualifications)
      }
    })
  }
})

person变量只解析下一个:

{
  birthday: '2008-10-27T00:00:00.000Z',
  secondName: 'Gisel',
...

并且 person.course 未定义 :/

我做错了什么?

调用prisma.person({ id: args.id }) 只会获取请求的人。它不 eager load any associated models. You can query the relations for a particular model instance by utilizing the fluent API as shown in the docs:

prisma.person({ id: args.id }).course()

甚至:

prisma.person({ id: args.id }).course().evaluations()

您还可以通过提供指定要获取哪些字段(包括关系)的片段来一次获取所有内容 as shown here:

const fragment = `
fragment PersonWithCourse on Person {
  # other person fields here
  course {
    # some course fields here
  }
}
`

prisma.person({ id: args.id }).$fragment(fragment)