简体   繁体   English

为什么Relay Modern QueryRenderer渲染道具未定义?

[英]Why are Relay Modern QueryRenderer render props undefined?

This is my first attempt at using Relay Modern. 这是我第一次使用Relay Modern。 Fetching for a specific User from a PostgraphQL GraphQL Server . PostgraphQL GraphQL Server获取特定用户。 It is fetching the data successfully but not passing to render function: 它正在成功获取数据,但未传递给render函数:

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>
        }}
      />
    )   
  }
} 

Only "Loading..." is rendered. 仅呈现“正在加载...”。

I am guessing because it successfully fetches data that the graphql server and environment are ok. 我猜是因为它成功获取了graphql服务器和环境都可以的数据。

I am not using React 16 and the project also uses Redux. 我没有使用React 16,该项目也使用Redux。

Any suggestions please as to why relayProps wouldn't have a value (eg relayProps.user)? 请问关于为什么RelayProps没有值的任何建议(例如relayProps.user)?

One further thing that may help, the environment (file) is in the main application and the QueryRenderer and components are in an imported npm package (to be shared across a number of applications). 可能会有帮助的另一件事是,环境(文件)在主应用程序中,而QueryRenderer和组件在导入的npm包中(在许多应用程序之间共享)。 As mentioned, the query seems to work fine so I did not think this was a problem. 如前所述,该查询似乎工作正常,因此我认为这不是问题。 I also run the relay compiler on the package but not the main application since there are no relay components there. 我还在程序包上运行了中继编译器,但没有在主应用程序上运行,因为那里没有中继组件。

Just in case it's needed the environment is setup using: 万一需要,可以使用以下环境进行设置:

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

props are not relayprops props不是relayprops props

    render={({ error, props }) => {
      if (error) {
        return <div>{error.message}</div>;
      } else if (props) {
        ...
      }
      return <div>Loading...</div>;
    }}

and

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);
  })
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM