Skip to content

Commit e48c2b5

Browse files
Add information about choosing which API to use (github#30781)
Co-authored-by: Lucas Costi <[email protected]>
1 parent a3a154a commit e48c2b5

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

content/developers/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ featuredLinks:
1111
- /developers/apps/building-github-apps/authenticating-with-github-apps
1212
- /developers/apps/building-github-apps/identifying-and-authorizing-users-for-github-apps
1313
popular:
14+
- /developers/overview/about-githubs-apis
1415
- /developers/webhooks-and-events/webhooks/webhook-events-and-payloads
1516
- /developers/apps/building-github-apps/creating-a-github-app
1617
- /developers/apps/building-github-apps/authenticating-with-github-apps
1718
- /developers/webhooks-and-events/webhooks/about-webhooks
18-
- /developers/apps/building-oauth-apps/creating-an-oauth-app
1919
- /developers/apps/building-oauth-apps/authorizing-oauth-apps
2020
- /developers/github-marketplace/github-marketplace-overview/about-github-marketplace
2121
guideCards:

content/developers/overview/about-githubs-apis.md

+69-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,72 @@ topics:
1414
- API
1515
---
1616

17-
There are two stable versions of the GitHub API: the [REST API](/rest) and the [GraphQL API](/graphql).
17+
## About {% data variables.product.company_short %}'s APIs
18+
19+
{% data variables.product.company_short %} provides two APIs: a REST API and a GraphQL API. You can interact with both APIs using {% data variables.product.prodname_cli %}, curl, the official Octokit libraries, and third party libraries. Occasionally, a feature may be supported on one API but not the other.
20+
21+
You should choose the API that best aligns with your needs and that you are most comfortable using. This article discusses the benefits of each API.
22+
23+
For more information about the GraphQL API, see [the GraphQL documentation](/graphql). For more information about the REST API, see [the REST documentation](/rest).
24+
25+
## Choosing the GraphQL API
26+
27+
The GraphQL API returns exactly the data that you request. GraphQL also returns the data in a pre-known structure based on your request. In contrast, the REST API returns more data than you requested and returns it in a pre-determined structure. You can also accomplish the equivalent of multiple REST API request in a single GraphQL request. The ability to make fewer requests and fetch less data makes GraphQL appealing to developers of mobile applications.
28+
29+
For example, to get the {% data variables.product.product_name %} login of ten of your followers, and the login of ten followers of each of your followers, you can send a single request like:
30+
31+
```graphql
32+
{
33+
viewer {
34+
followers(first: 10) {
35+
nodes {
36+
login
37+
followers(first: 10) {
38+
nodes {
39+
login
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}
46+
```
47+
48+
The response will be a JSON object that follows the structure of your request.
49+
50+
In contrast, to get this same information from the REST API, you would need to first make a request to `GET /user/followers`. The API would return the login of each follower, along with other data about the followers that you don't need. Then, for each follower, you would need to make a request to `GET /users/{username}/followers`. In total, you would need to make 11 requests to get the same information that you could get from a single GraphQL request, and you would receive excess data.
51+
52+
## Choosing the REST API
53+
54+
Because REST APIs have been around for longer than GraphQL APIs, some developers are more comfortable with the REST API. Since REST APIs use standard HTTP verbs and concepts, many developers are already familiar with the basic concepts to use the REST API.
55+
56+
For example, to create an issue in the `octocat/Spoon-Knife` repository, you would need to send a request to `POST /repos/octocat/Spoon-Knife/issues` with a JSON request body:
57+
58+
```json
59+
{
60+
"title": "Bug with feature X",
61+
"body": "If you do A, then B happens"
62+
}
63+
```
64+
65+
In contrast, to make an issue using the GraphQL API, you would need to get the node ID of the `octocat/Spoon-Knife` repository and then send a request like:
66+
67+
```graphql
68+
mutation {
69+
createIssue(
70+
input: {
71+
repositoryId: "MDEwOlJlcG9zaXRvcnkxMzAwMTky"
72+
title: "Bug with feature X"
73+
body: "If you do A, then B happens"}
74+
) {
75+
issue {
76+
number
77+
url
78+
}
79+
}
80+
}
81+
```
82+
83+
## Choosing both
84+
85+
You don't need to exclusively use one API over the other. Node IDs let you move between the REST API and GraphQL API. For more information, see "[Using global node IDs](/graphql/guides/using-global-node-ids)."

content/graphql/guides/migrating-from-rest-to-graphql.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ shortTitle: Migrate from REST to GraphQL
1616

1717
## Differences in API logic
1818

19+
{% data variables.product.company_short %} provides two APIs: a REST API and a GraphQL API. For more information about {% data variables.product.company_short %}'s APIs, see "[About {% data variables.product.company_short %}'s APIs](/developers/overview/about-githubs-apis)."
20+
1921
Migrating from REST to GraphQL represents a significant shift in API logic. The differences between REST as a style and GraphQL as a specification make it difficult&mdash;and often undesirable&mdash;to replace REST API calls with GraphQL API queries on a one-to-one basis. We've included specific examples of migration below.
2022

2123
To migrate your code from the [REST API](/rest) to the GraphQL API:

content/graphql/overview/about-the-graphql-api.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Here are some quick links to get you up and running with the GraphQL API:
2020
* [Rate limits](/graphql/overview/resource-limitations)
2121
* [Migrating from REST](/graphql/guides/migrating-from-rest-to-graphql)
2222

23+
For more information about {% data variables.product.company_short %}'s APIs, see "[About {% data variables.product.company_short %}'s APIs](/developers/overview/about-githubs-apis)."
24+
2325
## About GraphQL
2426

2527
The [GraphQL](https://graphql.github.io/) data query language is:

content/rest/guides/getting-started-with-the-rest-api.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ When you make a request to the REST API, you will specify an HTTP method and a p
2020

2121
The REST API reference documentation describes the HTTP method, path, and parameters for every operation. It also displays example requests and responses for each operation. For more information, see the [REST reference documentation](/rest).
2222

23+
For more information about {% data variables.product.company_short %}'s APIs, see "[About {% data variables.product.company_short %}'s APIs](/developers/overview/about-githubs-apis)."
24+
2325
## Making a request
2426

2527
To make a request, first find the HTTP method and the path for the operation that you want to use. For example, the "Get Octocat" operation uses the `GET` method and the `/octocat` path. For the full reference documentation for this operation, see "[Get Octocat](/rest/meta#get-octocat)."

0 commit comments

Comments
 (0)