Skip to main content

添加 Relay

Relay 是一个框架,用于构建由 GraphQL 提供支持的数据驱动的 React 应用。Relay 的当前版本与使用 Babel Macros 开箱即用的 Create React App 项目一起工作。按照 Relay 文档 中所示设置你的项目,然后确保你拥有提供宏的 babel 插件版本。

¥Relay is a framework for building data-driven React applications powered by GraphQL. The current release of Relay works with Create React App projects out of the box using Babel Macros. Set up your project as shown in the Relay documentation, then make sure you have a version of the babel plugin providing the macro.

要添加它,请运行:

¥To add it, run:

npm install --save babel-plugin-relay

或者你可以使用 yarn

¥Alternatively you may use yarn:

yarn add babel-plugin-relay

然后,在任何使用 graphql 模板标签的地方,导入宏:

¥Then, wherever you use the graphql template tag, import the macro:

import graphql from 'babel-plugin-relay/macro';
// instead of:
// import { graphql } from "babel-plugin-relay"

graphql`
query UserQuery {
viewer {
id
}
}
`;

要了解有关 Relay 的更多信息,请查看 它的文档

¥To learn more about Relay, check out its documentation.