javascript - 未捕获错误 : react-apollo only supports a query, 订阅,或每个 HOC 的突变

标签 javascript reactjs graphql apollo react-apollo

我正在尝试使用 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/

相关文章:

javascript - 如何修复 ReactJS 中的 "too much recursion"错误?

javascript - 获取 Stripe 调用以在 GraphQL 架构中解析

javascript - 带有展开图标的 Accordion

javascript - 在 PHP 和 JavaScript 之间传递变量

javascript - 在 HTML 页面中的 PDF 文档上添加触摸目标

javascript - 选择输入 radio 或其他时,如何更改范围 slider 的范围?

reactjs - 如何在 React 应用程序之间共享 redux 代码

javascript - react + typescript : Property * is missing in type *

graphql - 如何在 GraphQL-Dotnet 中将父类型值作为参数传递给子类型?

python - 使用 FastAPI 在基于 Python 的 GraphQL 服务器中进行身份验证验证