通过探索示例内容和查询,了解如何将 GraphQL 与 AEM 结合使用,以 Headless 方式提供内容。
本页面应该与下列内容一起阅读:
要开始了解 GraphQL 查询以及它们如何与 AEM 内容片段结合使用,看一些具体的示例会有所帮助。
有关这方面的帮助,请查看:
一个示例内容片段结构
以及一些示例 GraphQL 查询,基于示例内容片段结构(内容片段模型以及相关的内容片段)。
查看这些示例查询,以了解创建查询的说明以及示例结果。
根据您的实例,您可以直接访问 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"
]
}
}
}
}
如果您创建新的变体,命名为“柏林中心”(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"
]
}
]
}
}
}
使用嵌套片段的结构,此查询返回公司的 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 | 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 |