如果我在我的应用程序中使用 redux 和 apollo 客户端,从组件外部的操作触发查询的最佳方法是什么。
例如,如果我有一个标准应用程序,配置了 redux 和 apollo 客户端,我应该如何触发“刷新”列表。我可以在具有 gql 的组件本身上触发一个函数,但是我将如何从一个更符合通量的 Action 中做到这一点。
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 查询。
最佳答案
我需要在我的 actions.js 中使用 ApolloClient。例如
import ApolloClient, { createNetworkInterface } from 'apollo-client';
const networkInterface = createNetworkInterface({
uri: config.graphCoolUri,
});
const client = new ApolloClient({
networkInterface,
dataIdFromObject: r => r.id,
});
const { data } = await client.query({
query: UserQuery
});
关于react-apollo - 如何从 redux 操作调用 apollo 客户端查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40770350/