简体   繁体   English

如何使用 AWS Appsync 创建自定义的、硬编码的运行状况检查 GraphQL 查询?

[英]How to create a custom, hardcoded health check GraphQL query with AWS Appsync?

I'm new to AWS Appsync and to GraphQL.我是 AWS Appsync 和 GraphQL 的新手。

Previously, I used to create REST APIs in Python.以前,我曾在 Python 中创建 REST API。 I was always creating a GET /health-check endpoint, sending back, for example and among many other info, the API version number, easily parsed from the project descriptor pyproject.toml file.我总是创建一个GET /health-check端点,发回 API 版本号等许多其他信息,很容易从项目描述符pyproject.toml文件中解析出来。 That helped me massively to maintain APIs: with a single GET query in my browser, I was always able to instantly get if branch/version it was, the status of other services, etc. .这极大地帮助了我维护 API:在我的浏览器中使用单个GET查询,我总是能够立即获得它是否是分支/版本、其他服务的状态等。

I want to do something similar with AWS Appsync / GraphQL and my IaC tool (Pulumi).我想用 AWS Appsync/GraphQL 和我的 IaC 工具 (Pulumi) 做一些类似的事情。 Since I'm using IaC tool Pulumi in Python, I could still easily get the info I need and inject them in any resolver response template.由于我在 Python 中使用 IaC 工具 Pulumi,我仍然可以轻松获取我需要的信息并将它们注入任何解析器响应模板。

But if I create a resolver, should I create a corresponding health-check query itself in the GraphQL schema?但是如果我创建一个解析器,我是否应该在 GraphQL 模式中创建一个相应的健康检查查询本身? When creating a resolver with a hardcoded JSON response, should it be associated with a GraphQL query in the schema, and if yes, how should that query in the schema look like?当使用硬编码的 JSON 响应创建解析器时,它是否应该与模式中的 GraphQL 查询相关联,如果是,该模式中的查询应该是什么样子?

I finally found a way, but it's a very ugly workaround since AppSync VTL resolvers have a lot of limitations and since I'm using Pulumi as a Iac tool which also doesn't accept all arguments when creating a resolver - for example for GetItem operation it needs an id key in the request template.我终于找到了一种方法,但这是一个非常难看的解决方法,因为 AppSync VTL 解析器有很多限制,而且我使用 Pulumi 作为 Iac 工具,它在创建解析器时也不接受所有参数——例如GetItem操作它需要请求模板中的id键。

I'm posting the workaround here anyway if it can be of any help to someone.如果对某人有任何帮助,我还是会在这里发布解决方法。

GraphQL schema: GraphQL 模式:

schema {
  query: Query
}
type Query {
  getHealthCheck: String
}

AppSync getHealthCheck resolver request template: AppSync getHealthCheck解析程序请求模板:

{
    "version": "2018-05-29",
    "operation": "GetItem",
    "key": {
        "id": $util.dynamodb.toDynamoDBJson(""),
    },
}

AppSync getHealthCheck resolver response template example: AppSync getHealthCheck解析程序响应模板示例:

"'version':'0.1', 'branch':'staging', 'commitId':'abc123'"

So in a IaC tool like Pulumi using Python, one could build the response template like that:因此,在使用 Python 的 Pulumi 这样的 IaC 工具中,可以像这样构建响应模板:

"""
"'version':'{}', 'branch':'{}', 'commitId':'{}'"
""".format(
    version,
    os.environ.get("GITHUB_REF_NAME", "unknown"),
    os.environ.get("GITHUB_SHA", "unknown"),
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM