瞭解如何搭配AEM使用GraphQL,透過探索範例內容和查詢來提供Headless內容。
本頁應與以下主題一起閱讀:
若要開始使用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
執行個體然後,您可以使用查詢來傳回 name
和 tags
中所有標示為「城市分隔符號」的專案 city
結構描述。
範例查詢
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"
}
]
}
}
}
此查詢篩選至少出現一次項目 (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"
]
}
]
}
}
}
此查詢說明篩選出任何 name
為「Smith」的 person
,從兩個巢狀片段 - 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"
}
]
}
]
}
}
}
此查詢說明了跨三個巢狀片段 - 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"
}
]
}
]
}
]
}
}
}
此查詢說明了跨三個巢狀片段 - 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
。
範例查詢
{
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
}
}
}
}
}
此查詢有兩種形式:
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
}
}
}
}
此查詢會質詢:
article
和所有變數範例查詢
query {
articleList(
includeVariations: true ){
items {
_variation
_path
_tags
_metadata {
stringArrayMetadata {
name
value
}
}
}
}
}
此查詢會質詢:
article
一或多個具有標籤的變數 WKND : Activity / Hiking
範例查詢
{
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
}
}
}
}
這些範例查詢是根據以下結構,該結構使用:
對於範例查詢,請使用以下內容模型及其相互關係(參照 — >):
定義公司的基本欄位是:
欄位名稱 | 資料類型 | 參考 |
---|---|---|
公司名稱 | 單行文字 | |
CEO | 片段參考 (單一) | 人員 |
員工 | 片段參考 (多個欄位) | 人員 |
定義人員的欄位,人員也可以是員工:
欄位名稱 | 資料類型 | 參考 |
---|---|---|
名稱 | 單行文字 | |
名字 | 單行文字 | |
獎項 | 片段參考 (多個欄位) | 獎項 |
定義獎項的欄位是:
欄位名稱 | 資料類型 | 參考 |
---|---|---|
快速鍵/ID | 單行文字 | |
標題 | 單行文字 |
定義城市的名稱是:
欄位名稱 | 資料類型 | 參考 |
---|---|---|
名稱 | 單行文字 | |
國家/地區 | 單行文字 | |
人口 | 數字 | |
類別 | 標記 |
以下片段用於適當的模型。
公司名稱 | CEO | 員工 |
---|---|---|
Apple | Steve Jobs | Duke Marsh Max Caulfield |
Little Pony Inc. | Adam Smith | Lara Croft Cutter Slade |
NextStep Inc. | Steve Jobs | Joe Smith Abe Lincoln |
名稱 | 名字 | 獎項 |
---|---|---|
Lincoln | Abe | |
Smith | Adam | |
Slade | Cutter | Gameblitz Gamestar |
Marsh | Duke | |
Smith | Joe | |
Croft | Lara | Gamestar |
Caulfield | 最大值 | Gameblitz |
工作 | Steve |
快速鍵/ID | 標題 |
---|---|
GB | Gameblitz |
GS | Gamestar |
OSC | Oscar |
名稱 | 國家/地區 | 人口 | 類別 |
---|---|---|---|
巴塞爾 | 瑞士 | 172258 | city:emea |
柏林 | 德國 | 3669491 | city:capital city:emea |
布加勒斯特 | 羅馬尼亞 | 1821000 | city:capital city:emea |
舊金山 | 美國 | 883306 | city:beach city:na |
聖荷西 | 美國 | 102635 | city:na |
斯圖加特 | 德國 | 634830 | city:emea |
蘇黎世 | 瑞士 | 415367 | city:capital city:emea |