# GraphQL API 的速率限制和查询限制

GitHub GraphQL API 设有限制，以防止向 GitHub 的服务器发出过多或滥用的请求。

## 主要速率限制

默认情况下 GitHub Enterprise Server禁用速率限制。 请与站点管理员联系，以确认实例的速率限制。

如果你是网站管理员，可以为实例设置速率限制。 有关详细信息，请参阅“[配置速率限制](/zh/enterprise-server@3.22/admin/configuring-settings/configuring-user-applications-for-your-enterprise/configuring-rate-limits)”。

如果为超出您实例范围的用户或组织开发应用程序，则适用标准 GitHub 速率限制。 有关详细信息，请参阅文档中的 [](/zh/graphql/overview/rate-limits-and-query-limits-for-the-graphql-api)GitHub Free。

## 节点限制

若要通过 [schema](/zh/enterprise-server@3.22/graphql/guides/introduction-to-graphql#schema) 验证，所有 GraphQL API [calls](/zh/enterprise-server@3.22/graphql/guides/forming-calls-with-graphql)必须满足以下标准：

* 客户端必须在任何 `first` 上提供 `last` 或 [](/zh/enterprise-server@3.22/graphql/guides/introduction-to-graphql#connection) 参数。
* `first` 和 `last` 的值必须在 1-100 以内。
* 单个调用不能请求超过 500,000 个[节点](/zh/enterprise-server@3.22/graphql/guides/introduction-to-graphql#node)。

### 计算调用中的节点

下面两个示例显示如何计算调用中的节点总数。

1. 简单查询：

   <pre>query {
     viewer {
       repositories(first: <span class="redbox">50</span>) {

edges {
repository:node {
name

issues(first: <span class="greenbox">10</span>) {
totalCount
edges {
node {
title
bodyHTML
}
}
}
}
}
}
}
}</pre>

计算：

   <pre><span class="redbox">50</span>         = 50 repositories
    +
   <span class="redbox">50</span> x <span class="greenbox">10</span>  = 500 repository issues

= 550 total nodes</pre>

1. 复杂查询：

   <pre>query {
     viewer {
       repositories(first: <span class="redbox">50</span>) {

edges {
repository:node {
name

pullRequests(first: <span class="greenbox">20</span>) {
edges {
pullRequest:node {
title

comments(first: <span class="bluebox">10</span>) {
edges {
comment:node {
bodyHTML
}
}
}
}
}
}

issues(first: <span class="greenbox">20</span>) {
totalCount
edges {
issue:node {
title
bodyHTML

comments(first: <span class="bluebox">10</span>) {
edges {
comment:node {
bodyHTML
}
}
}
}
}
}
}
}
}

```
   followers(first: <span class="bluebox">10</span>) {
```

edges {
follower:node {
login
}
}
}
}
}</code></pre>

计算：

   <pre><span class="redbox">50</span>              = 50 repositories
    +
   <span class="redbox">50</span> x <span class="greenbox">20</span>       = 1,000 pullRequests
    +
   <span class="redbox">50</span> x <span class="greenbox">20</span> x <span class="bluebox">10</span> = 10,000 pullRequest comments
    +
   <span class="redbox">50</span> x <span class="greenbox">20</span>       = 1,000 issues
    +
   <span class="redbox">50</span> x <span class="greenbox">20</span> x <span class="bluebox">10</span> = 10,000 issue comments
    +
   <span class="bluebox">10</span>              = 10 followers

= 22,060 total nodes</pre>

## 查询优化策略

* **限制对象数量**：对 `first` 或 `last` 参数使用较小值，并对结果分页。
* **减少查询深度**：除非必要，否则请避免请求深度嵌套对象。
* **筛选结果**：使用参数筛选数据并仅返回所需内容。
* **拆分大型查询**：将复杂查询拆分为多个更简单的查询。
* **仅请求必填字段**：仅选择所需字段，而不是请求所有可用字段。

通过遵循这些策略，可以降低达到资源限制的可能性，并提高 API 请求的性能和可靠性。