如何在 "GraphQL for .NET" 和 "Relay" 中 link 架构?

How to link schema in "GraphQL for .NET" and "Relay"?

我想构建一个前端为 Javascript、后端为 C# 的 Web 应用程序,我想确定 GraphQL 的价值。

现在,对于我的后端,我实现了一个类似于 one of the examples 的示例架构,如下所示:

public class StarWarsSchema : Schema
{
    public StarWarsSchema()
    {
        Query = new StarWarsQuery();
    }
}

在我的前端,我现在需要以某种方式将此架构告诉 Relay。至少这是我在浏览教程时所理解的,因为出于某种原因需要转换 GraphQL 查询。 这是我想加载所有机器人的示例:

class Content extends React.Component<ContentProps, { }> {
    ...
}

export default Relay.createContainer(Content, {
    fragments: {
        viewer: () => Relay.QL`
            fragment on User {
                query HeroNameQuery {
                    droids {
                        id
                        name
                    }
                }
            }
        `,
    }
});

one of the examples for Relay, I have seen that the babel-relay-plugin is used for conversion. It gets a schema file (JSON). The Getting Started Relay 指南中展示了如何使用 graphql-js 和 graphql-relay-js 创建这样的模式。

现在我的问题:

  1. 我真的需要在前端和后端创建架构吗?
  2. 教 Relay 我的架构有什么意义,因为后端已经使用该架构来 return 格式正确的数据?
  3. 在这种情况下使用中继有什么好处?如果我只是通过常规 REST 端点以及 GraphQL 查询作为参数访问后端,我会失去什么?
  1. 您只需要在后端使用架构,您可以使用本文档底部的示例从您的后端下载 schema.json:https://facebook.github.io/relay/docs/guides-babel-plugin.html
  2. 中继需要架构才能正确构造查询并理解返回数据的类型。
  3. Relay 包装您的 React 组件和 fetch/provide 渲染所需的所有数据。有许多开箱即用的功能,如数据缓存、查询合并,因此使用 Relay 你不需要获取任何东西,也不需要担心数据如何提供给你的组件,你只需要编写查询和组件。