{"meta":{"title":"RESTからGraphQLへの移行","intro":"GitHubの REST API からGitHubの GraphQL API に移行するためのベスト プラクティスと考慮事項について説明します。","product":"GraphQL API","breadcrumbs":[{"href":"/ja/enterprise-cloud@latest/graphql","title":"GraphQL API"},{"href":"/ja/enterprise-cloud@latest/graphql/guides","title":"ガイド"},{"href":"/ja/enterprise-cloud@latest/graphql/guides/migrating-from-rest-to-graphql","title":"REST から GraphQL に移行する"}],"documentType":"article"},"body":"# RESTからGraphQLへの移行\n\nGitHubの REST API からGitHubの GraphQL API に移行するためのベスト プラクティスと考慮事項について説明します。\n\n## APIのロジックに関する差異\n\nGitHub には、REST API と GraphQL API の 2 つの API が用意されています。\nGitHubの API の詳細については、「[GitHubの REST API と GraphQL API の比較](/ja/enterprise-cloud@latest/rest/about-the-rest-api/comparing-githubs-rest-api-and-graphql-api)」を参照してください。\n\nRESTからGraphQLへの移行は、APIロジックの大きな変化を示します。 スタイルとしての REST と仕様としての GraphQL との違いのために、REST API の呼び出しを GraphQL API のクエリに 1 対 1 で置き換えることは難しく、—しばしば望まない—結果になります。 移行の具体的な例を以下に示しました。\n\nコードを [REST API](/ja/enterprise-cloud@latest/rest) から GraphQL API に移行するには、以下を行います。\n\n* [GraphQL 仕様](https://spec.graphql.org/June2018/)を確認する\n* GitHubの <c0>GraphQL スキーマ\\</c0 を確認します>\n* 現在、GitHub REST API とやり取りしている既存のコードを検討する\n* [Global ノード ID](/ja/enterprise-cloud@latest/graphql/guides/using-global-node-ids) を使用して API バージョン間でオブジェクトを参照する\n\nGraphQLによる重要な利点には以下があります。\n\n* [必要とするデータだけを取得できる](#example-getting-the-data-you-need-and-nothing-more)\n* [入れ子になったフィールド](#example-nesting)\n* [強い型付け](#example-strong-typing)\n\n以下にそれぞれの例を示します。\n\n## 例：必要なデータだけを取得\n\n1つのREST API呼び出しで、Organizationのメンバーのリストを取得します。\n\n```shell\ncurl -v https://api.github.com/orgs/:org/members\n```\n\n目的がメンバー名とアバターへのリンクの取得だけなのであれば、このRESTのペイロードには過剰なデータが含まれています。 しかし、GraphQLのクエリでは指定した内容だけが返されます。\n\n```graphql\nquery {\n    organization(login:\"github\") {\n    membersWithRole(first: 100) {\n      edges {\n        node {\n          name\n          avatarUrl\n        }\n      }\n    }\n  }\n}\n```\n\n別の例を考えてみましょう。プルリクエストのリストを取得して、それぞれがマージ可能かをチェックします。 REST API を呼び出すと、pull request とその [概要表現](/ja/enterprise-cloud@latest/rest#summary-representations)の一覧が取得されます。\n\n```shell\ncurl -v https://api.github.com/repos/:owner/:repo/pulls\n```\n\npull request がマージ可能かを判断するには、個別にそれぞれの pull request の[詳細な表現](/ja/enterprise-cloud@latest/rest#detailed-representations) (大きなペイロード) を取得し、その `mergeable` 属性が true か false かをチェックする必要があります。\n\n```shell\ncurl -v https://api.github.com/repos/:owner/:repo/pulls/:number\n```\n\nGraphQL では、各 pull request の `number` 属性と `mergeable` 属性のみを取得できます。\n\n```graphql\nquery {\n    repository(owner:\"octocat\", name:\"Hello-World\") {\n    pullRequests(last: 10) {\n      edges {\n        node {\n          number\n          mergeable\n        }\n      }\n    }\n  }\n}\n```\n\n## 例：入れ子\n\n入れ子になったフィールドにクエリを行うことで、複数のRESTの呼び出しを少数のGraphQLクエリに置き換えられます。 たとえば、**REST API** を使って、コミット、非レビュー コメント、レビューと一緒に pull request を取得するには、4 つの別々の呼び出しが必要になります。\n\n```shell\ncurl -v https://api.github.com/repos/:owner/:repo/pulls/:number\ncurl -v https://api.github.com/repos/:owner/:repo/pulls/:number/commits\ncurl -v https://api.github.com/repos/:owner/:repo/issues/:number/comments\ncurl -v https://api.github.com/repos/:owner/:repo/pulls/:number/reviews\n```\n\n**GraphQL API** を使えば、入れ子のフィールドを利用して単一のクエリでこのデータを取得できます。\n\n```graphql\n{\n  repository(owner: \"octocat\", name: \"Hello-World\") {\n    pullRequest(number: 1) {\n      commits(first: 10) {\n        edges {\n          node {\n            commit {\n              oid\n              message\n            }\n          }\n        }\n      }\n      comments(first: 10) {\n        edges {\n          node {\n            body\n            author {\n              login\n            }\n          }\n        }\n      }\n      reviews(first: 10) {\n        edges {\n          node {\n            state\n          }\n        }\n      }\n    }\n  }\n}\n```\n\nまた、pull request 番号の[変数を置き換える](/ja/enterprise-cloud@latest/graphql/guides/forming-calls-with-graphql#working-with-variables)ことで、このクエリの機能を拡張することもできます。\n\n## 例：強力な型付け\n\nGraphQLスキーマは強く型付けされており、データの扱いが安全になっています。\n\nGraphQL [ミューテーション](/ja/enterprise-cloud@latest/graphql/reference)を使用して問題または pull request にコメントを追加し、[`clientMutationId`](/ja/enterprise-cloud@latest/graphql/reference/issues#mutation-addcomment) の値に文字列ではなく整数を誤って指定する例を考えてみましょう。\n\n```graphql\nmutation {\n  addComment(input:{clientMutationId: 1234, subjectId: \"MDA6SXNzdWUyMjcyMDA2MTT=\", body: \"Looks good to me!\"}) {\n    clientMutationId\n    commentEdge {\n      node {\n        body\n        repository {\n          id\n          name\n          nameWithOwner\n        }\n        issue {\n          number\n        }\n      }\n    }\n  }\n}\n```\n\nこのクエリを実行すると、この操作に期待される型を指定したエラーが返されます。\n\n```json\n{\n  \"data\": null,\n  \"errors\": [\n    {\n      \"message\": \"Argument 'input' on Field 'addComment' has an invalid value. Expected type 'AddCommentInput!'.\",\n      \"locations\": [\n        {\n          \"line\": 3,\n          \"column\": 3\n        }\n      ]\n    },\n    {\n      \"message\": \"Argument 'clientMutationId' on InputObject 'AddCommentInput' has an invalid value. Expected type 'String'.\",\n      \"locations\": [\n        {\n          \"line\": 3,\n          \"column\": 20\n        }\n      ]\n    }\n  ]\n}\n```\n\nクオートで `1234` をラップすると、この値を整数値から期待されている型である文字列に変換できます。\n\n```graphql\nmutation {\n  addComment(input:{clientMutationId: \"1234\", subjectId: \"MDA6SXNzdWUyMjcyMDA2MTT=\", body: \"Looks good to me!\"}) {\n    clientMutationId\n    commentEdge {\n      node {\n        body\n        repository {\n          id\n          name\n          nameWithOwner\n        }\n        issue {\n          number\n        }\n      }\n    }\n  }\n}\n```"}