Movatterモバイル変換


[0]ホーム

URL:


Dev guideRecipesAPI ReferenceChangelog
Dev guideRecipesUser GuidesNuGetDev CommunityOptimizely AcademySubmit a ticketLog InCross-platform products
Dev guide
All
Pages
Start typing to search…

Create queries

How to create queries using the GraphQL querying services.

You can use GraphQL querying services to fetch, filter, and sort content from Optimizely Graph. Results may vary if you customize content types or data in your environment. The examples provided are based on the Optimizely Alloy sample site. See theAlloy documentation.

📘

Note

The following recipes work with the GraphQL schema generated from the Alloy site defaults. Your results may vary if you customize the data or content types in these examples.

Select fields

Select fields (items) from objects of a certain type and return them. The following example gets two items (Name andUrl) from objects of the typeContent.

{  Content(locale: en) {    items {      Name      Url    }  }}

Building on the previous example, you can modify the previous recipe to select items from theStandardPage type.

{  StandardPage(locale: en) {    items {      Name      Url    }  }}

Match on values in fields

You can use fields in GraphQL to restrict results based on their value. The following example getsName,TeaserText, andMainBody items from theStandardPage objects whose locale is English (en), but adds another condition: theMainBody field must contain the string "alloy".

{  StandardPage(    locale: en    where: {      MainBody: {        contains: "alloy"      }    }  )  {    items {      Name      TeaserText      MainBody    }  }}

Building off of the previous example, you can add another condition: theTeaserText must start with "explore".

{  StandardPage(    locale: en    where: {       MainBody: {         contains: "alloy"       }       TeaserText: {         startsWith: "explore"       }    }  )  {    items {      Name      TeaserText      MainBody    }  }}

When combining two or more conditions, you can use a Boolean operator (such asOR) to match fields that meet one or the other conditions. The following example returns items fromStandardPage objects whoseMainBody contains "alloy"or whoseTeaserText starts with "explore."

{  StandardPage(    locale: en    where: {      OR: [        {          MainBody: {            contains: "alloy"          }        }        {          TeaserText: {            startsWith: "explore"          }        }      ]    }  )  {    items {      Name      TeaserText      MainBody    }  }}

You can also negatively restrict results withNOT. This recipe gets data where:MainBody has "alloy"and itsMetaKeywords have one or more of the matching valuesbut it does not have aTeaserText field starting with"explor%" (for instance, "explore", "exploration", "exploring", and so on).

{  StandardPage(    locale: en    where: {      MainBody: {      contains: "alloy"      }      NOT: [        {          TeaserText: {            like: "explor%"          }        }      ],      MetaKeywords: {        in: ["Online seminar","collaboration", "Alloy Plan"]      }    }  )  {    items {      MainBody      TeaserText    }  }}

Certain field types, such as numbers and dates or date-times, can use comparison operators to select based on a range of values. The following example getsStandardPage objects whoseCreated field has a value between2020-09-13 and2020-10-3.

{  StandardPage(    where: {      Created: {        gte: "2012-09-13"        lt: "2012-10-03"      }    }  )  {    items {      MainBody      Created    }  }}

Sort results

You can sort GraphQL results usingorderBy. The following example sorts its results in ascending alphabetical order based on the value of theName field.

{  StandardPage(    locale: en    orderBy: {      Name: ASC    }  )  {    Name    Url  }}

You can also combine sorting criteria. The following example sorts its results by relevance first, then name (in case two items have the same relevance).

{  StandardPage(    locale: en    orderBy: {      _ranking: RELEVANCE      Name: DESC    }  )  {    items {      Name      Url    }  }}

Select the number of results per page

You can select the number of results returned by selecting a range in the total result set. This can be useful to "page" through the results one chunk at a time.

The following example orders the results by relevance and then byName (in case two results have the same relevance) and selects just the first five results by usinglimit. Using this example, you can create the first "page" of five results for a search results page.

{  StandardPage(    locale: en    limit: 5    orderBy: {      _ranking: RELEVANCE      Name: DESC    }  )  {    items {      Name      Url    }  }}

To build the next "page" of five results, you can useskip: 5 to pass over the first five results and select the next five (withlimit: 5 again). You can continue this pattern indefinitely by increasingskip by increments of five (or ten, twenty-five, and so forth).

{  StandardPage(    locale: en    skip: 5    limit: 5    orderBy: {      _ranking: RELEVANCE      Name: DESC    }  )  {    items {      Name      Url    }  }}

Handle expired content

Depending on the authentication method and request headers, expired content can be displayed in GraphQL query results by default.

  • Expired content continues to show as Published in GraphQL.CMS does not trigger a status change when content expires.
  • The StopPublish field indicates expiration:
    • null – content is not expired
    • Past date – content is expired

Authentication behavior

  • Public key (single CMS key) – Expired content is automatically filtered out.
  • Auth headers / Basic Auth keys – Expired content is returned unless explicitly excluded.

Control with request header

You can use thecg-include-expired request header to control whether expired content is returned.

Case 1: Include expired content

cg-include-expired: "true"

Case 2: Exclude expired content

cg-include-expired: "false"

Example

This query returnsStandardPage objects and includes theStopPublish field so you can check expiration. If you add the headercg-include-expired: false, only non-expired items are returned.

{  StandardPage(locale: en) {    items {      Name      Url      StopPublish    }  }}

Updated about 1 month ago



[8]ページ先頭

©2009-2025 Movatter.jp