Dev guideRecipesAPI ReferenceChangelog
Dev guideRecipesUser GuidesNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Logical connectors

Describes logical connectors used with the GraphQL API, for the Optimizely querying service, when retrieving content in Optimizely solutions.

Logical connectors let you express Boolean filter logic in Optimizely Graph by combining multiple filter clauses against the same query, so the calling layer can return content that satisfies complex conditions in a single round trip. Use _and, _or, and _not to group field clauses, build nested logic, and exclude content from a result set.

Construct queries with the logical (Boolean) connectors _and, _or, and _not. The precedence of _and, _or, and _not clauses is from left to right. This precedence lets you group one or more fields, where each member uses the Boolean connector. By default, with no Boolean connector, the clauses are conjunctions, an implicit _and.

Precedence does not differ for expressions grouped in curly braces.

Examples

The following examples show the three most common patterns: nested _and inside _or, an _or-only query, and a _not exclusion query. Each example includes a pseudo-code expression that explains the GraphQL query in plain Boolean logic.

_and clause nested within an _or clause

The following query returns content where the name matches either of two people or where the famous quote contains two specific phrases at the same time.

{
  BiographyPage(
    where: {
      _or: [
        {Name: {eq: "Alan Turing"}}
        {
          _and: [
            {FamousQuote: {contains: "What am I in the eyes of most people"}}
            {FamousQuote: {contains: "That is my ambition"}}
          ]
        }
        {Name: {eq: "Charlie Chaplin"}}
      ]
    }
    locale: [en]
  ) {
    Name
    FamousQuote
  }
}

The conditions expressed in where are equivalent to the following pseudo-code:

if (Name == "Alan Turing" OR (FamousQuote.Contains("What am I in the eyes of most people") AND FamousQuote.Contains("That is my ambition")) OR Name == "Charlie Chaplin")

_or clause

The following query returns content where the name matches either of two values, which is the most common standalone use of _or.

{
  BiographyPage(
    where: {
      _or: [
        {Name: {eq: "Alan Turing"}}
        {Name: {eq: "Charlie Chaplin"}}
      ]
    }
    locale: [en]
  ) {
    Name
  }
}

The conditions expressed in where are equivalent to the following pseudo-code:

if (Name == "Alan Turing" OR Name == "Charlie Chaplin")

_not clause

The following query excludes content whose name matches either of two values, so the result set contains every other biography page in the locale.

{
  BiographyPage(where: {
    _not: [
      {
        Name: {
          eq: "Alan Turing"
        }
      },
      {
        Name: {
          eq: "Charlie Chaplin"
        }
      }
    ]
  }, locale: [en]) {
    Name
    Language {
      Name
      DisplayName
    }
  }
}

The conditions in where are equivalent to the following pseudo-code:

if (!(Name == "Alan Turing" OR Name == "Charlie Chaplin"))

Because of De Morgan's law, the conditions are also equivalent to the following:

if (Name != "Alan Turing" AND Name != "Charlie Chaplin")