Schema tutorial
Schema tutorialLesson 2: Querying dynamic data

Lesson 2: Querying dynamic data

Gato GraphQL can further augment WordPress' capabilities to search data via the use of "function" fields (a distinct type of field which provides functionality instead of data), allowing us to dynamically compute data, input it back into the query, and affect the response with granular control.

Examples

Gato GraphQL provides function fields under the concept of Global fields: Fields that are accessible under all the types from the GraphQL schema. (Normal fields in GraphQL, in contrast, are accessible only under some specific type, such as Post or User).

By convention, global fields in Gato GraphQL start with _ (and normal fields do not).

The PHP Functions Via Schema extension provides many of the most common PHP functions as global fields, including:

  • _arrayItem
  • _arrayJoin
  • _date
  • _equals
  • _inArray
  • _intAdd
  • _isEmpty
  • _isNull
  • _makeTime
  • _objectProperty
  • _sprintf
  • _strContains
  • _strRegexReplace
  • _strSubstr
  • _time,
  • And many more...

We can create dynamically-generated data, and input it into a filter to fetch posts, comments, etc.

This query retrieves the number of comments added to the site in the last 24 hs, which is computed as "time now minus 86400 seconds":

query {
  timeNow: _time  
  time24HsAgo: _intSubstract(
    substract: 86400,
    from: $__timeNow
  )
  date24HsAgo: _date(
    format: "Y-m-d\\TH:i:sO",
    timestamp: $__time24HsAgo
  )  
  commentsAddedInLast24Hs: commentCount(
    filter: {
      dateQuery: {
        after: $__date24HsAgo
      }
    }
  ) 
}

$__timeNow is a variable dynamically created by the Field to Input extension, which allows us to obtain the value of a field and input it into another field in that same operation.

The field to obtain the value from is referenced using the "Variable" syntax $, and __ before the field alias or name:

{
  posts {
    excerpt
 
    # Referencing previous field with name "excerpt"
    isEmptyExcerpt: _isEmpty(value: $__excerpt)
 
    # Referencing previous field with alias "isEmptyExcerpt"
    isNotEmptyExcerpt: _not(value: $__isEmptyExcerpt)
  }
}

This query retrieves the number of comments added to the site starting from "24 hours ago", "1 year ago", "beginning of the month", and "beginning of the year":

query {
  timeNow: _time  
  time24HsAgo: _intSubstract(substract: 86400, from: $__timeNow)
  date24HsAgo: _date(format: "Y-m-d\\TH:i:sO", timestamp: $__time24HsAgo)  
  time1YearAgo: _intSubstract(substract: 31536000, from: $__timeNow)
  date1YearAgo: _date(format: "Y-m-d\\TH:i:sO", timestamp: $__time1YearAgo)
  timeBegOfThisMonth: _makeTime(hour: 0, minute: 0, second: 0, day: 1)
  dateBegOfThisMonth: _date(format: "Y-m-d\\TH:i:sO", timestamp: $__timeBegOfThisMonth)
  timeBegOfThisYear: _makeTime(hour: 0, minute: 0, second: 0, month: 1, day: 1)
  dateBegOfThisYear: _date(format: "Y-m-d\\TH:i:sO", timestamp: $__timeBegOfThisYear)
  
  commentsAddedInLast24Hs: commentCount(filter: { dateQuery: { after: $__date24HsAgo } } )  
  commentsAddedInLast1Year: commentCount(filter: { dateQuery: { after: $__date1YearAgo } } )  
  commentsAddedSinceBegOfThisMonth: commentCount(filter: { dateQuery: { after: $__dateBegOfThisMonth } } )  
  commentsAddedSinceBegOfThisYear: commentCount(filter: { dateQuery: { after: $__dateBegOfThisYear } } )
}

This query is the same as the previous one, however it retrieves the standardized time format "Y-m-d\\TH:i:sO" from PHP constant DATE_ISO8601:

query {
  # This PHP constant will provide standard time format "Y-m-d\\TH:i:sO"
  DATE_ISO8601: _env(name: DATE_ISO8601)
  timeNow: _time  
  time24HsAgo: _intSubstract(
    substract: 86400,
    from: $__timeNow
  )
  date24HsAgo: _date(
    format: $__DATE_ISO8601,
    timestamp: $__time24HsAgo
  )
}

Field _env is provided via the PHP Constants and Environment Variables via Schema extension.

Via the applied Schema Configuration and plugin Settings, we can configure which constants and environment variables can be queried.