Learn how to use GraphQL with AEM to serve content headlessly by exploring sample content and queries.
This page should be read together with:
To get started with GraphQL queries and how they work with AEM Content Fragments, it helps to see some practical examples.
To help with this see:
And some sample GraphQL queries, based on the sample content fragment structure (Content Fragment Models and related Content Fragments).
See these sample queries for illustrations of create queries, together with sample results.
Depending on your instance, you can directly access the GraphiQL interface included with AEM GraphQL API for submitting and testing queries.
For example: http://localhost:4502/content/graphiql.html
The sample queries are based on the Sample Content Fragment Structure for use with GraphQL
This sample query returns all types
for all available schemas.
Sample Query
{
__schema {
types {
name
description
}
}
}
Sample Result
{
"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"
}
]
}
}
}
To retrieve all information about all cities, you can use the basic query:
Sample Query
{
cityList {
items
}
}
When run, the system automatically expands the query to include all fields:
{
cityList {
items {
_path
name
country
population
}
}
}
Sample Results
{
"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
}
]
}
}
}
This sample query is a straightforward query to return the name
of all entries in the city
schema.
Sample Query
query {
cityList {
items {
name
}
}
}
Sample Results
{
"data": {
"cityList": {
"items": [
{
"name": "Basel"
},
{
"name": "Berlin"
},
{
"name": "Bucharest"
},
{
"name": "San Francisco"
},
{
"name": "San Jose"
},
{
"name": "Stuttgart"
},
{
"name": "Zurich"
}
]
}
}
}
This sample query is a query to return the details of a single fragment entry at a specific location in the repository.
Sample Query
{
cityByPath (_path: "/content/dam/sample-content-fragments/cities/berlin") {
item {
_path
name
country
population
categories
}
}
}
Sample Results
{
"data": {
"cityByPath": {
"item": {
"_path": "/content/dam/sample-content-fragments/cities/berlin",
"name": "Berlin",
"country": "Germany",
"population": 3669491,
"categories": [
"city:capital",
"city:emea"
]
}
}
}
}
If you create a variation named “Berlin Centre” (berlin_centre
), for the city
Berlin, you can use a query to return details of the variation.
Sample Query
{
cityList (variation: "berlin_center") {
items {
_path
name
country
population
categories
}
}
}
Sample Results
{
"data": {
"cityList": {
"items": [
{
"_path": "/content/dam/sample-content-fragments/cities/berlin",
"name": "Berlin",
"country": "Germany",
"population": 3669491,
"categories": [
"city:capital",
"city:emea"
]
}
]
}
}
}
If you:
Tourism
: Business
, City Break
, Holiday
City
instancesThen you can use a query to return details of the name
and tags
of all entries tagged as City Breaks in the city
schema.
Sample Query
query {
cityList(
includeVariations: true,
filter: {_tags: {_expressions: [{value: "tourism:city-break", _operator: CONTAINS}]}}
){
items {
name,
_tags
}
}
}
Sample Results
{
"data": {
"cityList": {
"items": [
{
"name": "Berlin",
"_tags": [
"tourism:city-break",
"tourism:business"
]
},
{
"name": "Zurich",
"_tags": [
"tourism:city-break",
"tourism:business"
]
}
]
}
}
}
Using the structure of the nested fragments, this query returns the full details of a company’s CEO and all its employees.
Sample Query
query {
companyList {
items {
name
ceo {
_path
name
firstName
awards {
id
title
}
}
employees {
name
firstName
awards {
id
title
}
}
}
}
}
Sample Results
{
"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": []
}
]
}
]
}
}
}
This sample query filters all persons
for any that have the name Jobs
or Smith
.
Sample Query
query {
personList(filter: {
name: {
_logOp: OR
_expressions: [
{
value: "Jobs"
},
{
value: "Smith"
}
]
}
}) {
items {
name
firstName
}
}
}
Sample Results
{
"data": {
"personList": {
"items": [
{
"name": "Smith",
"firstName": "Adam"
},
{
"name": "Smith",
"firstName": "Joe"
},
{
"name": "Jobs",
"firstName": "Steve"
}
]
}
}
}
This sample query filters all persons
for any that have the name Jobs
or Smith
.
Sample Query
query {
personList(filter: {
name: {
_expressions: [
{
value: "Jobs"
_operator: EQUALS_NOT
}
]
}
}) {
items {
name
firstName
}
}
}
Sample Results
{
"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
is starting with a specific prefixAll adventures
where _path
starts with a specific prefix (/content/dam/wknd/en/adventures/cycling
).
Sample Query
query {
adventureList(
filter: {
_path: {
_expressions: [
{
value: "/content/dam/wknd/en/adventures/cycling"
_operator: STARTS_WITH
}]
}
})
{
items {
_path
}
}
}
Sample Results
{
"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"
}
]
}
}
}
Here a combination of fields is filtered on. An AND
(implicit) is used to select the population
range, while an OR
(explicit) is used to select the required cities.
Sample Query
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
}
}
}
Sample Results
{
"data": {
"cityList": {
"items": [
{
"name": "Stuttgart",
"population": 634830,
"country": "Germany"
},
{
"name": "Zurich",
"population": 415367,
"country": "Switzerland"
}
]
}
}
}
This query interrogates for all cities that have SAN
in the name, irrespective of case.
Sample Query
query {
cityList(filter: {
name: {
_expressions: [
{
value: "SAN"
_operator: CONTAINS
_ignoreCase: true
}
]
}
}) {
items {
name
population
country
}
}
}
Sample Results
{
"data": {
"cityList": {
"items": [
{
"name": "San Francisco",
"population": 883306,
"country": "USA"
},
{
"name": "San Jose",
"population": 1026350,
"country": "USA"
}
]
}
}
}
This query filters on an array with an item (city:na
) that must occur at least once.
Sample Query
query {
cityList(filter: {
categories: {
_expressions: [
{
value: "city:na"
_apply: AT_LEAST_ONCE
}
]
}
}) {
items {
name
population
country
categories
}
}
}
Sample Results
{
"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"
]
}
]
}
}
}
This query filters on an exact array value.
Sample Query
query {
cityList(filter: {
categories: {
_expressions: [
{
values: [
"city:beach",
"city:na"
]
}
]
}
}) {
items {
name
population
country
categories
}
}
}
Sample Results
{
"data": {
"cityList": {
"items": [
{
"name": "San Francisco",
"population": 883306,
"country": "USA",
"categories": [
"city:beach",
"city:na"
]
}
]
}
}
}
This query illustrates filtering for any person
of name
“Smith”, returning information from across two nested fragments - company
and employee
.
Sample Query
query {
companyList(filter: {
employees: {
_match: {
name: {
_expressions: [
{
value: "Smith"
}
]
}
}
}
}) {
items {
name
ceo {
name
firstName
}
employees {
name
firstName
}
}
}
}
Sample Results
{
"data": {
"companyList": {
"items": [
{
"name": "NextStep Inc.",
"ceo": {
"name": "Jobs",
"firstName": "Steve"
},
"employees": [
{
"name": "Smith",
"firstName": "Joe"
},
{
"name": "Lincoln",
"firstName": "Abraham"
}
]
}
]
}
}
}
This query illustrates filtering across three nested fragments - company
, employee
, and award
.
Sample Query
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
}
}
}
}
}
Sample Results
{
"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"
}
]
}
]
}
]
}
}
}
This query illustrates filtering across three nested fragments - company
, employee
, and award
.
Sample Query
query {
awardList(filter: {
id: {
_expressions: [
{
value:"GB"
}
]
}
}) {
items {
_metadata {
stringMetadata {
name,
value
}
}
id
title
}
}
}
Sample Results
{
"data": {
"awardList": {
"items": [
{
"_metadata": {
"stringMetadata": [
{
"name": "title",
"value": "Gameblitz Award"
},
{
"name": "description",
"value": ""
}
]
},
"id": "GB",
"title": "Gameblitz"
}
]
}
}
}
These sample queries are based on the WKND project. It has the following:
Content Fragment Models available under:
http://<hostname>:<port>/libs/dam/cfm/models/console/content/models.html/conf/wknd
Content Fragments (and other content) available under:
http://<hostname>:<port>/assets.html/content/dam/wknd/en
As the results can be extensive, they are not reproduced here.
This sample query interrogates:
article
path
and author
properties.Sample Query
{
articleList {
items {
_path
author
}
}
}
This query interrogates:
adventure
Sample Query
{
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
}
}
}
}
}
This sample query interrogates:
article
at a specific path
Sample Query
{
articleByPath (_path: "/content/dam/wknd/en/magazine/alaska-adventure/alaskan-adventures") {
item {
_path
author
main {
html
markdown
plaintext
json
}
}
}
}
This sample query interrogates:
Sample Query
{
adventureByPath(_path: "/content/dam/wknd/en/adventures/riverside-camping-australia/riverside-camping-australia") {
item {
_path
adventureTitle
_model {
_path
title
}
}
}
}
This query interrogates:
article
at a specific path
The field referencearticle
has the Data type fragment-reference
.
Sample Query
{
adventureByPath(_path: "/content/dam/wknd-shared/en/magazine/western-australia/western-australia-by-camper-van") {
item {
_path
title
_model {
_path
title
}
}
}
}
This query interrogates:
bookmark
Article
The field fragments
has the Data type fragment-reference
, with the model Article
selected. Query delivers fragments
as an array of [Article]
.
{
bookmarkList {
items {
fragments {
_path
author
}
}
}
}
This query interrogates:
bookmark
Article
and Adventure
The field fragments
has the Data type fragment-reference
, with the models Article
, Adventure
selected. Query delivers fragments
as an array of [AllFragmentModels]
, which is dereferenced with union type.
{
bookmarkList {
items {
fragments {
... on ArticleModel {
_path
author
}
... on AdventureModel {
_path
adventureTitle
}
}
}
}
}
There are two flavors of this query:
attachments
.These queries interrogate:
bookmark
The following query returns all content references by using _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
}
}
}
The following query returns all attachments
- a specific field (subgroup) of type content-reference
:
The field attachments
has the Data type content-reference
, with various forms selected.
{
bookmarkList {
items {
attachments {
... on PageRef {
_path
type
}
... on ImageRef {
_path
width
}
... on MultimediaRef {
_path
size
}
... on DocumentRef {
_path
author
}
... on ArchiveRef {
_path
format
}
}
}
}
}
This query interrogates:
bookmark
at a specific path
The RTE inline references are hydrated in _references
.
Sample Query
{
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
}
}
}
}
This query interrogates:
article
at a specific path
variation1
Sample Query
{
articleByPath (_path: "/content/dam/wknd/en/magazine/alaska-adventure/alaskan-adventures", variation: "variation1") {
item {
_path
author
main {
html
markdown
plaintext
json
}
}
}
}
This query interrogates:
article
with a specific variation: variation1
Sample Query
{
articleList (variation: "variation1") {
items {
_path
author
main {
html
markdown
plaintext
json
}
}
}
}
This query interrogates:
article
and all variationsSample Query
query {
articleList(
includeVariations: true ){
items {
_variation
_path
_tags
_metadata {
stringArrayMetadata {
name
value
}
}
}
}
}
This query interrogates:
article
with one, or more, Variations having the tag WKND : Activity / Hiking
Sample Query
{
articleList(
includeVariations: true,
filter: {_tags: {_expressions: [{value: "wknd:activity/hiking", _operator: CONTAINS}]}}
){
items {
_variation
_path
_tags
_metadata {
stringArrayMetadata {
name
value
}
}
}
}
}
This query interrogates:
article
within the fr
localeSample Query
{
articleList (_locale: "fr") {
items {
_path
author
main {
html
markdown
plaintext
json
}
}
}
}
The sample queries are based on the following structure, which uses:
One, or more, Sample Content Fragment Models - form the basis for the GraphQL schemas
Sample Content Fragments based on the above models
For the sample queries, use the following Content Models and their interrelationships (references ->):
The basic fields defining the company are:
Field Name | Data Type | Reference |
---|---|---|
Company Name | Single-line text | |
CEO | Fragment Reference (single) | Person |
Employees | Fragment Reference (multifield) | Person |
The fields defining a person, who can also be an employee:
Field Name | Data Type | Reference |
---|---|---|
Name | Single-line text | |
First name | Single-line text | |
Awards | Fragment Reference (multifield) | Award |
The fields defining an award are:
Field Name | Data Type | Reference |
---|---|---|
Shortcut/ID | Single-line text | |
Title | Single-line text |
The fields for defining a city are:
Field Name | Data Type | Reference |
---|---|---|
Name | Single-line text | |
Country | Single-line text | |
Population | Number | |
Categories | Tags |
The following fragments are used for the appropriate model.
Company Name | CEO | Employees |
---|---|---|
Apple | Steve Jobs | Duke Marsh Max Caulfield |
Little Pony Inc. | Adam Smith | Lara Croft Cutter Slade |
NextStep Inc. | Steve Jobs | Joe Smith Abe Lincoln |
Name | First Name | Awards |
---|---|---|
Lincoln | Abe | |
Smith | Adam | |
Slade | Cutter | Gameblitz Gamestar |
Marsh | Duke | |
Smith | Joe | |
Croft | Lara | Gamestar |
Caulfield | Max | Gameblitz |
Jobs | Steve |
Shortcut/ID | Title |
---|---|
GB | Gameblitz |
GS | Gamestar |
OSC | Oscar |
Name | Country | Population | Categories |
---|---|---|---|
Basel | Switzerland | 172258 | city:emea |
Berlin | Germany | 3669491 | city:capital city:emea |
Bucharest | Romania | 1821000 | city:capital city:emea |
San Francisco | USA | 883306 | city:beach city:na |
San Jose | USA | 102635 | city:na |
Stuttgart | Germany | 634830 | city:emea |
Zurich | Switzerland | 415367 | city:capital city:emea |