在执行 create-react-app 以启动和运行新的 react 之后,我按照 Relay 教程开始。我在 TypeScript 模式(从这里:https ://github.com/Microsoft/TypeScript-React-Starter )和普通的 JavaScript 模式下运行它,最初得到了相同的结果。
这是我尝试运行应用程序时遇到的错误:
要么未设置 Babel 转换,要么未能识别此调用站点。确保它被逐字用作
graphql
我怀疑 Babel 根本没有运行,但我不确定这是否完全正确。我遵循了这个:https ://hackernoon.com/using-create-react-app-with-relay-modern-989c078fa892看看这是否有助于让我的 babel 在我的新 create-react-app + 中继应用程序中执行没有运气。我什至从 create-react-app 中退出了该应用程序并修改了 webpack 以使其正常工作。以下是我认为的相关文件。我在这个主题上进行了大量搜索,但没有这样的运气,也找不到使用 React + Relay Modern + Graphql 的示例。
包.json
{
"name": "testProj",
"version": "0.1.0",
"private": true,
"metadata": {
"graphql": {
"schema": "./graphql/testProj.json"
}
},
"dependencies": {
"@babel/polyfill": "^7.0.0-beta.42",
"@babel/runtime": "^7.0.0-beta.42",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-relay": "^1.5.0",
"react-scripts-ts": "2.14.0",
"relay-runtime": "^1.5.0"
},
"scripts": {
"start": "node ./setup && react-scripts-ts start",
"build": "node ./setup && react-scripts-ts build",
"test": "node ./setup && react-scripts-ts test --env=jsdom",
"relay": "relay-compiler --src ./src --schema graphql/implementato.graphql --extensions ts tsx"
},
"devDependencies": {
"@babel/register": "^7.0.0-beta.42",
"@types/jest": "^22.2.0",
"@types/node": "^9.4.7",
"@types/react": "^16.0.40",
"@types/react-dom": "^16.0.4",
"@types/react-relay": "^1.3.4",
"babel-plugin-relay": "^1.5.0",
"babel-plugin-styled-components": "^1.5.1",
"babel-relay-plugin-loader": "^0.11.0",
"graphql": "^0.13.2",
"relay-compiler": "^1.5.0",
"typescript": "^2.7.2"
}
}
setup.js
const fs = require('fs');
const path = require('path');
const file = path.resolve('./node_modules/babel-preset-react-app/index.js');
let text = fs.readFileSync(file, 'utf8');
if (!text.includes('babel-plugin-relay')) {
if (text.includes('const plugins = [')) {
text = text.replace(
'const plugins = [',
"const plugins = [\n require.resolve('babel-plugin-relay'),",
);
fs.writeFileSync(file, text, 'utf8');
} else {
throw new Error(`Failed to inject babel-plugin-relay.`);
}
}
App.tsx(或 App.jsx)
import * as React from 'react';
import { QueryRenderer, graphql } from 'react-relay';
import environment from './environment';
const query = graphql`
query AppQuery{
allAccounts {
totalCount
}
}`;
class App extends React.Component {
render() {
return (
<QueryRenderer
environment={environment}
query={query}
variables={{}}
render={({ error, props }) => {
if (error) {
return <div>Error!</div>;
}
if (!props) {
return <div>Loading...</div>;
}
return <div>Loaded!</div>;
}}
/>
);
}
}
export default App;
请让我知道是否有更多文件或信息会有所帮助。我真的很感激我能得到的任何方向。谢谢!