AWS AppSync中GraphQL三引号文档字符串不显示问题求助
AWS AppSync目前不原生支持GraphQL标准的三引号(""")文档字符串语法。不管是通过CloudFormation部署包含这类文档的Schema,还是直接在AppSync Schema编辑器UI中添加三引号注释,最终这些文档都不会在Schema编辑器或Documentation Explorer中显示,甚至在UI中添加后保存会直接消失。另外,使用#添加的行内注释仅会保留在Schema编辑器中,同样无法在Documentation Explorer中展示。
可行替代方案
1. 使用AppSync专属@documentation指令
AppSync提供了自定义的@documentation指令,专门用于为Schema元素添加描述,这些描述会正常显示在Documentation Explorer中。示例:
type EntitlementData @documentation(description: "Represents the entitlement data for a user.") { id: ID! @documentation(description: "The unique identifier for the entitlement data.") }
部署该Schema后,你可以在Documentation Explorer中查看到对应的类型和字段描述,这是目前官方支持的、能在AppSync控制台内展示文档的唯一方式。
2. #注释+第三方文档生成工具
如果需要在Schema代码中保留直观的注释,同时生成可共享的结构化文档,可以用#添加行内注释,再借助第三方工具(如GraphQL Code Generator的文档类插件)从Schema文件生成静态文档(Markdown/HTML格式)。示例Schema:
# Represents the entitlement data for a user. type EntitlementData { # The unique identifier for the entitlement data. id: ID! }
通过工具生成的文档可以在团队内部共享,弥补AppSync自身Documentation Explorer的局限性。
3. 基于Introspection构建自定义文档
通过GraphQL introspection查询获取包含@documentation描述的Schema元数据,然后自行构建文档展示页面,或者将introspection结果导入到GraphQL Playground、Altair等工具中,查看完整的Schema文档。
内容的提问来源于stack exchange,提问作者Ioan

