我正在尝试使用 compose
将我的 Chat
组件包装成两个查询和一个突变。
但是,我仍然在控制台中收到以下错误:
Uncaught Error:
react-apollo
only supports a query, subscription, or a mutation per HOC.[object Object]
had 2 queries, 0 subscriptions and 0 mutations. You can use 'compose
' to join multiple operation types to a component
这是我的查询和导出语句:
// this query seems to cause the issue
const findConversations = gql`
query allConversations($customerId: ID!) {
allConversations(filter: {
customerId: $customerId
})
} {
id
}
`
const createMessage = gql`
mutation createMessage($text: String!, $conversationId: ID!) {
createMessage(text: $text, conversationId: $conversationId) {
id
text
}
}
`
const allMessages = gql`
query allMessages($conversationId: ID!) {
allMessages(filter: {
conversation: {
id: $conversationId
}
})
{
text
createdAt
}
}
`
export default compose(
graphql(findConversations, {name: 'findConversationsQuery'}),
graphql(allMessages, {name: 'allMessagesQuery'}),
graphql(createMessage, {name : 'createMessageMutation'})
)(Chat)
显然,问题出在 findConversations
查询上。如果我将其注释掉,我不会收到错误并且组件会正确加载:
// this works
export default compose(
// graphql(findConversations, {name: 'findConversationsQuery'}),
graphql(allMessages, {name: 'allMessagesQuery'}),
graphql(createMessage, {name : 'createMessageMutation'})
)(Chat)
谁能告诉我我错过了什么?
顺便说一句,我还在 allMessagesQuery
上设置了订阅,以防相关:
componentDidMount() {
this.newMessageSubscription = this.props.allMessagesQuery.subscribeToMore({
document: gql`
subscription {
createMessage(filter: {
conversation: {
id: "${this.props.conversationId}"
}
}) {
text
createdAt
}
}
`,
updateQuery: (previousState, {subscriptionData}) => {
...
},
onError: (err) => console.error(err),
})
}
最佳答案
您的 findConversationsQuery
实际上是两个查询。这个:
query allConversations($customerId: ID!) {
allConversations(filter: {
customerId: $customerId
})
}
还有这个:
{
id
}
整个查询需要包含在一对左括号和右括号之间。
我想你的意思是:
query allConversations($customerId: ID!) {
allConversations(filter: { customerId: $customerId }){
id
}
}
关于javascript - 未捕获错误 : react-apollo only supports a query, 订阅,或每个 HOC 的突变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42457480/