Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught Error: react-apollo only supports a query, subscription, or a mutation per HOC

I'm trying to wrap my Chat component with two queries and one mutation using compose.

However, I'm still getting the following error in the console:

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

Here are my queries and the export statement:

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

Apparently, the issue is with the findConversations query. If I comment it out, I don't get the error and the component loads properly:

// this works
export default compose(
  // graphql(findConversations, {name: 'findConversationsQuery'}),
  graphql(allMessages, {name: 'allMessagesQuery'}),
  graphql(createMessage, {name : 'createMessageMutation'})
)(Chat)

Can anyone tell me what I'm missing?

By the way, I also have a subscription set up on the allMessagesQuery, in case that's relevant:

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

}
like image 781
nburk Avatar asked Feb 25 '17 14:02

nburk


People also ask

How to handle Apollo Client error?

When a network error occurs, Apollo Client adds it to the error. networkError field returned by your useQuery call (or whichever operation hook you used). You can add retry logic and other advanced network error handling to your application with Apollo Link.

What is options in GraphQL?

options is an object or a function that allows you to define the specific behavior your component should use in handling your GraphQL data. The specific options available for configuration depend on the operation you pass as the first argument to graphql() . There are options specific to queries and mutations.

How do you use useQuery in react?

We run a query within a React component by calling useQuery and passing it our GraphQL query string. This makes running queries from React components a breeze. When our component renders, useQuery returns an object from Apollo Client that contains loading , error , and data properties that we can use to render our UI.


1 Answers

Your findConversationsQuery is actually two queries. This one:

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

And this one:

{
    id
}

The entire query needs to be enclosed between a single pair of opening and closing brackets.

I think what you meant to write is:

query allConversations($customerId: ID!) {
    allConversations(filter: { customerId: $customerId }){
        id
    }
} 
like image 149
helfer Avatar answered Oct 10 '22 20:10

helfer