run mutation to store new user's post return Error: Variable '$data' expected value of type
run mutation to store new user's post return Error: Variable '$data' expected value of type
我使用 graphql-yoga 和 prisma 创建了用户和 post 数据模型。一个用户可以有多个 post
我确实喜欢下面。现在根据 addPost
突变,我想添加与当前用户相关的新 post。但是graphql服务器return出现以下错误
但是当我 运行 突变时:
mutation {
addPost(title:"aa", body: "bb"){
id
title
author {
id
email
}
}
}
给我这个错误:为什么会这样?我该如何解决这个问题?
"errors": [
{
"message": "Error: Variable '$data' expected value of type 'PostCreateInput!' but got: {\"title\":\"aa\",\"body\":\"bb\",\"author\":{\"id\":\"5f036dc924aa9a00070c4771\",\"email\":\"bb@bb.com\"}}. Reason: 'author.id' Field 'id' is not defined in the input type 'UserCreateOneInput'. (line 1, column 11):\nmutation ($data: PostCreateInput!)
数据model.prisma
type User {
id: ID! @id
email: String! @unique
password: String!
}
type Post {
id: ID! @id
title: String!
body: String!
author_id: Int
author: User! @relation(link: INLINE,fields: [authorId], references: [id])
createdAt: DateTime @createdAt
updatedAt: DateTime @updatedAt
}
架构:
type User {
id: ID!
email: String!
}
type Post {
id: ID!
title: String!
body: String!
author: User!
}
input UserInput {
id: ID!
email: String!
}
type Mutation {
addPost(title: String!, body: String!) : Post!
}
解析器。
addPost: async (parent, { title, body }, { prisma, me }) => {
console.log(me);
try {
const post = await prisma.createPost({
title,
body,
author: {id: me.user.id, email: me.user.email}
});
return post;
} catch (error) {
throw new Error(error);
}
}
应该是:
const post = await prisma.createPost({
title,
body,
author: {
connect: { id: me.user.id, email: me.user.email }
}
});
这会将 post 添加到现有用户。
此外,如果您刚开始使用 Prisma,我会推荐 Prisma 2
我使用 graphql-yoga 和 prisma 创建了用户和 post 数据模型。一个用户可以有多个 post
我确实喜欢下面。现在根据 addPost
突变,我想添加与当前用户相关的新 post。但是graphql服务器return出现以下错误
但是当我 运行 突变时:
mutation {
addPost(title:"aa", body: "bb"){
id
title
author {
id
email
}
}
}
给我这个错误:为什么会这样?我该如何解决这个问题?
"errors": [
{
"message": "Error: Variable '$data' expected value of type 'PostCreateInput!' but got: {\"title\":\"aa\",\"body\":\"bb\",\"author\":{\"id\":\"5f036dc924aa9a00070c4771\",\"email\":\"bb@bb.com\"}}. Reason: 'author.id' Field 'id' is not defined in the input type 'UserCreateOneInput'. (line 1, column 11):\nmutation ($data: PostCreateInput!)
数据model.prisma
type User {
id: ID! @id
email: String! @unique
password: String!
}
type Post {
id: ID! @id
title: String!
body: String!
author_id: Int
author: User! @relation(link: INLINE,fields: [authorId], references: [id])
createdAt: DateTime @createdAt
updatedAt: DateTime @updatedAt
}
架构:
type User {
id: ID!
email: String!
}
type Post {
id: ID!
title: String!
body: String!
author: User!
}
input UserInput {
id: ID!
email: String!
}
type Mutation {
addPost(title: String!, body: String!) : Post!
}
解析器。
addPost: async (parent, { title, body }, { prisma, me }) => {
console.log(me);
try {
const post = await prisma.createPost({
title,
body,
author: {id: me.user.id, email: me.user.email}
});
return post;
} catch (error) {
throw new Error(error);
}
}
应该是:
const post = await prisma.createPost({
title,
body,
author: {
connect: { id: me.user.id, email: me.user.email }
}
});
这会将 post 添加到现有用户。
此外,如果您刚开始使用 Prisma,我会推荐 Prisma 2