在创建帖子的变体中,我正在使用以下代码:
async function savePost(parent, args, context, info){
let data = {...args}
delete data.categories
delete data.status
if (!context.request.userId) {
throw new Error('Please SignIn to continue.')
}
const post = await context.prisma.createPost({
author: {
connect: { id: context.request.userId }
},
categories: {
set: args.categories,
},
status: args.status,
...data
})
return post
}
这会将帖子连接到数据库中的作者。我是通过使用数据库游乐场来了解这一点的。
但是,当我console.log postToUpdate时,它返回`author:null! 当我在客户端使用gql时,会发生相同的事情。该帖子没有作者!
尽管作者已保存在数据库中,但在客户端和服务器端我看不到他/她。
作为参考,这是数据模型
enum Previledge {
SUPERADMIN
ADMIN
MODERATOR
AUTHOR
READER
}
enum Category {
TECH
FIN
DIGIMARK
CODING
TUTORIAL
HOWTO
WRITING
INSPIRE
SCIENCE
POLITICS
LIFESTYLE
FOOD
BUSINESS
ENTREPRENEUR
HISTORY
HEALTH
PET
PARENTHOOD
TRAVEL
INDIA
CHINA
US
UK
WORLD
NEWS
REVIEW
}
enum PostStatus {
PUBLISHED
DRAFT
DELETED
}
type Post {
id: ID! @unique
title: String!
editorSerializedOutput: Json!
editorCurrentContent: Json!
editorHtml: String!
updatedAt: DateTime!
createdAt: DateTime!
author: User! @relation(name: "PostsAndUser")
categories: [Category]!
thumbnail: Json!
status: PostStatus!
}
type User {
id: ID! @unique
socialId: String! @unique
fname: String!
lname: String!
name: String!
phone: String @unique
email: String! @unique
gender: String
birthday: String
bio: String
posts: [Post]! @relation(name: "PostsAndUser")
profilePicture: String!
followers: [User]!
previledge: [Previledge]!
signUpMethod: String!
accessToken: String!
updatedAt: DateTime!
createdAt: DateTime!
}
这是整个架构:
# import * from './generated/prisma.graphql'
scalar DateTime
type Message {
code: Int
message: String
}
type Mutation {
signIn(
socialId: String!
fname: String!
lname: String!
name: String!
phone: String
email: String!
gender: String
birthday: String
bio: String
profilePicture: String!
signUpMethod: String!
accessToken: String!
): User!
signOut: Message
savePost(
title: String!
editorSerializedOutput: Json!
editorCurrentContent: Json!
editorHtml: String!
categories: [Category]!
thumbnail: Json!
status: PostStatus!
): Post!
}
type Query {
users: [User]!
me: User
canUpdatePost(id: ID!): Post
}
type User {
id: ID!
fname: String!
lname: String!
name: String!
phone: String
email: String!
gender: String
birthday: String
bio: String
posts(where: PostWhereInput, orderBy: PostOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): [Post!]
profilePicture: String!
followers(where: UserWhereInput, orderBy: UserOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): [User!]
previledge: [Previledge]!
signUpMethod: String!
updatedAt: String!
createdAt: String!
}
答案 0 :(得分:0)
据我所知,如果您不指定createPost
的第二个参数,它将仅返回标量字段。
试试:
const post = await context.prisma.createPost({
author: {
connect: { id: context.request.userId }
},
categories: {
set: args.categories,
},
status: args.status,
...data
}, `{
author {
id
}
}`)
console.log(post)
此外,您的架构中似乎缺少类型Post
,您可能需要再次生成它。
答案 1 :(得分:0)
您需要将info
作为第二个参数传递给Prisma绑定的createPost
函数。
有可能从客户端查询author
字段以响应savePost
突变,但是由于没有第二个参数传递给createPost
,prisma仅返回标量字段。由于author
类型中的Post
字段是按要求定义的,因此,如果从客户端查询了author
但解析程序未返回
const post = await context.prisma.createPost({
author: {
connect: { id: context.request.userId }
},
categories: {
set: args.categories,
},
status: args.status,
...data
})
,则GraphQL验证将失败。
更改此:
const post = await context.prisma.createPost({
author: {
connect: { id: context.request.userId }
},
categories: {
set: args.categories,
},
status: args.status,
...data
}, info)
收件人:
{{1}}