4

我从我的服务器正确地返回了数据,但是我得到了一个未提供的道具错误。

~ expected prop `query` to be supplied to `Relay(ContactsPage)`, but got `undefined`. 

与以下。

import makeRouteConfig from 'found/lib/makeRouteConfig';
import Route from 'found/lib/Route';
import React from 'react';
import { graphql } from 'react-relay';

import ContactsPage from '../components/ContactsPage';

export default makeRouteConfig(
    <Route
      path="/contacts"
      Component={ContactsPage}
      prepareVariables={ (params) => ({
        ...params,
        count: 5,
        order: "title",
        postType: ['mp_contact'],
      })}
      query={graphql`
        query contacts_WPQuery_Query(
          $count: Int!
          $order: String!
          $cursor: String
          $categoryName: String
          $postType: [String]
        ) {
            ...ContactsPage_query
        }
      `}
    />
);

我可以看到数据是从服务器获取的。

服务器响应

而且我还有其他组件遵循类似的工作模式:/ 这是 ContactsPage 组件

import React, { Component } from 'react'
import ContactsList from './ContactsList'
import { createFragmentContainer, graphql } from 'react-relay'

class ContactsPage extends Component {

  render() {
    const {query} = this.props
    return (
      <div>
        <ContactsList wp_query={query.wp_query} />
      </div>
    )
  }
}

export default createFragmentContainer(
  ContactsPage,
  {
    query: graphql`
      fragment ContactsPage_query on Query {
          wp_query {
            id
            ...ContactsList_wp_query
          }
      }
    `
  }
)
4

2 回答 2

1

我可以通过像这样更改在查询 {} 下嵌套根查询来解决此问题

query={graphql`
        query contacts_WPQuery_Query(
          $count: Int!
          $order: String!
          $cursor: String
          $categoryName: String
          $postType: [String]
        ) {
            query {
               ...ContactsPage_query
            }
        }
      `}

我需要更新我的graphql服务器以将查询节点嵌套一层深(我认为这在Relay Modern中不是必需的。但似乎确实如此。也许这是Found路由库的约束。我不确定。

于 2017-09-19T02:54:02.603 回答
0

我认为您将 Relay Modern 的两个不同方面混为一谈。

我已经用我们用作根类型的名称更新了您的代码,以便您可以更清楚地看到差异。当然,您可以随意称呼它。

query您在课堂上定义的是Route“QueryRenderer” - https://facebook.github.io/relay/docs/query-renderer.html

graphql`
  query contactsQuery (
    $count: Int!
    $order: String!
    $cursor: String
    $categoryName: String
    $postType: [String]
  ) {
    viewer {
      ...ContactsPage_viewer
    }
  }
`}

如果您有多个路由,则可能有多个这些路由,不建议您嵌套这些。

但是,您的容器中的片段是您定义对 graphql 的数据依赖性的地方 - https://facebook.github.io/relay/docs/fragment-container.html

export default createFragmentContainer(
  ContactsPage,
  {
    viewer: graphql`
      fragment ContactsPage_viewer on Viewer {
          wp_query {
            id
            ...ContactsList_wp_viewer
          }
      }
    `
  }
)

请注意,如果您正在创建“重新获取”容器,则可以将查询添加到容器声明中。例如,如果您有一个根据您传入的参数在您的 graphQL 解析中过滤的列表 - https://facebook.github.io/relay/docs/refetch-container.html

希望这有助于消除任何困惑。

于 2017-10-18T11:18:40.313 回答