如何将数据连接到react-router-relay中的深层组件?

时间:2017-03-07 15:41:32

标签: functional-programming react-router graphql relay react-router-relay

我有这样的路线

<Route path="/search" component={Search}>

基本Search组件看起来像这样

class Search extends React.Component {
  constructor (props) {
    super(props)
    this.state = {query: ''}
  }
  handleSubmit (event) {
    event.preventDefault()
    this.setState({query: this.refs.queryInput.value})
  }
  renderSearchResult() {
    if (this.state.query === '')
      return <EmptySearchResult />
    else
      return <SearchResult query={this.state.query}/>
  }
  render() {
    return (
      <div className="searchContainer">
        <div className="row">
          <div className="search">
            <form onSubmit={event => this.handleSubmit(event)}>
              <input className="searchInput" placeholder="robocop" ref="queryInput" />
            </form>
          </div>
        </div>
        <div className="row">
          {this.renderSearchResult()}
        </div>
      </div>
    )
  }
}

SearchResult中继容器看起来像这样

class SearchResult extends React.Component {
  render() {
    var {viewer: {moviesByTitle: movies}} = this.props;
    return (
      <div className="searchResult">
        {movies.edges.map((edge,i) =>
          <div key={i} className="rowItem scrollRowItem">
            <Movie movie={edge.node} />
          </div>)}
      </div>
    )
  }
}

export default Relay.createContainer(SearchResult, {
  initialVariables: {
    query: '???'
  },
  fragments: {
    viewer: () => Relay.QL`
      fragment on User {
        moviesByTitle(title: $query, first: 10) {
          edges {
            node {
              ${Movie.getFragment('movie')}
            }
          }
        }
      }
    `
  }
})

错误:

Warning: RelayContainer: Expected prop `viewer` to be supplied to `SearchResult`, but got `undefined`. Pass an explicit `null` if this is intentional.

我在搜索组件中尝试做的事情(粗体更改)

const ViewerQueries = {
  viewer: () => Relay.QL`query { viewer }`
}

...

renderSearchResult() {
  if (this.state.query === '')
    return <EmptySearchResult />
  else
    return <SearchResult query={this.state.query} queries={ViewerQueries} />
}

但当然这不起作用,因为查询需要以某种方式附加到Route

问题

  1. 我的Search组件只是一个不需要自己数据的表示组件。相反,它只是将query道具提供给SearchResult中继容器。我应该如何构建组件和中继容器以将它们正确地附加到路由?

  2. 如何使用SearchResult query道具在中继查询片段中设置变量?

  3. 我不理解&#34;观看者&#34;一般的抽象 - 我见过的大多数例子都是非常人为的,并没有表明他们如何在更现实的环境中工作;例如,对不同资源具有不同访问权限的用户,或程序中用于查看不同数据片段的不同部分

1 个答案:

答案 0 :(得分:2)

使用Relay的现代API,从refetchContainer组件撰写SearchResult,因为需要使用“选项来执行具有不同变量的新查询,并在以下情况下呈现该查询的响应请求回来“

import React, { Component } from 'react';
import debounce from 'debounce';
import { graphql, createRefetchContainer } from 'react-relay';

class SearchResult extends React.Component {
  componentWillReceiveProps(nextProps) {
    if (this.props.query != nextProps.query) {
      debounce(() => this._searchMovies(), 600);
    }
  }

  _searchMovies() {
    const refetchVariables = fragmentVariables => ({
      query: this.props.query
  });

    this.props.relay.refetch(refetchVariables, null);
  }

  render() {
    var { viewer: { moviesByTitle: movies } } = this.props;
    return (
      <div className="searchResult">
        {movies.edges.map((edge, i) =>
          <div key={i} className="rowItem scrollRowItem">
            <Movie movie={edge.node} />
          </div>)}
      </div>
    )
  }
}


// Similar query syntax as Relay Classic
// Only syntactic difference is with absence of Movie.getFragment
module.exports = createRefetchContainer(SearchResult, {
    viewer: graphql`
      fragment SearchResult_viewer on SearchResult {
        @argumentDefinitions(
          query: {type: "String", defaultValue: ""}
        ) {
        moviesByTitle(title: $query, first: 10) {
          edges {
            node {
              ...Movie_movie
            }
          }
        }
      }`
    },
    graphql`
      query MoviesRefetchQuery($query: String) {
        viewer {
          ...SearchResult_viewer @arguments(query: $query)
        }
      }
   `);