未捕获错误:react-apollo仅支持每个HOC的查询,订阅或突变

时间:2017-02-25 14:45:35

标签: javascript reactjs graphql apollo react-apollo

我正在尝试使用Chatcompose组件包含两个查询和一个变异。

但是,我仍然在控制台中收到以下错误:

  

未捕获错误: react-apollo仅支持每个HOC的查询,订阅或突变。 [object Object]有2个查询,0个订阅和0个突变。您可以使用“compose”将多种操作类型连接到组件

以下是我的查询和导出声明:

// 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),
  })

}

1 个答案:

答案 0 :(得分:5)

您的findConversationsQuery实际上是两个查询。这一个:

query allConversations($customerId: ID!) {
    allConversations(filter: {
      customerId: $customerId
    })
} 

这一个:

{
    id
}

整个查询需要包含在一对开始和结束括号中。

我认为你的意思是:

query allConversations($customerId: ID!) {
    allConversations(filter: { customerId: $customerId }){
        id
    }
}