Search code examples
graphqlapolloprisma

Cannot return null for non-nullable field , Debugger dosen't show null


I have this schema.graphql

### This file was generated by Nexus Schema
### Do not make changes to this file directly


type AuthPayload {
  token: String!
  users: users!
}

scalar DateTime

type Mutation {
  login(email: String, password: String): AuthPayload!
  signup(CREATED_BY: String, EMAIL: String, FIRST_NAME: String, IS_ACTIVE: Boolean, PASSWORD: String, USERNAME: String): AuthPayload!
}

type Query {
  me: users
}

type users {
  CREATED_BY: String!
  CREATED_ON: DateTime
  EMAIL: String!
  FIRST_NAME: String!
  id: Int!
  IS_ACTIVE: Boolean!
  LAST_NAME: String
  MODIFIED_BY: String
  MODIFIED_ON: DateTime
  ORGANIZATION_ID: String
  PASSWORD: String!
  PHONE: String
  USERNAME: String!
}

mutations :-

const Mutation = mutationType({
  definition(t) {
    t.field('signup', {
      type: 'AuthPayload',
      args: {
        FIRST_NAME: stringArg({ nullable: true }),
        EMAIL: stringArg(),
        PASSWORD: stringArg(),
        IS_ACTIVE: booleanArg(),
        USERNAME: stringArg(),
        CREATED_BY: stringArg(),
      },
      resolve: async (parent, { FIRST_NAME, EMAIL, PASSWORD ,IS_ACTIVE ,USERNAME,CREATED_BY }, ctx) => {
        const hashedPassword = await hash(PASSWORD, 10)

         const user = await ctx.prisma.users.create({
          data: {
            FIRST_NAME,
            EMAIL,
            PASSWORD: hashedPassword,
            IS_ACTIVE,
            USERNAME,
            CREATED_BY
          },
        })
        return {
          token: sign({ userId: user.id }, APP_SECRET),
          user,
        }
      },
    })

    t.field('login', {
      type: 'AuthPayload',
      args: {
        email: stringArg(),
        password: stringArg(),
      },
      resolve: async (parent, { email, password }, context) => {
        const user = await context.prisma.users.findOne({
          where: {
            EMAIL : email,
          },
        })
        if (!user) {
          return new Error(`No user found for email: ${email}`)
        }
        const passwordValid = await compare(password, user.PASSWORD)
        if (!passwordValid) {
          return new Error('Invalid password')
        }
        const token = await sign({ userId: user.id }, APP_SECRET)
        return {
          token ,
          user,
        }
      },
    })
  },
})

My problem is when i try to mutate the login method with token return value , it works perfectly and here is my mutation

mutation{
login(email :"dondala422@hotmail.com" password :"aa")
{
  token
}
}

Response

{
  "data": {
    "login": {
      "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjMyLCJpYXQiOjE1OTcxMDY0OTd9.d1Ra32ArCXumBfzg2vE1-xeea21cAkNwWBJPm3U3akM"
    }
  }
}

As shown . this works perfectly . now as mentioned the AuthPayload return the token and the users type when i try to mutate with user :-

mutation{
login(email :"dondala422@hotmail.com" password :"aa")
{
  token
    users{
    USERNAME
    FIRST_NAME
  }
}
}

it gives me this error

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field AuthPayload.users.",
      "locations": [
        {
          "line": 4,
          "column": 5
        }
      ],
      "path": [
        "login",
        "users"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Cannot return null for non-nullable field AuthPayload.users.",
            "    at completeValue (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:595:13)",
            "    at completeValueCatchingError (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:530:19)",
            "    at resolveField (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:461:10)",
            "    at executeFields (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:297:18)",
            "    at collectAndExecuteSubfields (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:748:10)",
            "    at completeObjectValue (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:738:10)",
            "    at completeValue (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:626:12)",
            "    at completeValue (C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:592:21)",
            "    at C:\\Users\\donda\\Desktop\\New folder (3)\\prisma-examples\\javascript\\graphql-auth - Copy\\node_modules\\graphql\\execution\\execute.js:527:16"
          ]
        }
      }
    }
  ],
  "data": null
}

i tried to attach the debugger to see where is the null occur and i didn't found any nullable values

here is a picture of vscode before return value , the token and user object are defined VSCode Debugger picture


Solution

  • The object returned inside your resolver includes a property named user, but your field is named users. Since users is undefined, it resolves to null, but the field is non-nullable, so an error is thrown instead.