おはこんばんちは、海外旅行に行きたいちーずです。
GraphQLを「超なんとなく」で使っていたため、基本文法まとめようと思ったら
いつの間にかクイズになってました。
GraphQLをはじめてみたい!という人はぜひチャレンジしてみてください!!
ルール
今回のクイズでは、publicで公開されている「国API」を使って、
- schemaの読み方
- GraphQLの基本的なクエリの書き方
を学んでいきます。
このAPIでは、大陸 - continent
/ 国 - country
/ 言語 - language
の取得ができます。
▼ 今回使う国APIのgithub
下記のplaygroundを使って、実際にクエリを実行してみて答えに辿り着けるかチャレンジしてみてください!
▼ playground
schema (クリックすると見れるよ)
directive @cacheControl(
maxAge: Int
scope: CacheControlScope
) on FIELD_DEFINITION | OBJECT | INTERFACE
enum CacheControlScope {
PUBLIC
PRIVATE
}
type Continent {
code: ID!
name: String!
countries: [Country!]!
}
input ContinentFilterInput {
code: StringQueryOperatorInput
}
type Country {
code: ID!
name: String!
native: String!
phone: String!
continent: Continent!
capital: String
currency: String
languages: [Language!]!
emoji: String!
emojiU: String!
states: [State!]!
}
input CountryFilterInput {
code: StringQueryOperatorInput
currency: StringQueryOperatorInput
continent: StringQueryOperatorInput
}
type Language {
code: ID!
name: String
native: String
rtl: Boolean!
}
input LanguageFilterInput {
code: StringQueryOperatorInput
}
type Query {
continents(filter: ContinentFilterInput): [Continent!]!
continent(code: ID!): Continent
countries(filter: CountryFilterInput): [Country!]!
country(code: ID!): Country
languages(filter: LanguageFilterInput): [Language!]!
language(code: ID!): Language
}
type State {
code: String
name: String!
country: Country!
}
input StringQueryOperatorInput {
eq: String
ne: String
in: [String]
nin: [String]
regex: String
glob: String
}
scalar Upload
第一問: 世界のすべての大陸名を答えよ
アジア大陸 / ヨーロッパ大陸 / アフリカ大陸 / オーストラリア大陸 / 北アメリカ大陸 / 南アメリカ大陸 / 南極大陸 の七つでした!! ちなみに大陸の分け方は諸説あるみたいです。答え
日本の社会の授業では「六大陸」だったような...?query {
continents {
name
}
}
ルート型には、下記の3種類があります。 ルートクエリ型は、 今回は「データの取得」をしたいため、「type Query」を探します。 これが、叩けるクエリです。 GraphQLのschemaのフィールドには大きく分けてスカラー型とオブジェクト型があります。 フィールドの集合体をオブジェクト型と言います。 このように、型の後ろに query{} の中にフィールドを列挙していくだけです。解説: schemaの基本的な型 / queryの書き方
schemaの見方
1. ルート型を見つける
schema
にて定義しなおすこともできます。schema {
query: query_root
mutation: mutation_root
subscription: subscription_root
}
type Query {
continents(filter: ContinentFilterInput): [Continent!]!
continent(code: ID!): Continent
countries(filter: CountryFilterInput): [Country!]!
country(code: ID!): Country
languages(filter: LanguageFilterInput): [Language!]!
language(code: ID!): Language
}
2. フィールドの型
スカラー型
String :文字列型
Int :整数型
Float :浮動小数点型
Boolean :論理型(true / false)
ID :ID型(ユニークな文字列)
オブジェクト型
下記のように、定義したtypeをそのまま使います。type User {
code: ID!
name: String
}
Query {
user: User // これがオブジェクト型
}
!
is 何type User {
code: ID!
name: String
}
!
があるケースがありますが、
これは「nullを許容しない」ことを意味します。
クエリの書き方
queryごとにDBへのお問い合わせがいくため、なるべくqueryは分割しないほうが良いです。query {
フィールド名
}
第二問: カナダの首都は?
ヒント: カナダのcodeはCA
です
「Ottawa(オタワ)」でした!答え
バンクーバーじゃないんですね!!笑query {
country(code: "CA") {
capital
}
}
第三問: ポンド(GBP)を使われている国は?
案外あるんや!!と思ったのですが、ほぼほぼイギリス周りの島国です答え
query {
countries (filter: {currency: {eq: "GBP"}}) {
name
}
}
第三問の拡張なので、説明は割愛させていただきます。
第四問: 日本とアメリカの「名前」「絵文字」「大陸名」は?
ヒント: 日本のcodeはJP
/ アメリカのコードはUS
答え
fragment details on Country {
name
emoji
ctinent {
name
}
}
query {
Japan: country(code: "JP") {
...details
}
America: country(code: "US") {
...details
}
}
参考