透過探索範例內容和查詢,了解如何搭配使用 GraphQL 與 AEM 以提供無周邊內容。
本頁應與以下主題一起閱讀:
若要開始使用 GraphQL 查詢,及了解它們如何與 AEM 內容片段搭配使用,查看一些實際範例會有所幫助。
如需相關幫助,請參閱:
以及一些範例 GraphQL 查詢,其根據範例內容片段結構 (內容片段模型和相關內容片段)。
請參閱這些範例查詢,以了解如何建立查詢以及範例結果。
視您的執行個體而定,您可以直接存取 ](/docs/experience-manager-cloud-service/headless/graphql-api/graphiql-ide.html?lang=zh-Hant)AEM GraphQL API 包含的 GraphiQL 介面[以提交和測試查詢。
您可以從以下任一方式存取查詢編輯器:
http://localhost:4502/aem/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
}
]
}
}
}
這是簡單查詢可傳回 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_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
或 Smith
的 persons
。
範例查詢
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
http://<hostname>:<port>/assets.html/content/dam/wknd-shared/en
由於結果可能很龐大,因此不在此處重現。
此範例查詢會質詢:
article
的所有內容片段_path
和 authorFragment
的屬性。範例查詢
{
articleList {
items {
_path
authorFragment {
_path
firstName
lastName
birthDay
}
}
}
}
此查詢會質詢:
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-shared/en/magazine/alaska-adventure/alaskan-adventures") {
item {
_path
authorFragment {
_path
firstName
lastName
birthDay
}
main {
html
markdown
plaintext
json
}
}
}
}
此範例查詢會質詢:
範例查詢
{
adventureByPath(_path: "/content/dam/wknd-shared/en/magazine/western-australia/western-australia-by-camper-van") {
item {
_path
title
_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
和 adventure
的其他片段。fragments
欄位的資料類型為 fragment-reference
,並選擇了模型 Article
、Adventure
。
{
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
}
}
}
}
此查詢會質詢:
author
的單一內容片段
another
範例查詢
{
authorByPath(_path: "/content/dam/wknd-shared/en/contributors/ian-provo", variation: "another") {
item {
_path
_variation
firstName
lastName
birthDay
}
}
}
此查詢會質詢:
author
的內容片段,具有特定變化:another
這將針對沒有所指定名稱之變化的內容片段,示範如何備援。
範例查詢
{
authorList(variation: "another") {
items {
_path
_variation
firstName
lastName
birthDay
}
}
}
此查詢會質詢:
fr
地區設定中類型為 article
的內容片段範例查詢
{
articleList(_locale: "fr") {
items {
_path
authorFragment {
_path
firstName
lastName
birthDay
}
main {
html
markdown
plaintext
json
}
}
}
}
此查詢會質詢:
範例查詢
{
articleList(offset: 5, limit: 5) {
items {
authorFragment {
_path
firstName
lastName
birthDay
}
_path
}
}
}
此查詢會質詢:
範例查詢
{
adventurePaginated(first: 5, after: "ODg1MmMyMmEtZTAzMy00MTNjLThiMzMtZGQyMzY5ZTNjN2M1") {
edges {
cursor
node {
title
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
這些範例查詢是根據以下結構,該結構使用:
對於範例查詢,我們將使用以下內容模型及其相互關係 (參考 ->):
定義公司的基本欄位是:
欄位名稱 | 資料類型 | 參考 |
---|---|---|
公司名稱 | 單行文字 | |
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 |