如何将道具从Redux商店传递到Apollo GraphQL查询中?[英] How To Pass Props From Redux Store into Apollo GraphQL Query

本文是小编为大家收集整理的关于如何将道具从Redux商店传递到Apollo GraphQL查询中?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我只是在一个简单的React本机应用程序上使用Apollo GraphQl,对它的作用给人留下了深刻的印象.但是我并没有完全围绕它与Redux商店集成.本质上,我需要将一些用户输入从我的searchReducer传递到我的GraphQl查询中.我以为我可以简单地将连接的组件传递到我的GraphQl查询并提供一个变量,但是找不到prop searchInput.这是代码:

import React, { Component } from 'react';
import { FlatList, Text, StyleSheet } from 'react-native';
import { connect } from 'react-redux';
import Repository from './Repository';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';

const query = gql`
    query repositoryOwner($login: String!) {
        repositoryOwner(login: $login) {
            url,
            avatarUrl(size: 100),
            repositories(first: 5) {
                nodes {
                    name,
                    description
                }
            }
        }
    }`;

class RepositoryList extends Component {

    renderItem = ({ item }) => {
        return <Repository name={item.name} description={item.description} />;
    }

    render() {

        const { repositoryOwner } = this.props.data;
        const data = repositoryOwner ? repositoryOwner.repositories.nodes : [];

        return (
            <FlatList data={data}
                renderItem={(repo) => this.renderItem(repo)} keyExtractor={(item, index) => index} />
        );
    }
}

const mapStateToProps = (state) => {

    return {
        search: state.searchReducer
    };
};

const withStore = connect(mapStateToProps)(RepositoryList);

export default graphql(query, {
    options: ({ search: { searchInput }}) => ({ variables: { login: searchInput }})
})(withStore);

我认为,通过传递connect ed React组件,它将找到search prop mapStateToProps指定的search prop.但是,它给出:

typeError:Undefined不是对象(评估_ref3.search.searchinput).

显然没有发现prop.我的问题是,从Redux Store用作Apollo GraphQl连接组件中的变量的惯用方法是什么?

?

推荐答案

创建一个更详细信息的答案,对其他用户很有用:

使用Redux connect高阶组件的Props确保该功能是最后应用的.这可以在您的示例中

完成
const WithGraphql = graphql(/* ... */)(RepositoryList);

export default connect(mapStateToProps)(WithGraphql);

另外,您可以从 redux 或 react-apollo 中使用compose. 撰写从最后到第一的功能.对于两个参数,可以写为以下内容:

compose = (f, g) => (...args) => f(g(...args))

确保首先在此处列出连接,然后列出GraphQL.这会创建一个新功能,然后您必须将其应用于组件RepositoryList:

export default compose(
  connect(mapStateToProps),
  graphql(/* ... */),
)(RepositoryList); 

本文地址:https://www.itbaoku.cn/post/1938134.html

问题描述

I'm just getting going with Apollo GraphQL on a simple React Native app and am really impressed by what it can do. But I'm not quite wrapping my head around how it integrates with the Redux store. Essentially, I need to pass some user input from my searchReducer into my GraphQL query. I thought I could simply pass the connected component to my GraphQL query and supply a variable, but it's not finding the prop searchInput. Here's the code:

import React, { Component } from 'react';
import { FlatList, Text, StyleSheet } from 'react-native';
import { connect } from 'react-redux';
import Repository from './Repository';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';

const query = gql`
    query repositoryOwner($login: String!) {
        repositoryOwner(login: $login) {
            url,
            avatarUrl(size: 100),
            repositories(first: 5) {
                nodes {
                    name,
                    description
                }
            }
        }
    }`;

class RepositoryList extends Component {

    renderItem = ({ item }) => {
        return <Repository name={item.name} description={item.description} />;
    }

    render() {

        const { repositoryOwner } = this.props.data;
        const data = repositoryOwner ? repositoryOwner.repositories.nodes : [];

        return (
            <FlatList data={data}
                renderItem={(repo) => this.renderItem(repo)} keyExtractor={(item, index) => index} />
        );
    }
}

const mapStateToProps = (state) => {

    return {
        search: state.searchReducer
    };
};

const withStore = connect(mapStateToProps)(RepositoryList);

export default graphql(query, {
    options: ({ search: { searchInput }}) => ({ variables: { login: searchInput }})
})(withStore);

I thought that by passing the connected React component, it would find the search prop specified by mapStateToProps. However, it gives:

TypeError: undefined is not an object (evaluating _ref3.search.searchInput).

So obviously it's not finding that prop. My question is, what is the idiomatic way to use props from the Redux store as variables in an Apollo GraphQL connected component?

推荐答案

To create an answer that goes more into detail and is useful for other users:

To use props from the redux connect higher order component make sure that the function is applied last. This can be done in your example by

const WithGraphql = graphql(/* ... */)(RepositoryList);

export default connect(mapStateToProps)(WithGraphql);

Alternatively you can use compose from redux or react-apollo. Compose applies functions from last to first. For two arguments compose can be written as the following:

compose = (f, g) => (...args) => f(g(...args))

Make sure to list connect first here and then graphql. This creates a new function that you then have to apply to your component RepositoryList:

export default compose(
  connect(mapStateToProps),
  graphql(/* ... */),
)(RepositoryList);