Gabriel Oliveira
11/04/2020, 12:21 PMCannot return null for non-nullable field User.id.
This is happening when trying to fetch data from a model called Consultation
which has a field called requestedBy
which is a relation to a model User
, so one Consultation
has a 1-to-1
relation with User
model
I know the data exist in this nested query, because I'm migrating from prisma1 to prisma2, and in the prisma1 project the result comes in correctly
simplified Models:
model User {
id String @id @default(cuid())
name String
Consultation_Consultation_requestedByToUser Consultation[] @relation("Consultation_requestedByToUser")
}
model Consultation {
id String @id @default(cuid())
details String?
requestedBy String?
User_Consultation_requestedByToUser User? @relation("Consultation_requestedByToUser", fields: [requestedBy], references: [id])
}
Query Resolver:
return ctx.prisma.consultation.findMany({
include: {
User_Consultation_requestedByToUser: true,
},
})
So my general question is, when you face this kind of error, would it be because something is wrong in the resolver query only, or should I also review if something is wrong in my models relation designnikolasburk
include
in a resolver because the nested data gets resolved by the type resolvers. Instead, you should implement an objectType
for User
which resolves the Consultation_Consultation_requestedByToUser Consultation
field 🙂Gabriel Oliveira
11/04/2020, 2:19 PMGabriel Oliveira
11/04/2020, 2:20 PMconst resolvers = {
Query: {
me:(parent, args, ctx) => {
const userId = getUserId(ctx)
return ctx.prisma.user.findOne({
where: {
id: userId
}
})
},
users:(parent, args, ctx) => {
return ctx.prisma.user.findMany();
},
consultations:(parent, args, ctx, info) => {
const userType = getUserType(ctx)
const userId = getUserId(ctx)
if(userType == 'PATIENT'){
return ctx.prisma.consultation.findMany({
where: {
requestedBy: {
id: userId
},
isDeleted:null
}
}, info)
}
if(userType == 'DOCTOR'){
return ctx.prisma.consultation.findMany({
include: {
User_Consultation_requestedByToUser: true,
},
})
}
}
},
Mutation: {
createUser:(parent, args, ctx) => {
//...
},
login: async(parent, args, ctx) => {
//...
},
deleteUser:(parent, args, ctx) => {
//...
},
updateUser:(parent, { data }, ctx) => {
//...
},
createPrescription:(parent, { data }, ctx) => {
//...
},
createConsultation:(parent, { data }, ctx) => {
//...
},
updateConsultation:(parent, { id, data }, ctx) => {
//...
}
}
}
nikolasburk
So, without an objectType for User, the nested part of the query, that fetches the user would make the user data return as null then?Yes, that's how GraphQL generally works (no matter if you're using SDL-first or code-first with Nexus or TypeGraphQL, you always need these extra type resolvers to resolve nested queries). That's also covered in the docs here: https://www.prisma.io/docs/guides/upgrade-guides/upgrade-from-prisma-1/upgrading-prisma-binding-to-nexus#2-create-your-graphql-types 🙂
Gabriel Oliveira
11/04/2020, 2:36 PMnikolasburk
nikolasburk
Gabriel Oliveira
11/04/2020, 2:53 PM