Query FunctionsConditional Field Manipulation
Conditional Field Manipulation
Included in the “Power Extensions” bundle
Addition of meta directives @if and @unless to the GraphQL schema, to conditionally execute a nested directive on the field.
@if
@if executes its nested directives only if a condition has value true.
In this query, users "Leo" and "Peter" get their names converted to upper case, since they are in the "special user" array, while "Martin" does not:
query {
  users {
    name
      @passOnwards(as: "userName")
      @applyField(
        name: "_inArray"
        arguments: {
          value: $userName
          array: ["Leo", "John", "Peter"]
        }
        passOnwardsAs: "isSpecialUser"
      )
      @if(
        condition: $isSpecialUser
      )
        @strUpperCase
  }
}...producing:
{
  "data": {
    "users": [
      {
        "name": "LEO"
      },
      {
        "name": "Martin"
      },
      {
        "name": "PETER"
      }
    ]
  }
}@unless
Similar to @if, but it executes the nested directives when the condition is false.
In this query, it is user "Martin" who gets the name converted to upper case, while the other ones do not:
query {
  users {
    name
      @passOnwards(as: "userName")
      @applyField(
        name: "_inArray"
        arguments: {
          value: $userName
          array: ["Leo", "John", "Peter"]
        }
        passOnwardsAs: "isSpecialUser"
      )
      @unless(
        condition: $isSpecialUser
      )
        @strUpperCase
  }
}...producing:
{
  "data": {
    "users": [
      {
        "name": "Leo"
      },
      {
        "name": "MARTIN"
      },
      {
        "name": "Peter"
      }
    ]
  }
}Prev
