This article contains examples of how to create queries using the GraphQL querying services, when working with Optimizely solutions. The examples here are based on the "Alloy" sample site for Optimizely CMS.
Tip: You can use Ctrl + Space bar (Alt + Space bar on OSX) to trigger the autocomplete in the GraphiQL user interface.
Examples
Selecting fields
Get Content items with Name and Url.
{
Content {
items {
Name
Url
}
}
}
Get English (en) Content items with Name and Url.
{
Content(locale: en) {
items {
Name
Url
}
}
}
Get English (en) StandardPage(s) with Name and Url.
{
StandardPage(locale: en) {
items {
Name
Url
}
}
}
Matching on values in fields
Get English (en) StandardPage(s) with Name and Url where MainBody contains "alloy".
{
StandardPage(
locale: en
where: {
MainBody: {
contains: "alloy"
}
}
)
items {
Name
Url
}
}
Get English (en) StandardPage(s) with Name, TeaserText, and MainBody, where MainBody contains "alloy".
{
StandardPage(
locale: en
where: {
MainBody: {
contains: "alloy"
}
}
)
{
items {
Name
TeaserText
MainBody
}
}
}
Get English (en) StandardPage(s) with Name, TeaserText, and MainBody, where MainBody contains "alloy" and TeaserText starts with "explore".
{
StandardPage(
locale: en
where: {
MainBody: {
contains: "alloy"
}
TeaserText: {
startsWith: "explore"
}
}
)
{
items {
Name
TeaserText
MainBody
}
}
}
Get English (en) StandardPage(s) with Name, TeaserText, and MainBody, where MainBody contains "alloy" OR TeaserText starts with "explore".
{
StandardPage(
locale: en
where: {
OR: [
{
MainBody: {
contains: "alloy"
}
}
{
TeaserText: {
startsWith: "explore"
}
}
]
}
)
{
items {
Name
TeaserText
MainBody
}
}
}
Get English (en) StandardPage(s) with MainBody and TeaserText, where MainBody has "alloy" and TeaserText does not start with "explor" (e.g. explore, exploration, exploring, etc) and MetaKeywords have one or more of the matching values.
{
StandardPage(
locale: en
where: {
MainBody: {
contains: "alloy"
}
NOT: [
{
TeaserText: {
like: "explor%"
}
}
],
MetaKeywords: {
in: ["Online seminar","collaboration", "Alloy Plan"]
}
}
)
{
items {
MainBody
TeaserText
}
}
}
Get StandardPage(s) with MainBody and Created that have a MainBody that contains "alloy" and have created between 2020-09-13 and 2020-10-3.
{
StandardPage(
where: {
MainBody: {
contains: "alloy"
}
Created: {
gte: "2012-09-13"
lt: "2012-10-03"
}
}
)
{
items {
MainBody
Created
}
}
}
Sorting
Get English (en) StandardPage(s) with Name and Url sorted by Name - ascending.
{
StandardPage(
locale: en
orderBy: {
Name: ASC
}
)
{
items {
Name
Url
}
}
}
Get English StandardPage(s) with Name and Url sorted by relevance first, and Name after (in case two content items have same relevance).
{
StandardPage(
locale: en
orderBy: {
_ranking: RELEVANCE
Name: DESC
}
)
{
items {
Name
Url
}
}
}
Pagination
Get English (en) StandardPage(s) with Name and Url sorted by name, skip 5 items and take 5 items.
{
StandardPage(
locale: en
skip: 5
limit: 5
orderBy: {
_ranking: RELEVANCE
Name: DESC
}
)
{
items {
Name
Url
}
}
}
Faceting
Get top ranked (out of all matches) StandardPage in all used languages with MainBody and MetaKeywords and where MetaKeywords starts with "Alloy". Also return the top 5 most frequently occuring facets in MetaKeywords.
{
StandardPage(
limit: 1
where: {
MetaKeywords: {
startsWith: "Alloy"
}
}
)
{
items {
MainBody
MetaKeywords
}
facets {
MetaKeywords(limit: 5) {
Name
Count
}
}
}
}
Apply a facet by adding it to the where parameter with the eq operator.
{
StandardPage(
limit: 10
where: {
MetaKeywords: {
startsWith: "Alloy"
eq: "Alloy Track"
}
}
)
{
items {
MainBody
MetaKeywords
}
facets {
MetaKeywords(limit: 5) {
Name
Count
}
}
}
}
Select total field
Get Content items with total field.
{
Content {
total
items {
Name
Url
}
}
}
Select cursor field
Get Content items with cursor field.
{
Content {
cursor
items {
Name
Url
}
}
}
Related topics
Last updated: Jul 02, 2021