GraphQL
![]() | |
開発元 | Facebookとコミュニティ |
---|---|
初版 | 2015年 |
最新版 |
2018年6月[1]
|
リポジトリ |
github |
プログラミング 言語 | Java、JavaScript、Ruby、Scalaなど |
対応OS | クロスプラットフォーム |
ライセンス | 3条項BSDライセンス |
公式サイト |
graphql |
GraphQL(グラフQL)はAPI向けに作られたクエリ言語およびランタイムである[2]。
ウェブAPIの開発に、RESTやその他のWebサービスと比較して、効率的、堅牢、フレキシブルなアプローチを提供する。GraphQLでは、クライアントが必要なデータの構造を定義することができ、サーバーからは定義したのと同じ構造のデータが返される。したがって、必要以上に大きなデータが返されるのを防ぐことができる。ただし、これはクエリ結果のウェブキャッシュの効果に影響を与える。また、柔軟性と豊かな表現が可能なクエリ言語は複雑さを加えるため、シンプルなAPIには適さない可能性もある[3][4][5]。GraphQLは、型システム、クエリ言語、実行セマンティクス、静的な検証、型イントロスペクションから構成される。
GraphQLは、読み込み、書き込み(ミューテーション)、データのサブスクリプション(リアルタイム更新機能で、一般にはWebHooksで実装される)をサポートする[6]。
概要
GraphQLでは、クライアントがクエリ内容を記したdocumentを送信し、GraphQLサービスがクエリを実行し結果を返信する。documentはDSLであるGraphQL query languageを用いて記述される[7]。以下の例は明日の天気を取得するクエリのdocumentとGraphQLサービスから返されたクエリ結果のJSONである。
// document
{
tomorrow {
weather
rainyPercent
}
}
// response JSON
{
"tomorrow": {
"weather": "cloudy",
"rainyPercent": 30
}
}
リソースをURLパスで表現するRESTful APIと異なり、GraphQLはリソースをdocumentで表現する。GraphQL WebAPIの場合、単一のAPIエンドポイントへこのdocumentをPOSTする(例:https://API.internal./graphql
エンドポイントへdocumentをBodyとしてPOSTする)ことでクエリが実行される(参考: アプリケーションプログラミングインタフェース#様式 )。
GraphQLはあくまでAPI仕様であり、通信方式の実装とは独立している。例えばGraphQL WebAPIを実装する際、documentをURLクエリ文字列に載せることも可能である(例:GET https://API.internal./graphql?query={tomorrow{weather}}
)[8]。
機能
Arguments
GraphQLではargumentを用いたクエリの制御が可能である。GraphQLオブジェクトの各field(例: tomorrow
)は0個以上のargumentsを持てる[9]。GraphQLサービスではクエリをトップレベルのfieldからresolverによって解決していくが、argumentsはその時にresolverへ渡されresolverの振る舞いを制御する[10]。次の例では"都道府県名/name" argumentと"天気/weather" argumentを用い、都道府県-街リストから東京/tokyoに含まれる晴れ/sunnyの街を取得する。
// document
{
prefecture(name: "Tokyo") {
prefName
cities(weather: "sunny") {
cityName
rainyPercent
}}}
// response JSON
{
"prefecture": {
{
"prefName": "Tokyo",
"cities": [
{
"cityName": "Shinjuku",
"rainyPercent": 10
},{
"cityName": "Ikebukuro",
"rainyPercent": 0
}]}}}
directives
GraphQLではdirectivesを用いたクエリの制御が可能である。GraphQLオブジェクトの各field(例: tomorrow
)はdirectiveを持てる[11]。directivesは処理系に依存したあらゆる処理を指定するアノテーションである。全ての処理系に実装されるdirectiveの例としてfieldの無視を指定する@include(if: Boolean)
がある。
// document
{
sinjuku {
weather
}
ikebukuro @include(if: false) {
weather
rainyPercent
}}
// response JSON
{
"sinjuku": {
"weather": "cloudy"
}}
全てのresolverに@include
相当の引数を用意すれば同等の機能を提供できるが、directivesを用いることでresolverに手を加えずかつdirective処理系1箇所での実装のみでこの機能を提供できる。
directivesはGraphQLサービス処理系以外にも利用されうる。例えばAWS AmplifyはSchema上のdirectivesに基づいたクラウドリソースプロビジョニングコード生成("GraphQL Transform")を実装している[12]。@model
が付与されたfieldはresolverが自動で生成されかつDynamoDBにリソースが準備される。これはGraphQLサービス処理系ではなく、Amplify CLIによっておこなわれる。
形式
Document
Documentは1つ以上のOperationDefinitionからなる。1つのみの場合Nameが省略可能であり、さらにOperationTypeがqueryならこれも省略できる。
// document
OpType Name [VarDef][Directives] {
...
}
OpType Name [VarDef][Directives] {
...
}
// e.g.
query ExampleQuery1 {
resource
}
query ExampleQuery2 ($var: S) @skip(if: false) {
time
}
Field
FieldはNameからなっており、Alias, Arguments, Directivesを利用できる。さらにサブFieldを持つことができる。
// document
{
[Alias] Name [Arguments][Directives] [SelectionSet]
}
// e.g.1 - simple
{
resource
}
// e.g.2 - full
{
time: resource (arg1: "arg") @skip(if:false) {
subResource
}
}
歴史
GraphQLは、2012年にFacebookの内部で開発され、2015年に公開された[13]。2018年11月7日、GraphQLプロジェクトは、Facebookから非営利のLinux Foundationがホストする新たに設立されたGraphQL Foundationに移譲された[14][15]。2012年以来、GraphQLの利用数は、GraphQLの作者のLee Byronが想定したとおりのスケジュールに正確に従って増加している[16] 。Byronの目標は、GraphQLをWebプラットフォーム全体で利用されるようにすることである。
2018年2月9日、GraphQL Schema Definition Language(SDL)が仕様の一部となった[17]。
実装
主要なGraphQLのクライアント実装としては、Apollo Client[18]とRelay[19]がある。GraphQLサーバーは複数の言語で利用可能であり、Haskell、JavaScript[20]、Perl[21]、Python[22]、Ruby、Java、C#、Scala、Go、Elixir[23]、Erlang、PHP、R、Clojureで書かれたものがある。
関連項目
出典
- ^ “GraphQL June 2018 Release Notes”. 2019年3月26日閲覧。
- ^ GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. https://graphql.org/
- ^ “GraphQL vs REST: Overview” (英語). Phil Sturgeon. (2017年1月24日) 2018年11月25日閲覧。
{{cite news}}
:|date=
の日付が不正です。 (説明)⚠ - ^ “Why use GraphQL, good and bad reasons” (英語). Honest Engineering. (2018年8月4日) 2018年11月26日閲覧。
- ^ “GraphQL Fundamentals”. Howto GraphQL. 2018年7月4日閲覧。
- ^ “GraphQL”. facebook.github.io. Facebook. 2018年7月4日閲覧。
- ^ Clients use the GraphQL query language to make requests to a GraphQL service. We refer to these request sources as documents. GraphQL specification
- ^ When receiving an HTTP GET request, the GraphQL query should be specified in the "query" query string. GraphQL
- ^ Every field on a GraphQL object type can have zero or more arguments GraphQL specification
- ^ Each field on each type is backed by a function called the resolver which is provided by the GraphQL server developer. When a field is executed, the corresponding resolver is called to produce the next value. GraphQL
- ^ A directive can be attached to a field or fragment inclusion, and can affect execution of the query in any way the server desires. GraphQL
- ^ Because the
Todo
type was decorated with an@model
directive of the GraphQL Transform library, the CLI created the additional schema and resolvers for queries, mutations, and subscriptions as well as a DynamoDB table to hold the Todos. Amplify Libraries - ^ “GraphQL: A data query language”. 2019年10月19日閲覧。
- ^ “Facebook’s GraphQL gets its own open-source foundation” (英語). TechCrunch 2018年11月7日閲覧。
- ^ “The Linux Foundation Announces Intent to Form New Foundation to Support GraphQL - The Linux Foundation” (英語). The Linux Foundation. (2018年11月6日) 2018年11月7日閲覧。
- ^ Anthony. “Is GraphQL Moving Toward Ubiquity?”. NordicAPIs. 2019年10月19日閲覧。
- ^ “[RFC GraphQL Schema Definition Language (SDL) by leebyron · Pull Request #90 · graphql/graphql-spec]” (英語). GitHub. 2019年10月19日閲覧。
- ^ “Introduction”. Apollo GraphQL Docs. 2019年10月19日閲覧。
- ^ “Relay · A JavaScript framework for building data-driven React applications” (英語). relay.dev. 2019年10月19日閲覧。
- ^ A reference implementation of GraphQL for JavaScript: graphql/graphql-js, GraphQL, (2019-10-19) 2019年10月19日閲覧。
- ^ “GraphQL - Perl implementation of GraphQL - metacpan.org”. metacpan.org. 2019年10月19日閲覧。
- ^ “Graphene”. graphene-python.org. 2017年6月18日閲覧。
- ^ “Absinthe: The GraphQL toolkit for Elixir”. 2018年7月19日閲覧。