首页 > 解决方案 > 无法为不可为空的字段 Mutation.createUser 返回 null - Apollo Server

问题描述

我正在为我的个人项目使用 ApolloServer 训练 ReactJS。我用你的查询和突变创建了一个类型用户。

type User {
id: ID!      
name: String
password: String!
email: String!  
}

type Query {
  users: [User!]!
  user(id: ID!): User
}

input UserInput{
name: String
password: String!
email: String! 
}

type Mutation {
  createUser(data: UserInput): User!
  updateUser(id: ID!, data: UserInput!): User!
  deleteUser(id: ID!): Boolean
}

我的突变:

createUser: async (_, { data }, { pubsub }) => {
      console.log(data)
      const { password, ...rest } = data
      const hashPassword = await bcrypt.hashSync(password, 10)
      const user = await prisma.user.create({
        data: {
          password: hashPassword,
          ...rest
        }
      })
      /* pubsub.publish(USER_ADDED, {
        userAdded: user
      }) */
      return user
    },

我用来执行突变的命令:

mutation {
  createUser(
    data:{
      name: "Renato",
      password: "12345",
      email: "renato@email.com"
    }
  ){
    id
  }
}

但是当我用这个命令执行一个突变 createUser 时,返回这个:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Mutation.createUser.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "createUser"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Cannot return null for non-nullable field Mutation.createUser.",
            "    at completeValue (/home/renato/Documents/workspace/rntjr/react-graphql-authentication-example/server/node_modules/graphql/execution/execute.js:560:13)",
            "    at completeValueCatchingError (/home/renato/Documents/workspace/rntjr/react-graphql-authentication-example/server/node_modules/graphql/execution/execute.js:495:19)",
            "    at resolveField (/home/renato/Documents/workspace/rntjr/react-graphql-authentication-example/server/node_modules/graphql/execution/execute.js:435:10)",
            "    at /home/renato/Documents/workspace/rntjr/react-graphql-authentication-example/server/node_modules/graphql/execution/execute.js:244:18",
            "    at /home/renato/Documents/workspace/rntjr/react-graphql-authentication-example/server/node_modules/graphql/jsutils/promiseReduce.js:23:10",
            "    at Array.reduce (<anonymous>)",
            "    at promiseReduce (/home/renato/Documents/workspace/rntjr/react-graphql-authentication-example/server/node_modules/graphql/jsutils/promiseReduce.js:20:17)",
            "    at executeFieldsSerially (/home/renato/Documents/workspace/rntjr/react-graphql-authentication-example/server/node_modules/graphql/execution/execute.js:241:37)",
            "    at executeOperation (/home/renato/Documents/workspace/rntjr/react-graphql-authentication-example/server/node_modules/graphql/execution/execute.js:219:55)",
            "    at executeImpl (/home/renato/Documents/workspace/rntjr/react-graphql-authentication-example/server/node_modules/graphql/execution/execute.js:104:14)"
          ]
        }
      }
    }
  ],
  "data": null
}

我的github项目进行分析:https ://github.com/rntjr/react-graphql-authentication-example/tree/develop/server

标签: node.jsjsongraphqlapollo-server

解决方案


当我测试突变时,我的终端中弹出了同样的消息。在我正在工作的项目中,我将解析器放在与执行突变的命令不同的文件中(在你的情况下,类似于第三张图像)在搜索错误 +30 分钟后,我意识到我的解析器没有被导出正确。我有module.export = { resolverName }module.exports = { resolverName }因此我的突变命令无法与我的解析器通信。


推荐阅读