1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GraphQL: GraphQLをはじめる

Last updated at Posted at 2022-03-30

本稿はGraphQL公式サイト「Introduction to GraphQL」の中身を日本語で説明し直しました(邦訳ではありません。)。

GraphQLはAPIに対するクエリ言語です。サーバーサイドのランタイムで、データに定義した型システムを用いてクエリが実行できます。GraphQLは特定のデータベースやストレージエンジンには縛られません。すでにあるコードやデータで利用できるバックエンドです。

GraphQLサービスは、型とその型のフィールドを定義し、それぞれの型のフィールドに関数を備えてつくります。たとえば、誰がログインしているユーザー(me)なのか、またそのユーザーの名前を教えてくれるサービスはつぎのようなコードです。

GraphQL
type Query {
	me: User
}

type User {
	id: ID
	name: String
}

そして、フィールドと型には、つぎのような関数を備えます。

GraphQL
function Query_me(request) {
	return request.auth.user;
}

function User_name(user) {
	return user.getName();
}

GraphQLサービスが(通常はwebサービスのURLで)起動すると、クエリを受け取って検証と実行が行えます。サービスは、まずクエリが参照するのは定義された型とフィールドのみであるかを確かめ、つぎに提供された関数の実行により結果を生成します。

たとえば、つぎのコードがクエリから生成されるJSON結果の例です。

GraphQL
{
	me {
		name
	}
}
結果
{
	"me": {
		"name": "Luke Skywalker"
	}
}

つぎのステップとしては「GraphQL: クエリ(queries)と変更(mutations)」をお読みください。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?