[英]Relay Modern isn't sending network request to GraphQL Server
I just started looking at Relay Modern recently and creating a simple app with a GraphQL backend (which works perfectly fine when testing with GraphIQL). 我最近才刚刚开始研究Relay Modern,并使用GraphQL后端创建了一个简单的应用程序(在使用GraphIQL进行测试时效果很好)。 However, I'm running into problems with Relay not sending network requests to retrieve any data.
但是,我遇到了中继无法发送网络请求以检索任何数据的问题。 I'm not 100% confident about the below code but I definitely would expect it to at least send a network request to
http://localhost:3000/graphql
, but the devtools don't show any such request (or server logs). 我对以下代码不是100%的自信,但我绝对希望它至少将网络请求发送到
http://localhost:3000/graphql
,但devtools不会显示任何此类请求(或服务器日志) 。
environment.js environment.js
import { Environment, Network, RecordSource, Store } from 'relay-runtime';
const store = new Store(new RecordSource());
const network = Network.create((operation, variables) =>
fetch('http://localhost:3000/graphql', {
method: 'POST',
headers: {
// Add authentication and other headers here
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: operation.text, // GraphQL text from input
variables,
}).then(res => res.json()),
})
);
const environment = new Environment({
network,
store,
});
export default environment;
App.jsx App.jsx
import React, { Component } from 'react';
import { graphql, QueryRenderer } from 'react-relay';
import environment from '@utilities/environment';
class App extends Component {
render() {
console.log(this.props); // Empty object {} here
return (
<div>
Hello World!
</div>
);
}
}
const Query = graphql`
query AppQuery {
user(id: "u01") {
id
username
}
}
`;
const AppQuery = () =>
(<QueryRenderer
environment={environment}
graphql={Query}
variables={{}}
render={({ error, props }) => {
console.log(error, props); // Get (undefined, {}) here
if (error) {
return <div>{error.message}</div>;
} else if (props) {
return <App {...props} />;
}
return <div>Loading!</div>;
}}
/>);
export default AppQuery;
Am I missing something obvious? 我是否缺少明显的东西? There are no console/webpack errors and the app renders properly, such as it is, but simply no GraphQL/Relay data.
没有控制台/ Webpack错误,并且应用程序可以正确渲染,例如,但是没有GraphQL / Relay数据。 Thanks!
谢谢!
I think your environnement is just fine. 我认为您的环境很好。
Few things that might help : You might want to create a FragmentContainer and setup/run Relay Compiler to generate the needed graphql files in order to Relay run your queries. 可能没有帮助的事情:您可能想创建一个FragmentContainer并设置/运行Relay Compiler以生成所需的graphql文件,以便Relay运行查询。
You probably want declare and collocate the data requirements with App through a FragmentContainer. 您可能想要通过FragmentContainer声明和搭配App的数据需求。 You need a Fragment Container because your data is masked in App hence not available through props ( see more here why it's masked).
您需要一个“片段容器”,因为您的数据在App中被屏蔽,因此无法通过props获得( 请参见此处为何屏蔽数据)。
You'll need to use createFragmentContainer() like this : 您需要像这样使用createFragmentContainer() :
App = createFragmentContainer(
App,
graphql`
fragment App_users on User { // The fragment name should follow the convention <FileName>_<propName>, so maybe you might the App component to an another file.
user(id: "u01") {
id
username
}
}
`,
);
Modify the Query to : 将查询修改为:
graphql`
viewer {
...App_users_viewer
}
`
When done, you should be able to run the Relay Compiler and have graphql generated files 完成后,您应该能够运行Relay Compiler并具有graphql生成的文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.