Skip to content

重构:使用 GraphQL 获取 Github APIs  #3

@zjp-CN

Description

@zjp-CN

https://docs.github.com/en/graphql/overview/about-the-graphql-api

https://lib.rs/crates/graphql_client

https://github.com/graphql-rust/graphql-client/blob/main/examples/github/examples/github.rs

原因:

  • 官方文档建议:要创建集成、检索数据和自动化工作流程,请使用 GitHub GraphQL API。 GitHub GraphQL API 提供比 GitHub REST API 更精确、更灵活的查询。
  • 示例 0:REST API 一次只能查询一个仓库的一个维度的数据,而 GraghQL 一次查询多个仓库的多个维度的数据。
  • 示例 1:当前 REST API 查询一个仓库的某分支的总提交数,需要某些技巧 https://stackoverflow.com/questions/27931139/how-to-use-github-v3-api-to-get-commit-count-for-a-repo ,而 GraphQL 可以直接查询这个数量。
  • 示例 2:REST API 可以被认为是固定死的结构化的查询结果,如果抽取自己的部分字段,则需要定义与原结构类似的结构,这可能导致不必要的嵌套结构 —— 而 GraphQL 让使用者直接定义所需的数据结构。
// REST API
struct Info {
    owner: Owner,
    license: Option<License>,
}
struct Owner {
    /// user name
    login: String,
    /// Organization, User, Bot or something
    r#type: String,
}
struct License {
    spdx_id: String,
}

// GraphQL: 只需要定义 scheme 来描述如何查询
struct Info {
    owner: String,
    owner_type: String,
    license: Option<String>,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions