7. 使用 rover dev 进行本地开发
5m

概述

兴奋地开始将配乐数据直接连接到食谱了吗?(可以说,为膳食寻找音乐!)首先,让我们确保我们为本地开发做好了准备。

在本课中,我们将

  • 回顾 开发工具包 提供
  • 使用rover dev 使用我们的

从开发到生产

尽管我们的 soundtracks 都在本地运行(recipes 在远程位置运行),我们正在模拟在将所有内容托管在实时、流量就绪环境中时会发生的相同交换。

与开发任何应用程序一样,我们需要一个工作流程,该流程允许我们对本地进行更改,对其进行测试,并确信我们已经解决了任何问题 之前 将我们的更改推送到生产环境(或者至少是 我们的 生产环境的版本,在教程中!)。

但除了直接将新更改直接推送到我们的生产环境中 ,我们如何实际测试我们的 API 是否会继续运行?

GraphOS 超级图开发工具包

提供一个 超级图开发工具包 帮助开发人员快速轻松地在 架构中构建和玩转

而且,正如您可能已经猜到的那样,我们一直在使用 的一部分 开发工具包!

沙箱就是这样一种工具,它是 的一种特殊模式,它为我们提供了资源管理器(我们一直在使用的 IDE,用于构建和运行针对我们本地运行的 的查询),模式引用, 和差异。

虽然我们没有自己创建,但 recipes 是使用 rover template 引导的,为使用 JavaScript 的 提供了一个起点。

Rover template options shown as Java plus GraphQL, .NET plus GraphQL, and Go plus GraphQL

注意:对使用不同语言引导新 的其他模板感兴趣?请查看 rover template 在 Apollo 文档中。有一个适用于 Hot Chocolate 的!

我们 开发工具包的最后一块是 rover dev

rover dev 用于本地开发

到目前为止,我们已经看到来自两个 的数据通过运行本地 聚合在一起。现在让我们设想我们的路由器和两个子图都在我们的云基础设施中的某个实时位置运行。

现在是进行一些更改的时候了。我们想对 soundtracks 的本地副本进行一些调整,并在我们编码时进行实时验证,以确保我们的 仍然可以组合这些更改。

中的一个特殊命令可以完全解决这种情况。让我们看看 rover dev 如何融入我们的 开发工作流程。

使用 rover dev

rover dev 允许我们启动一个 本地 —就在您的计算机上! —它可以跨一个或多个 进行 。每个子图可以在本地或远程运行。它是在我们的本地开发环境中的 !这使我们能够在将更改发布到 之前实现和测试更改。

The rover dev command, showing a router receiving a request and splitting it between two subgraphs

现在您可能会想:我们不是已经本地运行了一个 吗?在 第 5 课:路由器配置 中,我们下载了一个 并将其本地运行。关键区别在于 连接到 上的 注册表,而我们使用 rover dev 运行的路由器将连接到我们的本地 更改,即使这些更改尚未发布到注册表!这意味着我们可以试用实现新功能并探索我们想对子图进行的任何其他模式更改,并模拟它如何与 和其他子图配合使用。

要开始,我们需要 soundtracks 运行。如果您还没有,请使用 dotnet run 在本地启动服务器。

任务!

配置文件

使用 rover dev 会话启动多个 的最简单方法是使用一个 YAML 文件,其中列出了每个子图的详细信息。

  1. Router 目录中创建一个名为 supergraph-config.yaml 的新文件,并粘贴下面的内容:

    supergraph-config.yaml
    federation_version: =2.5.0
    subgraphs:
    soundtracks: # Subgraph running locally, using introspection to retrieve schema
    schema:
    subgraph_url: http://127.0.0.1:5059/graphql
    recipes: # Schema downloaded from GraphOS registry
    schema:
    graphref: <APOLLO_GRAPH_REF> # replace with your own graph_ref
    subgraph: recipes

    第一行定义了我们使用的联合版本。

    The properties under subgraphs are all the names of in the . Let's look at each one.

    soundtracks is running locally, and we're using to retrieve the schema. We could have given it the schema file, but because we're using Hot Chocolate's annotation-based approach, it's easier to take advantage of instead of remembering to generate the schema file after every change we make.

    Next up is recipes. This one is pointing to the recipes stored in . If we were making changes to the recipes as well, we'd probably point to a locally-running server and schema, similar to how we've set up soundtracks.

  2. Make sure to replace the <APOLLO_GRAPH_REF> value with your own!

    supergraph-config.yaml
    recipes: # Schema downloaded from GraphOS registry
    schema:
    graphref: <APOLLO_GRAPH_REF> # replace with your own graph_ref
    subgraph: recipes

We're ready to run rover dev!

Running rover dev

In a new terminal window, navigate to the Router folder (where the supergraph-config.yaml file lives).

Let's run rover dev and pass in the config file with the --supergraph-config flag.

rover dev --supergraph-config supergraph-config.yaml

After running it, we'll get the following output

⚠️ Do not run this command in production! ⚠️ It is intended for local development.
👂 polling https://127.0.0.1:5059/graphql every 1 second
🛫 starting a session with the 'recipes' subgraph
🛫 starting a session with the 'soundtracks' subgraph
🎶 composing supergraph with Federation v2.5.4
🚀 your supergraph is running! head to https://127.0.0.1:4000 to query your supergraph
🎶 composing supergraph with Federation v2.5.4
✅ successfully composed after adding the 'soundtracks' subgraph

Awesome, looks like our is running at https://127.0.0.1:4000. Let's check it out!

Note: By default, rover dev runs the on port 4000. If you would prefer running it on a different port, you can add the additional flag --supergraph-port (or -p) and set it to a different value. Just make sure you're not using port 5059 because that's where the soundtracks is running!

In the browser, navigate to https://127.0.0.1:4000. We'll see Sandbox connected to port 4000, ready for us to our local . We've just done the equivalent of what we did in lesson 6 when we added the recipes using Studio, but locally!

Let's make the same we did in Studio, just to double check everything is working smoothly.

query GetRecipeAndPlaylists {
recipe(id: "recgUKbxnQssl9fYc") {
cookingTime
description
instructions
}
featuredPlaylists {
name
}
}

Run the query and... data! 🎉You can confirm the same data was returned in Studio as it is in Sandbox.

We're now free to make any changes we'd like to in the soundtracks , and see for ourselves how the would handle them. We can even spin up a whole new subgraph and add it to our local .

This is your playground to explore in, to test new concepts, and dream up new features– all without impacting the production !

Practice

Which of the following does rover dev not do?

Key takeaways

  • The development kit includes the following: Sandbox, rover template and rover dev.
  • rover dev lets us start a local that can across one or more .
  • To use rover dev, we need each 's name, the URL where it's running, and (optionally) the path to the schema file. The easiest way to do this is with a config file.

Up next

We have everything we need to get started on our changes! Let's turn our focus back to that dream that connects data between a recipe and the perfect playlists to accompany our cooking.

Previous

Share your questions and comments about this lesson

This course is currently in

beta
.Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.