Graphql 对于不可为null的字段,调试器dosen';不显示空值

Graphql 对于不可为null的字段,调试器dosen';不显示空值,graphql,apollo,prisma,Graphql,Apollo,Prisma,我有这个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): AuthPaylo

我有这个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
}
我试图附加调试器以查看空值出现的位置 我没有发现任何可为空的值

下面是vscode在返回值之前的图片,定义了令牌和用户对象

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

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