Search code examples
graphqlprismaprisma-graphqlnexus-prisma

How resolve a custom nested extendType resolver with prisma nexus?


I need resolve a custom resolver from a prisma type. But I can't access to children in this case course or subjects, my person type only resolve data over one level.

Deps:

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

Datamodel:

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!
}

My resolver:

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 variable only resolve the next:

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

And person.course is undefined :/

What I'm doing wrong?


Solution

  • Calling prisma.person({ id: args.id }) will only fetch the requested Person. It does not 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()
    

    or even:

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

    You can also get everything at once by providing a fragment that specifies what fields, including relations, you want to fetch 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)