我正在为我的个人项目使用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
答案 0 :(得分:0)
当我测试突变时,我的终端中弹出了同样的消息。在我工作的项目中,我将解析器放在与执行突变的命令不同的文件中(在您的情况下,类似于第三张图像)在搜索错误 30 分钟后,我意识到我的解析器没有被导出正确。我有 module.export = { resolverName }
而不是 module.exports = { resolverName }
,因此我的突变命令无法与我的解析器通信。