Hey everyone, what are the recommended places to r...
# orm-help
g
Hey everyone, what are the recommended places to review when you have this kind of error:
Cannot 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:
Copy code
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:
Copy code
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 design
n
Hey Gabriel 👋 can you show the full query (not only the resolver) for the. Also, it shouldn't be necessary to use
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 🙂
g
hey @nikolasburk thanks for the reply... 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? I think that may be what's missing here now
Entire resolvers object with not-relevant code stripped out
Copy code
const 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) => {
      //...
    }
  }
}
n
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 🙂
g
That's great thanks again, I'm gonna try that out then.. Also, I'm not using nexus at the moment, I really did not feel like it's an advantage, but I see that almost all of the examples use it, so it seems like using nexus is the recommended way to go about creating the models for prisma, is that correct? Is there a reason why I should use nexus over choosing not to use it? I did not see yet a reason why nexus will help me create the app faster (do less and get more out of it) but maybe I missed something...
n
There's no need to use Nexus if you don't want to 🙂 (one reason why you might want to use it is because it plays better with TypeScript but iirc you're using plain JS anyways, so that's not really relevant for you 👍).
g
Oh yes, I've seen that page before and noticed the specific resolvers for the models (apart from Query and Mutation on top) but I did not get it from reading that page that they were mandatory for nested queries. Thanks! I'm gonna use that page as the guide then nikolasburk The section with the type resolvers is also explained for your SDL-first approach here: https://www.prisma.io/docs/guides/upgrade-guides/upgrade-from-prisma-1/upgrading-prisma-binding-to-sdl-first#2-write-your-graphql-type-resolvers
🙌 1