概述
兴奋地开始将配乐数据直接连接到食谱了吗?(可以说,为膳食寻找音乐!)首先,让我们确保我们为本地开发做好了准备。
在本课中,我们将
- 回顾超级图 开发工具包 GraphOS 提供
- 使用
rover dev
使用我们的 超级图
从开发到生产
尽管我们的 路由器 和 soundtracks
子图 都在本地运行(recipes
在远程位置运行),我们正在模拟在将所有内容托管在实时、流量就绪环境中时会发生的相同交换。
与开发任何应用程序一样,我们需要一个工作流程,该流程允许我们对本地进行更改,对其进行测试,并确信我们已经解决了任何问题 之前 将我们的更改推送到生产环境(或者至少是 我们的 生产环境的版本,在教程中!)。
但除了直接将新更改直接推送到我们的生产环境中 超级图,我们如何实际测试我们的 API 是否会继续运行?
GraphOS 超级图开发工具包
Apollo GraphOS 提供一个 超级图开发工具包 帮助开发人员快速轻松地在 子图 架构中构建和玩转 子图。
而且,正如您可能已经猜到的那样,我们一直在使用 GraphOS 的一部分 超级图 开发工具包!
沙箱就是这样一种工具,它是 GraphOS Studio 的一种特殊模式,它为我们提供了资源管理器(我们一直在使用的 GraphQL IDE,用于构建和运行针对我们本地运行的 子图 的查询),模式引用, 模式检查 和差异。
虽然我们没有自己创建,但 recipes
子图 是使用 rover template
引导的,为使用 JavaScript 的 GraphQL 子图服务器 提供了一个起点。
注意:对使用不同语言引导新 子图 的其他模板感兴趣?请查看 rover template
在 Apollo 文档中。有一个适用于 Hot Chocolate 的!
我们 超级图 开发工具包的最后一块是 rover dev
。
rover dev
用于本地开发
到目前为止,我们已经看到来自两个 子图 的数据通过运行本地 路由器 聚合在一起。现在让我们设想我们的路由器和两个子图都在我们的云基础设施中的某个实时位置运行。
现在是进行一些更改的时候了。我们想对 soundtracks
的本地副本进行一些调整,并在我们编码时进行实时验证,以确保我们的 超级图 仍然可以组合这些更改。
在 Rover CLI 中的一个特殊命令可以完全解决这种情况。让我们看看 rover dev
如何融入我们的 图形 开发工作流程。
使用 rover dev
rover dev
允许我们启动一个 本地 路由器 —就在您的计算机上! —它可以跨一个或多个 子图 进行 查询。每个子图可以在本地或远程运行。它是在我们的本地开发环境中的 超级图!这使我们能够在将更改发布到 GraphOS 之前实现和测试更改。
现在您可能会想:我们不是已经本地运行了一个 路由器 吗?在 第 5 课:路由器配置 中,我们下载了一个 路由器 并将其本地运行。关键区别在于 该 路由器 连接到 GraphOS 上的 超级图模式 注册表,而我们使用 rover dev
运行的路由器将连接到我们的本地 子图 更改,即使这些更改尚未发布到注册表!这意味着我们可以试用实现新功能并探索我们想对子图进行的任何其他模式更改,并模拟它如何与 路由器 和其他子图配合使用。
要开始,我们需要 soundtracks
子图 运行。如果您还没有,请使用 dotnet run
在本地启动服务器。
配置文件
使用 rover dev
会话启动多个 子图 的最简单方法是使用一个 YAML 文件,其中列出了每个子图的详细信息。
在
Router
目录中创建一个名为supergraph-config.yaml
的新文件,并粘贴下面的内容:supergraph-config.yamlfederation_version: =2.5.0subgraphs:soundtracks: # Subgraph running locally, using introspection to retrieve schemaschema:subgraph_url: http://127.0.0.1:5059/graphqlrecipes: # Schema downloaded from GraphOS registryschema:graphref: <APOLLO_GRAPH_REF> # replace with your own graph_refsubgraph: recipes第一行定义了我们使用的联合版本。
The properties under
subgraphs
are all the names of subgraphs in the supergraph. Let's look at each one.soundtracks
is running locally, and we're using introspection 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 introspection instead of remembering to generate the schema file after every change we make.Next up is
recipes
. This one is pointing to therecipes
subgraph stored in GraphOS. If we were making changes to therecipes
subgraph as well, we'd probably point to a locally-running server and schema, similar to how we've set upsoundtracks
.Make sure to replace the
<APOLLO_GRAPH_REF>
value with your own!supergraph-config.yamlrecipes: # Schema downloaded from GraphOS registryschema:graphref: <APOLLO_GRAPH_REF> # replace with your own graph_refsubgraph: 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 supergraph is running at https://127.0.0.1:4000. Let's check it out!
Note: By default, rover dev
runs the supergraph 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
subgraph 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 query our local router. We've just done the equivalent of what we did in lesson 6 when we added the recipes
subgraph using Studio, but locally!
Let's make the same query we did in Studio, just to double check everything is working smoothly.
query GetRecipeAndPlaylists {recipe(id: "recgUKbxnQssl9fYc") {cookingTimedescriptioninstructions}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
subgraph, and see for ourselves how the router would handle them. We can even spin up a whole new subgraph and add it to our local supergraph.
This is your playground to explore in, to test new concepts, and dream up new features– all without impacting the production supergraph!
Practice
rover dev
not do?Key takeaways
- The GraphOS supergraph development kit includes the following: Sandbox,
rover template
androver dev
. rover dev
lets us start a local router that can query across one or more subgraphs.- To use
rover dev
, we need each subgraph'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 supergraph config file.
Up next
We have everything we need to get started on our changes! Let's turn our focus back to that dream query that connects data between a recipe and the perfect playlists to accompany our cooking.
Share your questions and comments about this lesson
This course is currently in
You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.