通过探索示例内容和查询,了解如何将 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"
]
}
}
}
}
如果您为 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"
]
}
]
}
}
}
如果您:
Tourism
: Business
, City Break
, Holiday
City
实例的主变体然后,您可以使用查询返回city
模式中标记为 City Breaks 的所有条目的 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": []
}
]
}
]
}
}
}
用于筛选所有名称为 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
的其他片段的片段引用字段 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
}
}
}
}
此查询查找:
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
}
}
}
此查询查找:
article
类型和所有变体的内容片段示例查询
query {
articleList(
includeVariations: true ){
items {
_variation
_path
_tags
_metadata {
stringArrayMetadata {
name
value
}
}
}
}
}
此查询查找:
WKND : Activity / Hiking
的变体的 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
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
}
}
}
此查询查找:
big-block
标签的 vehicle
类型的内容片段示例查询
query {
vehicleList(
filter: {
_tags: {
_expressions: [
{
value: "vehicles:big-block"
_operator: CONTAINS
}
]
}
}) {
items {
_variation
_path
type
name
model
fuel
_tags
}
}
}
此查询查找:
big-block
标签的 vehicle
类型的内容片段示例查询
{
enginePaginated(after: "SjkKNmVkODFmMGQtNTQyYy00NmQ4LTljMzktMjhlNzQwZTY1YWI2Cmo5", first: 9 ,includeVariations:true, sort: "name",
filter: {
_tags: {
_expressions: [
{
value: "vehicles:big-block"
_operator: CONTAINS
}
]
}
}) {
edges{
node {
_variation
_path
name
type
size
_tags
_metadata {
stringArrayMetadata {
name
value
}
}
}
cursor
}
}
}
示例查询基于以下结构,该结构使用:
对于相同的查询,使用以下内容模型及其相互关系(引用 ->):
定义公司的基本字段包括:
字段名 | 数据类型 | 引用 |
---|---|---|
公司名称 | 单行文本 | |
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 |