我在StepZen的免费版本上尝试了GraphQL

首先

GraphQL是一种为API设计的查询语言,与REST API相比,它允许客户端详细控制获取的数据,实现高效的数据获取,因此备受关注。
2023年,IBM收购的StepZen提供了能够快速开发GraphQL API的GraphQL服务器环境。

本文介绍了如何使用StepZen的免费版本部署样例GraphQL并进行API调用的步骤。

无偿版有以下主要限制:

每月最多300,000次调用
最多可以部署两个端点
只能部署到StepZen Cloud上

预先准备

在StepZen的官方网站上,通过注册电子邮件地址来创建账号。

使用GraphQL进行创建

只需按照屏幕上的指示进行操作,即可简单地开始使用。

第一步:选择后端

选择连接到后端作为数据源。

1_select-backend.png

在这里,我们选择All Connectors中的”GraphQL”。选择All Connectors中的任意一项将跳过”Step2 Configuration”。

步骤3. 设置StepZen

安装StepZen CLI并进行帐户认证。

3_setup-stepzen.png

使用npm安装StepZen CLI。

如果未安装npm,则需要先安装npm(Node.js)。
https://nodejs.org/en/download
~ % npm install -g stepzen

added 284 packages, and audited 285 packages in 15s

38 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
npm notice
npm notice New major version of npm available! 8.19.3 -> 9.6.5
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.6.5
npm notice Run npm install -g npm@9.6.5 to update!
npm notice

确认已安装的 Stepzen 版本。

~ % stepzen version
stepzen/0.29.0 darwin-arm64 node-v18.13.0
You are using the latest version.

使用StepZen帐户登录。
登录时需要Admin Key,因此从GUI中进行复制粘贴。

~ % stepzen login -a travilah
What is your admin key?: *******************************************************************************************
You have successfully logged in with the travilah account.

第四步:建立您的终端

配置GraphQL终端节点。

4_build-your-endpoint.png

创建任意目录并将其初始化为StepZen工作区。

~ % mkdir stepzen
~ % cd stepzen
stepzen % stepzen init --endpoint=api/getting-started
Created a StepZen workspace in /Users/jiszk/stepzen
不能将home目录用作工作区。
~%stepzen导入graphql
›错误:无法在当前目录中创建StepZen工作区。
›不支持使用home目录作为StepZen工作区。请选择一个不同的目录。

导入GraphQL的模式示例(getting-started)。

stepzen % stepzen import graphql
? What is the GraphQL endpoint URL? https://graphqldd.stepzen.net/api/dd1cf47f51ac830fe21dc00ec80cee65/__graphql
? Prefix to add to all generated type names (leave blank for none)
? Add an HTTP header, e.g. Header-Name: header value (leave blank for none)
Starting... done
Successfully imported graphql data source into your GraphQL schema

部署导入的GraphQL模式。

stepzen % stepzen start
Deploying api/getting-started to StepZen... done in 2.5s ?
  ✓ ? https://travilah.stepzen.net/api/getting-started/__graphql
  ✓ ? wss://travilah.stepzen.net/stepzen-subscriptions/api/getting-started/__graphql (subscriptions)

You can test your hosted API with curl:

curl https://travilah.stepzen.net/api/getting-started/__graphql \
   --header "Authorization: Apikey $(stepzen whoami --apikey)" \
   --header "Content-Type: application/json" \
   --data-raw '{
     "query": "query SampleQuery { __schema { description queryType { fields {name} } } }"
   }'

Or explore it with GraphiQL at
   https://dashboard.stepzen.com/explorer?endpoint=api%2Fgetting-started

   The StepZen Dashboard at dashboard.stepzen.com is the new default way to
   explore your GraphQL APIs. You can use the --dashboard=local flag to start
   a locally running GraphiQL instead.


Watching ~/stepzen for changes...

确认已部署的GraphQL

纵览StepZen应用程序

您可以使用StepZen Explorer在仪表板上测试GraphQL。请参考StepZen Explorer的详细信息。在仪表板上测试和查询部署在StepZen的GraphQL API。

访问仪表板。
仪表板的URL将在执行”stepzen start”命令时显示。
https://dashboard.stepzen.com/explorer?endpoint=api%2Fgetting-started

stepzen-explorer_1.png

你可以在”Builder”标签中创建查询,并使用”実行ボタン”执行查询。
你可以在”Docs”标签中查看可用的查询列表。

在这里,我们将执行一个查询,作为示例,将1美元换算成日元的结果。

stepzen-explorer_2.png

curl (Chinese paraphrase): 弯曲

我会使用curl来执行相同的查询,同时使用graphql格式和json格式两种方式。在Authorization头中,我会指定在步骤3中提供的Admin Key。

GraphQL的格式

~ % curl -i -X POST 'https://travilah.stepzen.net/api/getting-started/__graphql' \
 -H 'Authorization: apikey travilah::stepzen.io+1000::4ac3b69d000bae20e7f2d9a1b9249fc20a0f459b...' \
 -H 'Content-Type: application/graphql' \
 --data '{frankfurter_convertedAmount(amount: 1, from: "USD", to: "JPY")}'
HTTP/2 200
content-type: application/json; charset=utf-8
stepzen-trace: e9c5932306df684c0e777e4a9372541f
strict-transport-security: max-age=63072000; includeSubDomains; preload
vary: Origin
x-content-type-options: nosniff
date: Tue, 09 May 2023 14:10:48 GMT
content-length: 57
via: 1.1 google
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

{
	"data": {
		"frankfurter_convertedAmount": 135.14
	}
}%

JSON格式

~ % curl -i -X POST 'https://travilah.stepzen.net/api/getting-started/__graphql' \
 -H 'Authorization: apikey travilah::stepzen.io+1000::4ac3b69d000bae20e7f2d9a1b9249fc20a0f459b...' \
 -H 'Content-Type: application/json' \
 --data '{"query": "{frankfurter_convertedAmount(amount: 1, from: \"USD\", to: \"JPY\")}"}'
HTTP/2 200
content-type: application/json; charset=utf-8
stepzen-trace: a236f402a9d758f1d4ee92f92af2c9ba
strict-transport-security: max-age=63072000; includeSubDomains; preload
vary: Origin
x-content-type-options: nosniff
date: Tue, 09 May 2023 14:27:28 GMT
content-length: 57
via: 1.1 google
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

{
	"data": {
		"frankfurter_convertedAmount": 135.14
	}
}%

我可以在以上的StepZen免费版本中轻松创建和运行一个示例的GraphQL(getting-started)。

广告
将在 10 秒后关闭
bannerAds