I am trying to setState() of the GraphQL subscription Query that I am doing through react-apollo. My purpose is to store the complete the object that I received from GraphQL and save it into the state in the ComponentDidMount()
method of the App.js file.
I have tried a lot of ways for it to work such as SubscribetoMore
endpoint by react-apollo, but I think I am writing the wrong react code for it.
/App.js
const haveAnyFish = () => (
<Subscription subscription={gql`${SUBSCRIPTION_SYNC}`} >
{({ loading, error, data }) => {
if (loading) return <p>Loading...</p>;
if (error) return <p>Error :</p>;
return (
<div>
<div>{fishes(data)}</div>
</div>
);
}}
</Subscription>
);
/App.js
class App extends React.Component {
state = {
fishes: {},
order: {}
};
componentDidMount() {
const fishes = (data) => {
this.setState({ fishes: data });
}
}
Subscription Query
const SUBSCRIPTION_SYNC = `
subscription syncState{
cotd {
_id_2
name
desc
image
price
status
}
}`;