首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么中继现代QueryRenderer呈现道具没有定义?

为什么中继现代QueryRenderer呈现道具没有定义?
EN

Stack Overflow用户
提问于 2017-10-15 14:28:33
回答 1查看 1.1K关注 0票数 2

这是我第一次尝试使用继电器现代。从PostgraphQL GraphQL Server获取特定用户的信息。它成功地获取了数据,但没有传递给呈现函数:

代码语言:javascript
复制
import {createFragmentContainer, QueryRenderer, graphql} from 'react-relay'
import environment from 'environment'

@CSSModules(styles) export default class Profile extends Component {   
  render() {
    var {props: {children}} = this
    return (
      <QueryRenderer
        environment={environment}
        query={graphql`
          query ProfileQuery {
            userById(id: "f0301eaf-55ad-46db-ac90-b52d6138489e") {
              firstName
              userName
            }
          }
        `}
        render={({error, relayProps}) => {
          if (error) {
            return <div>{error.message}</div>
          } else if (relayProps) {
            ...
          }
          return <div>Loading...</div>
        }}
      />
    )   
  }
} 

只有“装.”被渲染。

我猜是因为它成功地获取了graphql服务器和环境都正常的数据。

我没有使用React 16,而且这个项目也使用Redux。

请给我任何建议,为什么relayProps没有价值(例如relayProps.user)?

还有一件可能有用的事情,环境(文件)在主应用程序中,QueryRenderer和组件在导入的npm包中(将在多个应用程序之间共享)。如前所述,查询似乎运行良好,因此我不认为这是一个问题。我还在包上运行中继编译器,但不运行主应用程序,因为那里没有中继组件。

为了防止需要,使用以下方法设置环境:

代码语言:javascript
复制
const {
  Environment,
  Network,
  RecordSource,
  Store,
} = require('relay-runtime')

// Instantiate Store for Cached Data
const store = new Store(new RecordSource())

// Create Network for GraphQL Server
const network = Network.create((operation, variables) => {
  // GraphQL Endpoint
  return fetch(config.gqlapiProtocol + "://" + config.gqlapiHost + config.gqlapiUri + "/a3/graphql" , {
    method: 'POST',
    headers: {
      'Content-Type': "application/json",
      'Accept': 'application/json',
    },
    body: JSON.stringify({
      query: operation.text,
      variables,
    }),
  }).then(response => {
    return response.json()
  })
})

// Instantiate Environment
const environment = new Environment({
  network,
  store,
})

// Export environment
export default environment
EN

回答 1

Stack Overflow用户

发布于 2017-10-25 05:10:17

props不是relayprops

代码语言:javascript
复制
    render={({ error, props }) => {
      if (error) {
        return <div>{error.message}</div>;
      } else if (props) {
        ...
      }
      return <div>Loading...</div>;
    }}

代码语言:javascript
复制
fetch(GRAPHQL_URL, {
  method: 'POST',
  get headers() {
    return {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
    };
  },
  body: JSON.stringify({
    query: operation.text, // GraphQL text from input
    variables
  })
})
  .then(response => response.json())
  .then((json) => {
    // https://github.com/facebook/relay/issues/1816
    if (operation.query.operation === 'mutation' && json.errors) {
      return Promise.reject(json);
    }

    return Promise.resolve(json);
  })
);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46756129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档