0

我需要从 prisma 类型中解析自定义解析器。但是在这种情况下我无法访问孩子,course或者subjects,我的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未定义:/

我做错了什么?

4

1 回答 1

1

调用prisma.person({ id: args.id })只会获取请求的人。它不会急切地加载任何关联的模型。您可以使用文档中所示的 fluent API 查询特定模型实例的关系:

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

甚至:

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

您还可以通过提供一个片段来一次获取所有内容,该片段指定要获取的字段(包括关系),如下所示

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

prisma.person({ id: args.id }).$fragment(fragment)
于 2019-10-06T01:16:48.740 回答