了解如何将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
实例然后,您可以使用查询返回 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
。查询以 [AllFragmentModels]
数组形式传递 fragments
,该数组通过联合类型解除引用。
{
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 | Max | Gameblitz |
Jobs | 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 |