如果我在應用程序中使用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'? –