【问题标题】:How resolve a custom nested extendType resolver with prisma nexus?如何使用 prisma nexus 解析自定义嵌套的 extendType 解析器?
【发布时间】:2020-02-03 12:52:29
【问题描述】:

我需要从 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 未定义:/

我做错了什么?

【问题讨论】:

  • 请编辑您的问题以包含数据模型,或至少包含其中的相关类型。

标签: graphql prisma prisma-graphql nexus-prisma


【解决方案1】:

调用prisma.person({ id: args.id }) 只会获取请求的人。它没有eager load 任何关联的模型。您可以使用 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)

【讨论】:

    猜你喜欢
    • 2019-09-13
    • 2012-02-21
    • 2019-09-29
    • 2019-01-23
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 2019-06-13
    相关资源
    最近更新 更多