GraphQL(グラフQL)はAPI向けに作られたクエリ言語およびランタイムである[2]。 ベンダーフリーな GraphQL財団の支援を受け、開発が進められている[3]

GraphQL
開発元 Facebookとコミュニティ
初版 2015年 (2015)
最新版

2018年6月[1]

リポジトリ github.com/graphql/graphql-spec
プログラミング
言語
JavaJavaScriptRubyScalaなど
対応OS クロスプラットフォーム
ライセンス 3条項BSDライセンス
公式サイト graphql.org ウィキデータを編集
テンプレートを表示

APIRESTWebGraphQLAPI[4][5][6]GraphQL

GraphQL (query) (mutation) (subscription) [7]

概要

編集

GraphQLdocumentGraphQLdocumentDSLGraphQL query language[8]documentGraphQLJSON
// document
{
  tomorrow {
    weather
    rainyPercent
  }
}
{
  "tomorrow": {
    "weather": "cloudy",
    "rainyPercent": 30
  }
}

URLRESTful APIGraphQLdocumentGraphQL WebAPIAPIdocumentPOST:https://API.internal./graphqldocumentBodyPOST: # 

DocumentGraphQLDocumentfield (: tomorrow) 1resolverfieldDocumentresolverfield1GraphQLfieldresolverresolver

RESTful APIGraphQLdocumentresolverRESTful API1GraphQLschema-valid1RESTful APIAPI1APIGraphQLPersion.friends fieldresolver{personA {friends {friends {name} } } }DocumentPOSTresolver1

GraphQL1

GraphQLIDL"type system definition language" (schema definition language) Schema

GraphQLAPIGraphQL WebAPIdocumentURL:GET https://API.internal./graphql?query={tomorrow{weather}}[9]

機能

編集

Arguments

編集

GraphQLargumentGraphQLfield: tomorrow0arguments[10]GraphQLfieldresolverargumentsresolverresolver[11]"/name" argument"/weather" argument-/tokyo/sunny
// document
{
  prefecture(name: "Tokyo") {
    prefName
    cities(weather: "sunny") {
      cityName
      rainyPercent
}}}
{
  "prefecture": {
    {
      "prefName": "Tokyo",
      "cities": [
        {
          "cityName": "Shinjuku",
          "rainyPercent": 10
        },{
          "cityName": "Ikebukuro",
          "rainyPercent": 0
}]}}}

directives

編集

GraphQLdirectivesGraphQLfield: tomorrowdirective[12]directives[13]directivefield@include(if: Boolean)
{
  sinjuku {
    weather
  }
  ikebukuro @include(if: false) {
    weather
    rainyPercent
}}
{
  "sinjuku": {
    "weather": "cloudy"
}}

resolver@includedirectivesresolverdirective1

directivesGraphQLAWS AmplifySchemadirectives"GraphQL Transform"[14]@modelfieldresolverDynamoDBGraphQLAmplify 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は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  }  }

処理系

編集

値解決

編集

Value ResolutionField[15]ResolveFieldValue[, , field, arguments]field: Person, Obama, job fieldName, no argument => presidentfield value)JavaScript
function ResolveFieldValue(objectType, objectValue, fieldName, argumentValues){
  const resolver = getExternalResolver(ojectType, fieldName);
  return resolver(objectValue, argumentValues);
}

歴史

編集

GraphQL2012Facebook2015[16]2018117GraphQLFacebookLinux FoundationGraphQL Foundation[17][18]2012GraphQLGraphQLLee Byron[19] ByronGraphQLWeb

201829GraphQL Schema Definition LanguageSDL[20]

実装

編集

クライアント

編集

GraphQLクライアントは適切なdocumentをGraphQL APIエンドポイントへPOSTするだけでクエリを実行できるため、ライブラリを採用せずとも容易にクエリを実行できる[21]

より高度な機能を有するGraphQLクライアント実装としては、Apollo Client[22]とRelay[23]がある。

サービス/サーバー

編集

GraphQLHaskellJavaScript[24]Perl[25]Python[26]RubyJavaC#ScalaGoElixir[27]ErlangPHPRClojure

利用例

編集

GitHub API

編集

GitHub4APIGraphQL APIGitHub GraphQL API v4[28]

GitHubmore flexibility for our integrators[29]GitHubIssueCI/CDAPIAPIREST API[30]GitHubREST APIGraphQLAPI

関連項目

編集

出典

編集


(一)^ GraphQL June 2018 Release Notes. 2019326

(二)^ GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. https://graphql.org/

(三)^ GraphQL foundation. https://graphql.org/foundation/

(四)^ GraphQL vs REST: Overview (). Phil Sturgeon. (2017124). https://phil.tech/api/2017/01/24/graphql-vs-rest-overview/ 20181125 

(五)^ Why use GraphQL, good and bad reasons (). Honest Engineering. (201884). https://honest.engineering/posts/why-use-graphql-good-and-bad-reasons 20181126 

(六)^ GraphQL Fundamentals. Howto GraphQL. 201874

(七)^ GraphQL. facebook.github.io. Facebook. 201874

(八)^ 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

(11)^ 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

(12)^ 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

(13)^ Directives provide a way to describe alternate runtime execution and type validation behavior in a GraphQL document GraphQL Specification

(14)^ 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

(15)^ This is exposed via ResolveFieldValue, which produces a value for a given field on a type for a real value. 6.4.2 Value Resolution

(16)^ GraphQL: A data query language. 20191019

(17)^ Facebooks GraphQL gets its own open-source foundation (). TechCrunch. https://techcrunch.com/2018/11/06/facebooks-graphql-gets-its-own-open-source-foundation/ 2018117 

(18)^ The Linux Foundation Announces Intent to Form New Foundation to Support GraphQL - The Linux Foundation (). The Linux Foundation. (2018116). https://www.linuxfoundation.org/press-release/2018/11/intent_to_form_graphql/ 2018117 

(19)^ Anthony. Is GraphQL Moving Toward Ubiquity?. NordicAPIs. 20191019

(20)^ [RFC GraphQL Schema Definition Language (SDL) by leebyron · Pull Request #90 · graphql/graphql-spec] (). GitHub. 20191019

(21)^ But you don't need a complex client to call a GraphQL server. With express-graphql, you can just send an HTTP POST request to the endpoint you mounted your GraphQL server on, passing the GraphQL query as the query field in a JSON payload. graphql.org

(22)^ Introduction. Apollo GraphQL Docs. 20191019

(23)^ Relay · A JavaScript framework for building data-driven React applications (). relay.dev. 20191019

(24)^ A reference implementation of GraphQL for JavaScript: graphql/graphql-js, GraphQL, (2019-10-19), https://github.com/graphql/graphql-js 20191019 

(25)^ GraphQL - Perl implementation of GraphQL - metacpan.org. metacpan.org. 20191019

(26)^ Graphene. graphene-python.org. 2017618

(27)^ Absinthe: The GraphQL toolkit for Elixir. 2018719

(28)^ GitHub chose GraphQL for our API v4 GitHub Developer

(29)^ because it offers significantly more flexibility for our integrators. GitHub Developer

(30)^ Despite all the information we provided, we heard from integrators that our REST API also wasnt very flexible. GitHub blog

外部リンク

編集