GraphQLをAEMと共に使用して、サンプルコンテンツとクエリを参照し、コンテンツをヘッドレスに提供する方法を学びます。
このページと併せて、次の記事も参照してください。
GraphQLクエリとAEMコンテンツフラグメントの使用方法を学ぶには、実用的な例をいくつか見るのに役立ちます。
以下を参照してください。
サンプルコンテンツフラグメント構造(コンテンツフラグメントモデルと関連するコンテンツフラグメント)に基づくいくつかのサンプル GraphQL クエリ
クエリの作成とサンプル結果については、これらのサンプルクエリを参照してください。
インスタンスに応じて、 AEM GraphQL API に含まれる GraphiQL インターフェイス クエリの送信とテストに使用します。
例:http://localhost:4502/content/graphiql.html
サンプルクエリは、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
}
]
}
}
}
このサンプルクエリは、 name
の city
スキーマ。
サンプルクエリ
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"
]
}
}
}
}
「ベルリンセンター」という名前のバリエーション (berlin_centre
)、 city
ベルリンでは、クエリを使用してバリエーションの詳細を返すことができます。
サンプルクエリ
{
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"
]
}
]
}
}
}
次の場合:
Tourism
: Business
, City Break
, Holiday
City
インスタンスこの場合、クエリを使用して、city
スキーマで都市滞在型休暇としてタグ付けされたすべてのエントリの name
および tags
の詳細を返すことができます。
サンプルクエリ
query {
cityList(
includeVariations: true,
filter: {_tags: {_expressions: [{value: "tourism:city-break", _operator: CONTAINS}]}}
){
items {
name,
_tags
}
}
}
サンプル結果
{
"data": {
"cityList": {
"items": [
{
"name": "Berlin",
"_tags": [
"tourism:city-break",
"tourism:business"
]
},
{
"name": "Zurich",
"_tags": [
"tourism:city-break",
"tourism:business"
]
}
]
}
}
}
このクエリでは、ネストされたフラグメントの構造を使用して、ある会社の 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": []
}
]
}
]
}
}
}
このサンプルクエリでは、 persons
名前の付いたものは Jobs
または Smith
.
サンプルクエリ
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"
}
]
}
}
}
このサンプルクエリでは、 persons
名前の付いたものは Jobs
または 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"
}
]
}
}
}
_path
が特定のプレフィックスで始まるすべてのアドベンチャー_path
が特定のプレフィックス(/content/dam/wknd/en/adventures/cycling
)で始まるすべての adventures
。
サンプルクエリ
query {
adventureList(
filter: {
_path: {
_expressions: [
{
value: "/content/dam/wknd/en/adventures/cycling"
_operator: STARTS_WITH
}]
}
})
{
items {
_path
}
}
}
サンプル結果
{
"data": {
"adventureList": {
"items": [
{
"_path": "/content/dam/wknd/en/adventures/cycling-southern-utah/cycling-southern-utah"
},
{
"_path": "/content/dam/wknd/en/adventures/cycling-tuscany/cycling-tuscany"
}
]
}
}
}
ここでは、フィールドの組み合わせがフィルターされます。 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 プロジェクトに基づいています。次のようになります。
次の URL で入手できるコンテンツフラグメントモデル:
http://<hostname>:<port>/libs/dam/cfm/models/console/content/models.html/conf/wknd
次の URL で入手できるコンテンツフラグメント(およびその他のコンテンツ):
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
タイプの 1 つのコンテンツフラグメントについて
サンプルクエリ
{
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
タイプの 1 つのコンテンツフラグメントについて
フィールド referencearticle
のデータタイプは fragment-reference
です。
サンプルクエリ
{
adventureByPath(_path: "/content/dam/wknd-shared/en/magazine/western-australia/western-australia-by-camper-van") {
item {
_path
title
_model {
_path
title
}
}
}
}
このクエリでは次のものを検索します。
bookmark
タイプの複数のコンテンツフラグメントについて
Article
フィールド fragments
はデータタイプです fragment-reference
(モデルを使用) Article
選択済み クエリ配信 fragments
配列として [Article]
.
{
bookmarkList {
items {
fragments {
_path
author
}
}
}
}
このクエリでは次のものを検索します。
bookmark
タイプの複数のコンテンツフラグメントについて
Article
および Adventure
の他のフラグメントへのフラグメント参照を含むものフィールド fragments
のデータタイプは fragment-reference
で、モデル Article
および Adventure
が選択されています。クエリ配信 fragments
配列として [AllFragmentModels]
:和集合タイプで非参照になります。
{
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
タイプの 1 つのコンテンツフラグメントについて
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
タイプの 1 つのコンテンツフラグメントについて
variation1
サンプルクエリ
{
articleByPath (_path: "/content/dam/wknd/en/magazine/alaska-adventure/alaskan-adventures", variation: "variation1") {
item {
_path
author
main {
html
markdown
plaintext
json
}
}
}
}
このクエリでは次のものを検索します。
variation1
を持つ article
タイプのコンテンツフラグメントサンプルクエリ
{
articleList (variation: "variation1") {
items {
_path
author
main {
html
markdown
plaintext
json
}
}
}
}
このクエリでは次のものを検索します。
article
タイプのコンテンツフラグメントとすべてのバリエーションサンプルクエリ
query {
articleList(
includeVariations: true ){
items {
_variation
_path
_tags
_metadata {
stringArrayMetadata {
name
value
}
}
}
}
}
このクエリでは次のものを検索します。
WKND : Activity / Hiking
の付いた 1 つ以上のバリエーションを持つ article
タイプのコンテンツフラグメントサンプルクエリ
{
articleList(
includeVariations: true,
filter: {_tags: {_expressions: [{value: "wknd:activity/hiking", _operator: CONTAINS}]}}
){
items {
_variation
_path
_tags
_metadata {
stringArrayMetadata {
name
value
}
}
}
}
}
このクエリでは次のものを検索します。
fr
ロケール内の article
タイプのコンテンツフラグメントサンプルクエリ
{
articleList (_locale: "fr") {
items {
_path
author
main {
html
markdown
plaintext
json
}
}
}
}
サンプルクエリは次の構造に基づいています。
1 つ以上のサンプルコンテンツフラグメントモデル - GraphQL スキーマの基盤となります
上記のモデルに基づくサンプルコンテンツフラグメント
サンプルクエリでは、次のコンテンツモデルとその相互関係(参照 —>)を使用します。
会社を定義する基本フィールドは次のとおりです。
フィールド名 | データタイプ | 参照 |
---|---|---|
会社名 | 1 行のテキスト | |
CEO | フラグメント参照(1 つ) | Person |
従業員数 | フラグメント参照(複数フィールド) | Person |
人物(従業員になることも可能)を定義するフィールドは次のとおりです。
フィールド名 | データタイプ | 参照 |
---|---|---|
name(氏名) | 1 行のテキスト | |
名 | 1 行のテキスト | |
授賞歴 | フラグメント参照(複数フィールド) | Award |
賞を定義するフィールドは次のとおりです。
フィールド名 | データタイプ | 参照 |
---|---|---|
ショートカット/ID | 1 行のテキスト | |
タイトル | 1 行のテキスト |
都市を定義するフィールドは次のとおりです。
フィールド名 | データタイプ | 参照 |
---|---|---|
name(氏名) | 1 行のテキスト | |
Country | 1 行のテキスト | |
人口 | 数値 | |
カテゴリ | タグ |
適切なモデルでは次のフラグメントが使用されます。
会社名 | CEO | 従業員数 |
---|---|---|
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 | 名 | 授賞歴 |
---|---|---|
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 | Gamestar |
OSC | Oscar |
name | 国名 | 人口 | カテゴリ |
---|---|---|---|
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 | USA | 102635 | city:na |
Stuttgart | Germany | 634830 | city:emea |
Zurich | Switzerland | 415367 | city:capital city:emea |