【发布时间】:2019-05-31 15:51:28
【问题描述】:
我正在尝试通过 react-apollo 设置 GraphQL 订阅查询的 setState()。我的目的是存储我从 GraphQL 收到的完整对象,并将其保存到 App.js 文件的 ComponentDidMount() 方法中的状态中。
我已经尝试了很多方法让它工作,例如 react-apollo 的SubscribetoMore 端点,但我认为我正在为它编写错误的反应代码。
/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 });
}
}
订阅查询
const SUBSCRIPTION_SYNC = `
subscription syncState{
cotd {
_id_2
name
desc
image
price
status
}
}`;
【问题讨论】:
标签: javascript reactjs graphql react-apollo graphql-subscriptions