无法为不可为空的字段返回 null,调试器不显示 null [重复]

Posted

技术标签:

【中文标题】无法为不可为空的字段返回 null,调试器不显示 null [重复]【英文标题】:Cannot return null for non-nullable field , Debugger dosen't show null [duplicate] 【发布时间】:2020-11-30 15:15:26 【问题描述】:

我有这个 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!

突变:-

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,
        
      ,
    )
  ,
)

我的问题是当我尝试使用令牌返回值更改登录方法时,它运行良好,这是我的更改

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

  token


回应


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

如图所示。这完美地工作。现在如前所述,AuthPayload 返回令牌和用户类型 当我尝试与用户发生变异时:-

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

  token
    users
    USERNAME
    FIRST_NAME
  


它给了我这个错误


  "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

我试图附加调试器以查看 null 出现在哪里 而且我没有找到任何可以为空的值

这是vscode在返回值之前的图片,定义了token和user对象 VSCode Debugger picture

【问题讨论】:

【参考方案1】:

解析器中返回的对象包含一个名为user 的属性,但您的字段名为users。由于users 未定义,它解析为null,但该字段不可为空,因此会引发错误。

【讨论】:

以上是关于无法为不可为空的字段返回 null,调试器不显示 null [重复]的主要内容,如果未能解决你的问题,请参考以下文章

GraphQL 查询返回错误“不能为不可为空的字段返回 null”

Schema graphql 错误给出“不能为不可为空的字段 Organisation.id 返回 null”为啥?

AWS放大graphql突变:不能为不可为空的字段返回null

Gatsby graphiQL - 不能为不可为空的字段 ImageSharpFluid.src 返回 null

使用 Graphql 在 NestJS 上订阅时,“不能为不可为空的字段返回 null”

错误无法为不可为空的类型返回 null:父 MyModelType 中的“字符串”(/createMyModelType/id)