无法为非空字段返回null,调试器不显示null

时间:2020-08-11 00:49:14

标签: graphql apollo prisma

我有这个schema.graphql

defaultLow

变异:-

### 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
}
}

如图所示。这完美地工作。现在,如前所述,AuthPayload返回令牌,用户输入 当我尝试与用户进行变异时:-

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

它给了我这个错误

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

我尝试附加调试器,以查看在哪里出现空值 而且我没有发现任何可空值

这是返回值之前的vscode的图片,定义了令牌和用户对象 VSCode Debugger picture

1 个答案:

答案 0 :(得分:2)

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