【发布时间】:2019-09-17 12:16:14
【问题描述】:
不久前,我使用 GraphQL-Yoga 和 Prisma 做了几个网站,并尝试使用 Apollo Express 做同样的事情。一切正常。但是现在我无法开始这件事——也许我走得太快了,一次尝试了太多事情。我开始尝试按照this 指南实施注册突变。我的第一个(也是目前唯一的)突变是
const resolvers = {
Mutation: {
register: async (parent, { username, password }, ctx, info) => {
const hashedPassword = await bcrypt.hash(password, 10)
const user = await ctx.prisma.createUser({
username,
password: hashedPassword,
})
return user
},
},
}
但是当我在 GraphQL Playground 中测试它时
mutation {
register(username:"foo",password: "bar") {
id
username
}
}
我收到这个错误
"errors": [
{
"message": "Variable '$data' expected value of type 'UserCreateInput!' but got: {\"username\":\"sandor\",\"password\":\"$2a$10$S7IF1L3YjS4dLUm8QB2zjenPnKHwlohml7AaPf1iVhNipHSkNb/62\"}. Reason: 'name' Expected non-null value, found null. (line 1, column 11):\nmutation ($data: UserCreateInput!) {\n ^",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"register"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
我查看了“生成的/prisma-client/prisma-schema”,发现我有
createUser(data: UserCreateInput!): User!
但是当我使用 Prisma 之前,它从来没有抱怨过当我传递一个对象时期望一个输入类型。我哪里错了?
我的架构是
const { gql } = require('apollo-server-express');
const typeDefs = gql`
type User {
id: ID!
username: String!
password: String!
}
type Query {
currentUser: User!
}
type Mutation {
register(username: String!, password: String!): User!
login(username: String!, password: String!): LoginResponse!
}
type LoginResponse {
id: ID!
token: String
user: User
}
`
module.exports = typeDefs
我的 datamodel.prisma 是
type User {
id: ID! @id
name: String!
password: String!
}
type LoginResponse {
id: ID! @id
token: String
user: User
}
我确信我遗漏了一些非常明显的东西,但看了一段时间后我没有看到它——所以任何线索都会非常感激!
【问题讨论】:
标签: graphql apollo apollo-server prisma prisma-graphql