简体   繁体   English

有Babel问题的React / Relay Modern / GraphQL简单项目设置

[英]React/Relay Modern/GraphQL Simple Project Setup with Babel Issues

I followed the Relay tutorial for getting started after doing the create-react-app to get a new react up and running. 在完成create-react-app之后,我按照Relay教程进行了入门,从而开始了新的反应并开始运行。 I ran it both in TypeScript mode (from here: https://github.com/Microsoft/TypeScript-React-Starter ) and also in the normal JavaScript mode and came to the same result initially. 我既在TypeScript模式下运行它(从这里: https : //github.com/Microsoft/TypeScript-React-Starter ),也在正常的JavaScript模式下运行,最初得到了相同的结果。

This is the error I'm getting when I try and run the app: 这是我尝试运行该应用程序时遇到的错误:

Either the Babel transform was not set up, or it failed to identify this call site. 没有建立Babel转换,或者无法识别此呼叫站点。 Make sure it is being used verbatim as graphql 确保将其逐字用作graphql

My suspicion is that Babel was just not running at all, but I'm not sure if that's completely true. 我的怀疑是Babel根本没有运行,但是我不确定那是不是真的。 I followed this: https://hackernoon.com/using-create-react-app-with-relay-modern-989c078fa892 to see if that would help get my babel executing within my new create-react-app + relay app with no luck. 我遵循了这一点: https : //hackernoon.com/using-create-react-app-with-relay-modern-989c078fa892看看这是否有助于让babel在新的create-react-app + Relay应用程序中执行,而没有运气。 I even ejected the app from create-react-app and modified the webpack to get it working. 我什至从create-react-app弹出了应用程序,并修改了webpack使其正常工作。 Below are what I believe are the relevant files. 以下是我认为是相关的文件。 I've done a ton of searching on this topic with no such luck and can't find an example that's using React + Relay Modern + Graphql. 我已经在这个主题上进行了大量搜索,但是没有运气,也找不到使用React + Relay Modern + Graphql的示例。

package.json 的package.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 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 (or App.jsx) 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;

Please let me know if any more files or information would be helpful. 请让我知道是否还有其他文件或信息会有所帮助。 I'd really appreciate any direction I can get. 我真的很感激我能得到的任何指示。 Thanks! 谢谢!

I ran into this exact issue on the same tutorial. 我在同一教程中遇到了这个确切的问题。 Basically, this is a Babel configuration issue. 基本上,这是Babel配置问题。 There are ways to jump through hoops getting this to work with create react app but the easiest way it to just eject the app and do the following steps: 有多种方法可以克服这些障碍,以使其与create react app一起使用,但是最简单的方法是退出app并执行以下步骤:

Run react-scripts eject (make sure react-scripts is installed globally) 运行react-scripts eject (确保已全局安装react-scripts

Adjust your Webpack config to include 'babel-loader' : 调整您的Webpack配置,使其包含'babel-loader'

{
    test: /\.jsx$/,
    exclude: /node_modules/,
    loader: 'babel-loader'
},

Add .babelrc to you project's root directory: .babelrc添加到项目的根目录:

{
    "presets": [
        "env"
    ],
    "plugins": [
        "relay"
    ]
}

Install babel-core , babel-preset-env , and babel-loader as dev dependencies in your project. babel-corebabel-preset-envbabel-loader为项目中的开发依赖项。

Once Babel is running properly you should no longer get the error you are seeing. Babel正常运行后,您将不再收到所看到的错误。

In case of create-react-app , you can avoid having to eject by installing babel-plugin-relay/macro and doing this: 如果是create-react-app ,则可以通过安装babel-plugin-relay/macro并执行以下操作来避免退出:

import graphql from "babel-plugin-relay/macro";

Instead of importing the graphql tag from react-relay . 而不是从react-relay导入graphql标签。

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

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