使用redux observable管理react apollo

问题描述 投票:1回答:1

我想用redux observable管理我的提取,但我找不到任何例子。有人有答案吗?

通过apollo vanillaJS这是获取我的查询结果的方法:

const opts = {uri: 'http://localhost:8080/graphql'};
const networkInterface = createNetworkInterface(opts);
const client = new ApolloClient({
networkInterface});

let query = gql`query {
                    viewer {
                        id
                        firstName
                        lastName
                        defaultTimeZoneId
                        defaultLanguage
                    }
                }`;
client.query({query}).then((results) => {})
//return a promise

现在我需要在redux observable中插入它来管理答案:

export const fetchActionGridDataEpic = action$ => 
  action$.ofType('FETCHING_ACTION_GRID_DATA')
//Execute the query then if networkStatus === 7 send the result to FETCHING_ACTION_GRID_DATA_FULFILLED as a payload
  .mapTo({ type: 'FETCHING_ACTION_GRID_DATA_FULFILLED' })....

谢谢

reactjs redux react-apollo redux-observable
1个回答
1
投票

您可以像这样使用运算符fromPromise

const fetchQuestionsEpic = action$ =>
  action$.ofType(FETCH_QUESTIONS)
  .mergeMap(() =>
    Observable.fromPromise(fetchQuestions())
      .map(questions => ({ type: QUESTIONS_FETCHED, questions })),
  );

这个回购遵循一些良好的做法github.com/voteetvous

© www.soinside.com 2019 - 2024. All rights reserved.