如果我在应用程序中使用REDX和APOLLO客户端,从组件外的操作触发查询的最佳方式是什么?如何从REDX操作调用阿波罗客户端查询
例如,如果我有一个标准的应用程序,配置了redux和apollo客户端,我应该如何触发一个“刷新”列表。我可以在组件本身上触发一个具有gql的函数,但是如何从一个更符合流量的操作中执行此操作。
import React, { Component, PropTypes } from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import { connect } from 'react-redux';
import { refreshProfile } from './actions';
class Profile extends Component { ... }
Profile.propTypes = {
data: PropTypes.shape({
loading: PropTypes.bool.isRequired,
user: PropTypes.object,
}).isRequired,
};
const UserQuery = gql`
query getUser {
user {
id
name
}
}
`;
const ProfileWithData = graphql(UserQuery)(Profile);
const ProfileWithDataAndState = connect(
(state) => ({ user: state.user })),
)(ProfileWithData);
而且,我想触发一个动作来刷新用户数据?由于逻辑在组件本身中,所以我不确定如何从动作本身触发该gql查询。
你有没有办法做到这一点? –
是的,你可以直接使用apollo客户端,就像我在示例代码中的答案。 – MonkeyBonkey
并从其他组件? 'withApollo'? –