这是我第一次尝试使用继电器现代。从PostgraphQL GraphQL Server获取特定用户的信息。它成功地获取了数据,但没有传递给呈现函数:
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包中(将在多个应用程序之间共享)。如前所述,查询似乎运行良好,因此我不认为这是一个问题。我还在包上运行中继编译器,但不运行主应用程序,因为那里没有中继组件。
为了防止需要,使用以下方法设置环境:
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发布于 2017-10-25 05:10:17
props不是relayprops
render={({ error, props }) => {
if (error) {
return <div>{error.message}</div>;
} else if (props) {
...
}
return <div>Loading...</div>;
}}和
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);
})
);https://stackoverflow.com/questions/46756129
复制相似问题