このページは、次の内容と共に読む必要があります。
GraphQL クエリの基本と、AEM コンテンツフラグメントとの連携方法を学ぶには、いくつかの実践的な例が参考になります。
以下を参照してください。
サンプルコンテンツフラグメント構造(コンテンツフラグメントモデルと関連するコンテンツフラグメント)に基づくいくつかのサンプル GraphQL クエリ
AEM 用の GraphQL でのクエリの基本操作は、標準の GraphQL 仕様に従います。AEM での GraphQL クエリには、次のような拡張機能があります。
結果が 1 つだけ必要な場合:
結果のリストを想定している場合:
List
を追加;例:cityList
論理和(OR)を使用する場合:
_logOp: OR
を使用します論理積(AND)も存在しますが、(多くの場合)暗黙的です
コンテンツフラグメントモデル内のフィールドに対応するフィールド名に対してクエリを実行できます
モデルのフィールドに加えて、システム生成フィールドがいくつかあります(前にアンダースコアが付いています)。
コンテンツの場合:
_locale
:言語を表示します(言語マネージャーに基づく)
_metadata
:フラグメントのメタデータを表示します
_model
:コンテンツフラグメントモデルのクエリを許可(パスとタイトル)
_path
:リポジトリ内のコンテンツフラグメントへのパス
_reference
:参照(リッチテキストエディターでのインライン参照など)を表示します
_variation
:コンテンツフラグメント内の特定のバリエーションを表示します
操作の場合:
_operator
:特定の演算子を適用する; EQUALS
, EQUALS_NOT
, GREATER_EQUAL
, LOWER
, CONTAINS
_apply
:特定の条件(例:AT_LEAST_ONCE
)を適用します
_ignoreCase
:クエリの実行時に大文字と小文字を区別しません
GraphQL のユニオン型がサポートされています
... on
を使用します
作成クエリの図とサンプル結果については、以下のサンプルクエリを参照してください。
インスタンスによっては、AEM GraphQL API に付属している Graph i QL インターフェイスに直接アクセスして、クエリの送信とテストをおこなうことができます。
例:http://localhost:4502/content/graphiql.html
サンプルクエリは、GraphQL](#content-fragment-structure-graphql)で使用する[サンプルコンテンツフラグメント構造に基づいています
これは、使用可能なすべてのスキーマのすべてのtypes
を返します。
サンプルクエリ
{
__schema {
types {
name
description
}
}
}
サンプル結果
{
"data": {
"__schema": {
"types": [
{
"name": "AdventureModel",
"description": null
},
{
"name": "AdventureModelArrayFilter",
"description": null
},
{
"name": "AdventureModelFilter",
"description": null
},
{
"name": "AdventureModelResult",
"description": null
},
{
"name": "AdventureModelResults",
"description": null
},
{
"name": "AllFragmentModels",
"description": null
},
{
"name": "ArchiveRef",
"description": null
},
{
"name": "ArrayMode",
"description": null
},
{
"name": "ArticleModel",
"description": null
},
...more results...
{
"name": "__EnumValue",
"description": null
},
{
"name": "__Field",
"description": null
},
{
"name": "__InputValue",
"description": null
},
{
"name": "__Schema",
"description": "A GraphQL Introspection defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, the entry points for query, mutation, and subscription operations."
},
{
"name": "__Type",
"description": null
},
{
"name": "__TypeKind",
"description": "An enum describing what kind of type a given __Type is"
}
]
}
}
}
すべての都市に関するすべての情報を取得するには、次のような非常に基本的なクエリを使用します。
サンプルクエリ
{
cityList {
items
}
}
実行時にクエリが自動的に展開されて、次のように、すべてのフィールドが組み込まれます。
{
cityList {
items {
_path
name
country
population
}
}
}
サンプル結果
{
"data": {
"cityList": {
"items": [
{
"_path": "/content/dam/sample-content-fragments/cities/basel",
"name": "Basel",
"country": "Switzerland",
"population": 172258
},
{
"_path": "/content/dam/sample-content-fragments/cities/berlin",
"name": "Berlin",
"country": "Germany",
"population": 3669491
},
{
"_path": "/content/dam/sample-content-fragments/cities/bucharest",
"name": "Bucharest",
"country": "Romania",
"population": 1821000
},
{
"_path": "/content/dam/sample-content-fragments/cities/san-francisco",
"name": "San Francisco",
"country": "USA",
"population": 883306
},
{
"_path": "/content/dam/sample-content-fragments/cities/san-jose",
"name": "San Jose",
"country": "USA",
"population": 1026350
},
{
"_path": "/content/dam/sample-content-fragments/cities/stuttgart",
"name": "Stuttgart",
"country": "Germany",
"population": 634830
},
{
"_path": "/content/dam/sample-content-fragments/cities/zurich",
"name": "Zurich",
"country": "Switzerland",
"population": 415367
}
]
}
}
}
これは、city
スキーマ内のすべてのエントリの name
を返す単純なクエリです。
サンプルクエリ
query {
cityList {
items {
name
}
}
}
サンプル結果
{
"data": {
"cityList": {
"items": [
{
"name": "Basel"
},
{
"name": "Berlin"
},
{
"name": "Bucharest"
},
{
"name": "San Francisco"
},
{
"name": "San Jose"
},
{
"name": "Stuttgart"
},
{
"name": "Zurich"
}
]
}
}
}
これは、リポジトリ内の特定の場所にある単一のフラグメントエントリの詳細を返すクエリです。
サンプルクエリ
{
cityByPath (_path: "/content/dam/sample-content-fragments/cities/berlin") {
item {
_path
name
country
population
categories
}
}
}
サンプル結果
{
"data": {
"cityByPath": {
"item": {
"_path": "/content/dam/sample-content-fragments/cities/berlin",
"name": "Berlin",
"country": "Germany",
"population": 3669491,
"categories": [
"city:capital",
"city:emea"
]
}
}
}
}
city
Berlin の新しいバリエーションを「Berlin Center」(berlin_centre
)という名前で作成する場合は、クエリを使用してバリエーションの詳細を返すことができます。
サンプルクエリ
{
cityList (variation: "berlin_center") {
items {
_path
name
country
population
categories
}
}
}
サンプル結果
{
"data": {
"cityList": {
"items": [
{
"_path": "/content/dam/sample-content-fragments/cities/berlin",
"name": "Berlin",
"country": "Germany",
"population": 3669491,
"categories": [
"city:capital",
"city:emea"
]
}
]
}
}
}
このクエリでは、ネストされたフラグメントの構造を使用して、ある会社の CEO とすべての従業員の詳細を返します。
サンプルクエリ
query {
companyList {
items {
name
ceo {
_path
name
firstName
awards {
id
title
}
}
employees {
name
firstName
awards {
id
title
}
}
}
}
}
サンプル結果
{
"data": {
"companyList": {
"items": [
{
"name": "Apple Inc.",
"ceo": {
"_path": "/content/dam/sample-content-fragments/persons/steve-jobs",
"name": "Jobs",
"firstName": "Steve",
"awards": []
},
"employees": [
{
"name": "Marsh",
"firstName": "Duke",
"awards": []
},
{
"name": "Caulfield",
"firstName": "Max",
"awards": [
{
"id": "GB",
"title": "Gameblitz"
}
]
}
]
},
{
"name": "Little Pony, Inc.",
"ceo": {
"_path": "/content/dam/sample-content-fragments/persons/adam-smith",
"name": "Smith",
"firstName": "Adam",
"awards": []
},
"employees": [
{
"name": "Croft",
"firstName": "Lara",
"awards": [
{
"id": "GS",
"title": "Gamestar"
}
]
},
{
"name": "Slade",
"firstName": "Cutter",
"awards": [
{
"id": "GB",
"title": "Gameblitz"
},
{
"id": "GS",
"title": "Gamestar"
}
]
}
]
},
{
"name": "NextStep Inc.",
"ceo": {
"_path": "/content/dam/sample-content-fragments/persons/steve-jobs",
"name": "Jobs",
"firstName": "Steve",
"awards": []
},
"employees": [
{
"name": "Smith",
"firstName": "Joe",
"awards": []
},
{
"name": "Lincoln",
"firstName": "Abraham",
"awards": []
}
]
}
]
}
}
}
Jobs
または Smith
という名前を持つすべての persons
が抜き出されます。
サンプルクエリ
query {
personList(filter: {
name: {
_logOp: OR
_expressions: [
{
value: "Jobs"
},
{
value: "Smith"
}
]
}
}) {
items {
name
firstName
}
}
}
サンプル結果
{
"data": {
"personList": {
"items": [
{
"name": "Smith",
"firstName": "Adam"
},
{
"name": "Smith",
"firstName": "Joe"
},
{
"name": "Jobs",
"firstName": "Steve"
}
]
}
}
}
Jobs
という名前を持たないすべての persons
(Smith
など)が抜き出されます。
サンプルクエリ
query {
personList(filter: {
name: {
_expressions: [
{
value: "Jobs"
_operator: EQUALS_NOT
}
]
}
}) {
items {
name
firstName
}
}
}
サンプル結果
{
"data": {
"personList": {
"items": [
{
"name": "Lincoln",
"firstName": "Abraham"
},
{
"name": "Smith",
"firstName": "Adam"
},
{
"name": "Slade",
"firstName": "Cutter"
},
{
"name": "Marsh",
"firstName": "Duke"
},
{
"name": "Smith",
"firstName": "Joe"
},
{
"name": "Croft",
"firstName": "Lara"
},
{
"name": "Caulfield",
"firstName": "Max"
}
]
}
}
}
ここでは、フィールドの組み合わせに基づいてフィルタリングされます。AND
(暗黙的)を使用して population
の範囲を選択しつつ、OR
(明示的)を使用して必要な都市を選択しています。
サンプルクエリ
query {
cityList(filter: {
population: {
_expressions: [
{
value: 400000
_operator: GREATER_EQUAL
}, {
value: 1000000
_operator: LOWER
}
]
},
country: {
_logOp: OR
_expressions: [
{
value: "Germany"
}, {
value: "Switzerland"
}
]
}
}) {
items {
name
population
country
}
}
}
サンプル結果
{
"data": {
"cityList": {
"items": [
{
"name": "Stuttgart",
"population": 634830,
"country": "Germany"
},
{
"name": "Zurich",
"population": 415367,
"country": "Switzerland"
}
]
}
}
}
このクエリでは、名前に SAN
が含まれるすべての都市を、大文字と小文字を区別せずに検索します。
サンプルクエリ
query {
cityList(filter: {
name: {
_expressions: [
{
value: "SAN"
_operator: CONTAINS
_ignoreCase: true
}
]
}
}) {
items {
name
population
country
}
}
}
サンプル結果
{
"data": {
"cityList": {
"items": [
{
"name": "San Francisco",
"population": 883306,
"country": "USA"
},
{
"name": "San Jose",
"population": 1026350,
"country": "USA"
}
]
}
}
}
このクエリでは、少なくとも 1 回は現れる項目(city:na
)を含んだ配列を抜き出します。
サンプルクエリ
query {
cityList(filter: {
categories: {
_expressions: [
{
value: "city:na"
_apply: AT_LEAST_ONCE
}
]
}
}) {
items {
name
population
country
categories
}
}
}
サンプル結果
{
"data": {
"cityList": {
"items": [
{
"name": "San Francisco",
"population": 883306,
"country": "USA",
"categories": [
"city:beach",
"city:na"
]
},
{
"name": "San Jose",
"population": 1026350,
"country": "USA",
"categories": [
"city:na"
]
}
]
}
}
}
このクエリでは、配列値が正確に一致するものだけを抜き出します。
サンプルクエリ
query {
cityList(filter: {
categories: {
_expressions: [
{
values: [
"city:beach",
"city:na"
]
}
]
}
}) {
items {
name
population
country
categories
}
}
}
サンプル結果
{
"data": {
"cityList": {
"items": [
{
"name": "San Francisco",
"population": 883306,
"country": "USA",
"categories": [
"city:beach",
"city:na"
]
}
]
}
}
}
このクエリでは、「Smith」という name
の person
をフィルタリングし、ネストされた 2 つのフラグメント(company
と employee
)から取得した情報を返します。
サンプルクエリ
query {
companyList(filter: {
employees: {
_match: {
name: {
_expressions: [
{
value: "Smith"
}
]
}
}
}
}) {
items {
name
ceo {
name
firstName
}
employees {
name
firstName
}
}
}
}
サンプル結果
{
"data": {
"companyList": {
"items": [
{
"name": "NextStep Inc.",
"ceo": {
"name": "Jobs",
"firstName": "Steve"
},
"employees": [
{
"name": "Smith",
"firstName": "Joe"
},
{
"name": "Lincoln",
"firstName": "Abraham"
}
]
}
]
}
}
}
このクエリは、ネストされた 3 つのフラグメント(company
、employee
、award
)にわたるフィルタリングの例を示しています。
サンプルクエリ
query {
companyList(filter: {
employees: {
_apply: ALL
_match: {
awards: {
_match: {
id: {
_expressions: [
{
value: "GS"
_operator:EQUALS
}
]
}
}
}
}
}
}) {
items {
name
ceo {
name
firstName
}
employees {
name
firstName
awards {
id
title
}
}
}
}
}
サンプル結果
{
"data": {
"companyList": {
"items": [
{
"name": "Little Pony, Inc.",
"ceo": {
"name": "Smith",
"firstName": "Adam"
},
"employees": [
{
"name": "Croft",
"firstName": "Lara",
"awards": [
{
"id": "GS",
"title": "Gamestar"
}
]
},
{
"name": "Slade",
"firstName": "Cutter",
"awards": [
{
"id": "GB",
"title": "Gameblitz"
},
{
"id": "GS",
"title": "Gamestar"
}
]
}
]
}
]
}
}
}
このクエリは、ネストされた 3 つのフラグメント(company
、employee
、award
)にわたるフィルタリングの例を示しています。
サンプルクエリ
query {
awardList(filter: {
id: {
_expressions: [
{
value:"GB"
}
]
}
}) {
items {
_metadata {
stringMetadata {
name,
value
}
}
id
title
}
}
}
サンプル結果
{
"data": {
"awardList": {
"items": [
{
"_metadata": {
"stringMetadata": [
{
"name": "title",
"value": "Gameblitz Award"
},
{
"name": "description",
"value": ""
}
]
},
"id": "GB",
"title": "Gameblitz"
}
]
}
}
}
これらのサンプルクエリは WKND プロジェクトに基づいています。これには次のものがあります。
コンテンツフラグメントモデルは次の場所で利用できます。
http://<hostname>:<port>/libs/dam/cfm/models/console/content/models.html/conf/wknd
次の場所で利用可能なコンテンツフラグメント(およびその他のコンテンツ)
http://<hostname>:<port>/assets.html/content/dam/wknd/en
結果は膨大な量になる可能性があるので、ここでは再現されていません。
このサンプルクエリでは次のものを検索します。
article
タイプのすべてのコンテンツフラグメントについてpath
および author
プロパティを持つもの。サンプルクエリ
{
articleList {
items {
_path
author
}
}
}
このクエリでは次のものを検索します。
adventure
タイプのすべてのコンテンツフラグメントについてサンプルクエリ
{
adventureList {
items {
_path,
_metadata {
stringMetadata {
name,
value
}
stringArrayMetadata {
name,
value
}
intMetadata {
name,
value
}
intArrayMetadata {
name,
value
}
floatMetadata {
name,
value
}
floatArrayMetadata {
name,
value
}
booleanMetadata {
name,
value
}
booleanArrayMetadata {
name,
value
}
calendarMetadata {
name,
value
}
calendarArrayMetadata {
name,
value
}
}
}
}
}
このサンプルクエリでは次のものを検索します。
article
の単一のコンテンツフラグメント
サンプルクエリ
{
articleByPath (_path: "/content/dam/wknd/en/magazine/alaska-adventure/alaskan-adventures") {
item {
_path
author
main {
html
markdown
plaintext
json
}
}
}
}
このサンプルクエリでは次のものを検索します。
サンプルクエリ
{
adventureByPath(_path: "/content/dam/wknd/en/adventures/riverside-camping-australia/riverside-camping-australia") {
item {
_path
adventureTitle
_model {
_path
title
}
}
}
}
このクエリでは次のものを検索します。
article
の単一のコンテンツフラグメント
フィールドreferencearticle
のデータ型はfragment-reference
です。
サンプルクエリ
{
articleByPath (_path: "/content/dam/wknd/en/magazine/skitouring/skitouring") {
item {
_path
author
referencearticle {
_path
author
}
}
}
}
このクエリでは次のものを検索します。
bookmark
の複数のコンテンツフラグメント
article
およびadventure
の他のフラグメントへのフラグメント参照と共に使用します。フィールドfragments
のデータタイプはfragment-reference
で、モデルArticle
、Adventure
が選択されています。
{
bookmarkList {
items {
fragments {
... on ArticleModel {
_path
author
}
... on AdventureModel {
_path
adventureTitle
}
}
}
}
}
このクエリには2種類あります。
attachments
型の特定のコンテンツ参照を返す。次のクエリが問い合わされます。
bookmark
の複数のコンテンツフラグメント
次のクエリは、_references
を使用して、すべてのコンテンツ参照を返します。
{
bookmarkList {
_references {
... on ImageRef {
_path
type
height
}
... on MultimediaRef {
_path
type
size
}
... on DocumentRef {
_path
type
author
}
... on ArchiveRef {
_path
type
format
}
}
items {
_path
}
}
}
次のクエリは、すべてのattachments
— タイプcontent-reference
の特定のフィールド(サブグループ)を返します。
フィールドattachments
のデータ型はcontent-reference
で、様々なフォームが選択されています。
{
bookmarkList {
items {
attachments {
... on PageRef {
_path
type
}
... on ImageRef {
_path
width
}
... on MultimediaRef {
_path
size
}
... on DocumentRef {
_path
author
}
... on ArchiveRef {
_path
format
}
}
}
}
}
このクエリでは次のものを検索します。
bookmark
の単一のコンテンツフラグメント
RTEインライン参照は、_references
内で水和されます。
サンプルクエリ
{
bookmarkByPath(_path: "/content/dam/wknd/en/bookmarks/skitouring") {
item {
_path
description {
json
}
}
_references {
... on ArticleModel {
_path
}
... on AdventureModel {
_path
}
... on ImageRef {
_path
}
... on MultimediaRef {
_path
}
... on DocumentRef {
_path
}
... on ArchiveRef {
_path
}
}
}
}
このクエリでは次のものを検索します。
article
の単一のコンテンツフラグメント
variation1
サンプルクエリ
{
articleByPath (_path: "/content/dam/wknd/en/magazine/alaska-adventure/alaskan-adventures", variation: "variation1") {
item {
_path
author
main {
html
markdown
plaintext
json
}
}
}
}
このクエリでは次のものを検索します。
article
タイプのコンテンツフラグメントの場合:variation1
サンプルクエリ
{
articleList (variation: "variation1") {
items {
_path
author
main {
html
markdown
plaintext
json
}
}
}
}
このクエリでは次のものを検索します。
fr
ロケール内のタイプarticle
のコンテンツフラグメントサンプルクエリ
{
articleList (_locale: "fr") {
items {
_path
author
main {
html
markdown
plaintext
json
}
}
}
}
サンプルクエリは次の構造に基づいています。この構造では、次の値を使用します。
1 つ以上のサンプルコンテンツフラグメントモデル - GraphQL スキーマの基盤となります
上記のモデルに基づくサンプルコンテンツフラグメント
サンプルクエリでは、次のコンテンツモデルとその相互関係(参照関係「->」)を使用します。
会社を定義する基本フィールドは次のとおりです。
フィールド名 | データタイプ | 参照 |
---|---|---|
name(会社名) | 1 行のテキスト | |
ceo(最高経営責任者) | フラグメント参照(1 つ) | Person |
employees(従業員) | フラグメント参照(複数フィールド) | 人 |
人物(従業員になることも可能)を定義するフィールドは次のとおりです。
フィールド名 | データタイプ | 参照 |
---|---|---|
name(氏名) | 1 行のテキスト | |
firstName(名) | 1 行のテキスト | |
awards(受賞歴) | フラグメント参照(複数フィールド) | Award |
賞を定義するフィールドは次のとおりです。
フィールド名 | データタイプ | 参照 |
---|---|---|
id(賞の ID) | 1 行のテキスト | |
title(タイトル) | 1 行のテキスト |
都市を定義するフィールドは次のとおりです。
フィールド名 | データタイプ | 参照 |
---|---|---|
name(氏名) | 1 行のテキスト | |
country(国) | 1 行のテキスト | |
population(人口) | 数値 | |
categories(カテゴリ) | タグ |
適切なモデルでは次のフラグメントが使用されます。
name | ceo | employees |
---|---|---|
Apple Inc. | Steve Jobs | Duke Marsh Max Caulfield |
Little Pony, Inc. | Adam Smith | Lara Croft Cutter Slade |
NextStep Inc. | Steve Jobs | Joe Smith Abe Lincoln |
name | firstName | awards |
---|---|---|
Lincoln | Abe | |
Smith | Adam | |
Slade | Cutter | Gameblitz Gamestar |
Marsh | Duke | |
Smith | Joe | |
Croft | Lara | Gamestar |
Caulfield | Max | Gameblitz |
Jobs | Steve |
id | title |
---|---|
GB | Gameblitz |
GS | ゲームスター |
OSC | Oscar |
name | country | population | categories |
---|---|---|---|
Basel | Switzerland | 172258 | city:emea |
Berlin | Germany | 3669491 | city:capital city:emea |
Bucharest | Romania | 1821000 | city:capital city:emea |
San Francisco | USA | 883306 | city:beach city:na |
San Jose | 米国 | 102635 | city:na |
Stuttgart | Germany | 634830 | city:emea |
Zurich | Switzerland | 415367 | city:capital city:emea |