我正在尝试进行第一次中继查询。我做了npm run relay
和npm run build
。一切正常,但在控制台中出现错误:
有人知道什么可能导致此错误吗?
更新。
Table.js(要在其中进行查询的组件)
import React, { Component } from 'react';
import { graphql, QueryRenderer } from 'react-relay';
const environment = import('../../environment.js');
class Table extends Component {
render() {
return (
<QueryRenderer
environment={environment}
query={graphql`
query TableQuery {
users {
data {
name
}
}
}
`}
render={({error, props}) => {
return <div>User: 1</div>;
}}
/>
);
}
}
export default Table;
environment.js(中继配置)
import {
Environment,
Network,
RecordSource,
Store,
} from 'relay-runtime';
function fetchQuery(
operation,
variables,
) {
return fetch('/graphql', {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
query: operation.text,
variables,
}),
}).then(response => {
return response.json();
});
}
const network = Network.create(fetchQuery);
const store = new Store(new RecordSource());
const environment = new Environment({
network,
store
});
export default environment;
所有内容都来自文档设置页面。
答案 0 :(得分:1)
在Table.js
中,您似乎混淆了导入的语法:
const environment = import('../../environment.js'); // Wrong
const environment = require('../../environment.js'); // OK
import environment from '../../environment.js'; // OK
使用import('../../environment.js')
使其动态导入,并返回Promise(取决于捆绑程序配置),并且不太可能实现您的期望。