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

时间:2019-10-05 13:57:15

标签: graphql prisma prisma-graphql nexus-prisma

我需要从pyramida类型解析自定义解析器。但是在这种情况下,我无法访问File file = new File(AG_HOME+"/conf/subscriber_content_restriction.conf"); course的孩子,我的subjects类型只能解析一个级别的数据。

Deps:

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', ... 是未定义的:/

我做错了什么?

1 个答案:

答案 0 :(得分:1)

呼叫prisma.person({ id: args.id })仅会提取请求的人。它没有eager load任何关联的模型。您可以使用shown in the docs作为流利的API来查询特定模型实例的关系:

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)