Docs 菜单
Docs 主页
/ /
Atlas App Services
/ /

将 GraphQL 迁移到 WunderGraph

在此页面上

  • 迁移到 WunderGraph
  • 创建 WunderGraph 应用程序
  • 添加 npm 脚本
  • 启动 WunderGraph MongoDB Server
  • 配置 MongoDB Atlas 数据源
  • 添加查询操作
  • 使用 GraphiQL 用户界面浏览数据
  • 使用 WunderGraph 的 JSON-RPC 接口进行生产
  • 更新客户端应用程序
  • 关闭 Atlas App Services 端点
  • 后续步骤
  • WunderGraph Cosmo for GraphQL Federation

重要

请务必参阅MongoDB Atlas和 WunderGraph 的官方文档 以获取最新、最准确的信息。具体步骤可能会有所不同,具体取决于项目的详细信息和所使用的技术。

WunderGraph 是一个综合性的开发者平台,配备了种类繁多的开源工具和 SDK。

这些工具之一是 前端后端 (BFF) 框架 。此 BFF 框架是一个强大且符合规范的服务器框架,您可以将其添加到生产环境中。 该框架可以集成任何来源的数据。 这包括来自 MongoDB Atlas 的数据。 有关更多详细信息,请参阅 的 WunderGraphDocs 页面Atlas 。如果您计划使用 GraphQL Federation,请查看 WunderGraph Cosmo。

WunderGraph BFF 是一个开源网关,可捆绑您的数据并为前端框架生成类型安全的客户端。 您还可以使用钩子添加其他业务逻辑或 自定义 GraphQL 解析程序 将客户端与数据库解耦。

以下概述了如何将 GraphQL 主机从 Atlas App Services 迁移到 WunderGraph。 具体步骤可能会有所不同,具体取决于项目的详细信息和所使用的技术。

1
# Init a new project
npx create-wundergraph-app my-project --example simple
# Move to the project directory
cd my-project
# Install dependencies
npm i
2

将以下脚本添加到 package.json中,以便运行 WunderGraph 服务器。

{
"scripts": {
"start": "wunderctl up --debug",
"build": "wunderctl generate"
}
}
3
npm run start

启动服务器后,WunderGraph 会生成一些代码。 检查位于http://localhost:9991的服务器状态。

4

您需要配置 MongoDB Atlas 数据源。 在wundergraph.config.ts文件中添加以下内容:

wundergraph.config.ts
const Atlas = introspect.mongodb({
apiNamespace: 'my_db',
databaseURL: 'YourAtlasURL',
});
configureWunderGraphApplication({
apis: [Atlas],
});
5

要使用 Atlas 数据,您需要添加查询操作。 导航到operations文件夹,创建一个名为Users.graphql的新 GraphQL 文件,然后添加以下内容:

/operations/Users.graphql
{
Mongo_findFirstusers {
id
name
email
}
}
6

在 WunderGraph 服务器运行并添加查询操作后,您可以使用 GraphQL 用户界面通过 GraphQL API 探索 Atlas 数据。 当应用程序处于开发阶段时,这非常有用。 对于生产环境中的应用程序,我们建议使用 WunderGraph 的 JSON-RPC 接口(请参阅下一步)。

  1. 在 WunderGraph 服务器运行时,导航到http://localhost:9991/graphql

  2. 将这些查询添加到 GraphiQL 用户界面:

    query Users {
    db_findManyusers {
    id
    name
    email
    }
    }
  3. 单击“播放”按钮。

7

GraphiQL 有利于开发,但在生产中,您应该考虑使用 WunderGraph 的 JSON-RPC 接口与 Atlas 数据进行交互。

简而言之,WunderGraph 将您的 GraphQL 操作编译为您可以调用的 JSON-RPC 端点。

运行wunderctl up后,WunderGraph 会检查.wundergraph/operations目录中是否有*.graphql文件并进行处理。 为此,每个文件应包含一个 GraphQL 操作。

每个文件都将被编译到一个 JSON-RPC 端点中。 端点的名称由文件名确定。

以下是 JSON-RPC API 查询示例:

curl http://localhost:9991/operations/Users
8

更新与 GraphQL API 端点交互的任何客户端应用程序,使其指向新的 WunderGraph 端点 URL。

9

验证GraphQL API端点已完全迁移并可在 WunderGraph 上运行后,您可以删除MongoDB Atlas App Services应用,以避免不必要的费用。 谨此提醒,从 9 月302025 开始,将不再支持Atlas GraphQL端点。

查看 WunderGraph CosmoDocs 了解如何构建分布式GraphQL 架构,该架构结合多个GraphQL API 来创建统一的图形。

Cosmo 使团队和组织能够轻松管理和扩展(联合)GraphQL 架构。 通过组合检查快速迭代,而不会破坏任何内容。

WunderGraph Cosmo 可以作为托管服务在本地、本地或云中轻松运行。 Cosmo 是一款含电池的解决方案,涵盖了整个平台从路由到分析的所有功能。

Cosmo 支持整体式 GraphQL API 以及 Federation v 1和 v 2包括订阅)。

后退

迁移至 Apollo