【发布时间】:2023-04-08 13:38:02
【问题描述】:
我正在尝试返回包含以下内容的订阅:
const postsSubscription = gql`
subscription postAdded {
postAdded {
id
title
description
author{
name
}
}
}
`
发生的情况是作者是用户类型,我只是传递了一个作者 ID。这意味着我在创建帖子时没有作者姓名:
createPost: async (root, req, { posts }) => {
const Item = {
id: uuid.v4(),
authorId: '565dbdc0-36f2-4bba-be67-c126d0c71fff',
...req
}
await posts.create({ Item })
pubsub.publish('postAdded', { postAdded: Item })
return Item
},
这是作者解析器:
Post: {
author: async({ authorId }, req, { users }) => {
const Key = { id: authorId }
const { Item } = await users.get({ Key })
return Item
}
}
这是架构:
type Post {
id: ID
title: String
description: String
author: User @relation(name: "PostAuthor")
}
type User {
id: ID
name: String
email: String
password: String
posts: [Post] @relation(name: "UserPosts")
}
type PostPayload {
post: Post
}
type CreateUserPayload {
user: User
}
type Query {
allPosts: [Post]
allUsers: [User]
post(id: ID!): Post
user(id: ID!): User
}
type Mutation {
createPost(input: CreatePostInput!): PostPayload
updatePost(input : UpdatePostInput!): PostPayload
createUser(input : CreateUserInput!): CreateUserPayload
}
type Subscription {
postAdded: Post
}
input CreatePostInput {
title: String!
description: String!
}
input UpdatePostInput {
id: ID!
title: String!,
description: String!
}
input CreateUserInput {
name: String!
email: String!
password: String!
}
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
那么,我的问题是,如何将所有必填字段(包括连接)传递给订阅?
【问题讨论】:
-
这个字段解析器被调用了吗?您也可以创建复制品或共享架构吗?
-
你去@Urigo 它正在工作,因为当我查询
allPosts或post时,我得到了作者。但我无法在订阅中获取此信息。所以它打破了,说它需要author
标签: graphql apollo react-apollo apollo-client