使用GraphQL生成API文档

GraphiQL 是一个非常流行的交互式开发环境(IDE),专门用于浏览、编写和测试 GraphQL 查询。它不仅可以帮助你查询 API,还可以自动生成文档。以下是如何使用 GraphiQL 的具体步骤:

仓库地址:https://github.com/anvilco/spectaql?tab=readme-ov-file#yaml-options

1. GraphiQL 安装与配置

方式 : 本地或全局安装 GraphiQL

如果你的 GraphQL API 服务器没有内置 GraphiQL,你可以使用独立的 GraphiQL 框架或包。

  1. 全局安装 GraphiQL
    如果你想在本地环境使用 GraphiQL,你可以通过 npmyarn 安装:
    如果下载不成功可以使用淘宝镜像源

    npm install -g graphiql
  2. 通过 npm 或 Yarn 安装为开发依赖
    你也可以将 GraphiQL 作为开发依赖安装到项目中:

    npm install graphiql
  3. 生成您的文档!

    npx spectaql config.yml

    运行此命令你需要一份config.yml文件。具体使用参考https://github.com/anvilco/spectaql?tab=readme-ov-file#yaml-options

2. 使用JSON格式生成文档

生成或导出 schema 文件

  1. 自动生成 schema
    如果你使用的是 Java 类和注解方式定义的 GraphQL API(使用 @GraphQLQuery 等注解),GraphQL schema 通常在运行时生成。你可以使用 Spring Boot 启动后访问 GraphQL endpoint 来手动导出 schema。

    • 访问 GraphQL Endpoint:在应用运行时,GraphQL API 通常暴露在 /graphql 路径下。你可以通过 introspection 查询导出完整的 GraphQL schema。

    下面是一个 introspection 查询的示例,它可以帮助你获取 schema:这个查询可根据文档需要动态调整。查询http://127.0.0.1:8091/pamirs/graphql

    query IntrospectionQuery {
     __schema {
       queryType { ...FullType }
       mutationType { ...FullType }
       subscriptionType { name }
       types {
         ...FullType
       }
       directives {
         name
         description
         locations
         args {
           ...InputValue
         }
       }
     }
    }
    
    fragment FullType on __Type {
     kind
     name
     description
     fields(includeDeprecated: true) {
       name
       description
       args {
         ...InputValue
       }
       type {
         ...TypeRef
       }
       isDeprecated
       deprecationReason
     }
     inputFields {
       ...InputValue
     }
     interfaces {
       ...TypeRef
     }
     enumValues(includeDeprecated: true) {
       name
       description
       isDeprecated
       deprecationReason
     }
     possibleTypes {
       ...TypeRef
     }
    }
    
    fragment InputValue on __InputValue {
     name
     description
     type { ...TypeRef }
     defaultValue
    }
    
    fragment TypeRef on __Type {
     kind
     name
     ofType {
       kind
       name
       ofType {
         kind
         name
         ofType {
           kind
           name
           ofType {
             kind
             name
             ofType {
               kind
               name
             }
           }
         }
       }
     }
    }

    你可以将这个查询放在 GraphiQL(开发工具)或者 Postman 等工具中,发送请求到 /graphql,这样可以获得完整的 schema。

将请求的响应保存为JSON文件,在config.yml中配置。配置参考:https://github.com/anvilco/spectaql/blob/main/config-example.yml

spectaql:
  # Optional path to the target build directory.
  # Set to null to not write the output to the filesystem, making it only available via the API (default: public)
  #
  # Default: public
  targetDir: /Users/mmy/Desktop
  themeDir: /Users/mmy/Desktop

introspection:
  # File containing a GraphQL Schema Definition written in SDL.
  # Can also pass an array of paths (or glob supported by @graphql-tools/load-files)
  # like so:
  # schemaFile:
  #   - path/to/schema/part1.gql
  #   - path/to/schema/part2.gql
#  schemaFile: /Users/mmy/Desktop/schema2.graphql

  # File containing Introspection Query response in JS module export, or JSON format
  introspectionFile: /Users/mmy/Documents/response2.json
#
  # URL of the GraphQL endpoint to hit if you want to generate the documentation based on live Introspection Query results
  # NOTE: If not using introspection.url OR servers[], you need to provide x-url below
#  url: 'http://127.0.0.1:8091/pamirs/graphql'

extensions:
  # Utilize the 'graphql-scalars' library when generating examples for scalars it supports that
  # do not have an example already set via some other method. Usually this is a good
  # thing to have on, but it is possible to turn it off.
  # Default: true
  graphqlScalarExamples: true

servers:
  - url: http://127.0.0.1:8091/pamirs/base

运行命令npx spectaql config.yml就可以得到一份html的API文档啦!

作者注:这行命令生成文档的执行时间和文件大小息息相关。平台接口执行时间大概1-2小时,只要没报错请耐心等待!

Oinone社区 作者:yexiu原创文章,如若转载,请注明出处:https://doc.oinone.top/chan-pin-shi-yong-shou-ce/17412.html

访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验

(0)
yexiu的头像yexiu数式员工
上一篇 6天前
下一篇 20小时前

相关推荐

  • 系统配置

    系统配置模块帮助企业在平台中实现个性化,以满足不同的业务需求和企业文化展示。该模块主要包含二个部分:全局配置、应用配置。

    2024年8月30日
    29200
  • 新版权限操作手册(5.0以上)

    一 整体介绍 1. 角色类型: 为了更好地管理和控制平台中的不同用户,权限系统将对角色进行分类定义。通过角色分类,可以更高效地管理同一类角色,无需单独操作每个角色,实现统一管理。 2. 角色管理: 角色管理是权限系统中的一个基础功能,负责管理具体角色的创建、编辑和禁用,以及对角色的单独操作。 3. 系统权限: 可配置某个应用或其下的某个菜单、某个应用的首页的…

    2024年7月2日
    54500
  • 设计导入导出(5.1.0及以上版本)

    一 产品功能说明 1. 设计同步部署 设计同步部署功能实现不同环境设计数据一键同步的目标,即将A环境设计完成的模型、界面、等设计数据一键部署至B环境。 使用此功能,需要在‘应用中心’安装‘应用环境’: ‘应用环境’安装完成,进入发起同步部署环境——>业务应用——>‘应用环境’,对要进行快速部署的发起环境、目标环境进行配置: 在进行多个环境同步部署…

    2024年9月2日
    4900
  • 应用中心

    在App Finder 中点击应用中心可以进入Oinone的应用中心,可以看到Oinone平台所有应用列表、应用大屏、以及技术可视化。

    2024年8月30日
    7000
  • 业务审计

    业务审计是指通过跟踪用户在平台业务应用中的各类行为操作以及关联的数据变化,支撑企业业务审计所需。平台提供可视化审计规则配置,根据审计规则记录行为日志,其中日志包括登录日志、应用日志;

    2024年9月2日
    3100

发表回复

登录后才能评论