无法为不可为空的字段 Mutation.createUser语言 - Apollo Server 返回 null



我正在用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
}
}

但是当我使用此命令执行突变创建用户时,返回以下内容:

{
"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

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

最新更新