我将 Prisma 与 GraphQL 一起使用,并在运行 mutatioin 时出错。我成功部署了 prisma 并将其与本地 graphQL 绑定。
-- datamodel.graphql - 棱镜设置
type Link {
id: ID! @unique
description: String!
url: String!
postedBy: User
}
type User {
id: ID! @unique
name: String!
email: String! @unique
password: String!
links: [Link!]!
}
-- schema.graphql - 本地设置
# import Link from "./generated/prisma.graphql"
type Query {
info: String!
feed: [Link!]!
}
type Mutation {
post(url: String!, description: String!): Link!
signup(email: String!, password: String!, name: String!): AuthPayload
login(email: String!, password: String!): AuthPayload
}
type AuthPayload {
token: String
user: User
}
type User {
id: ID!
name: String!
email: String!
links: [Link!]!
}
注册突变的解析器是
async function signup(parent, args, context, info) {
// 1
const password = await bcrypt.hash(args.password, 10)
// 2
const user = await context.db.mutation.createUser({
data: { ...args, password },
}, `{ id }`)
// 3
const token = jwt.sign({ userId: user.id }, APP_SECRET)
// 4
return {
token,
user,
}
}
这是 .graphqlconfig.yml 内容
projects:
app:
schemaPath: src/schema.graphql
extensions:
endpoints:
default: http://localhost:4000
database:
schemaPath: src/generated/prisma.graphql
extensions:
prisma: database/prisma.yml
我运行的 GraphQL 查询是 .
mutation {
signup(
name: "Alice"
email: "alice@graph.cool"
password: "graphql"
) {
token
user {
id
}
}
}
当我运行它时得到的响应是
{
"data": {
"signup": null
},
"errors": [
{
"message": "Variable '$_data' cannot be non input type 'UserCreateInput!'. (line 1, column 19):\nmutation ($_data: UserCreateInput!) {\n ^",
"locations": [],
"path": [
"createUser"
]
}
]
}
我找不到这个的原因。
谢谢你。