The REST API endpoint newapi.getpop.org/wp-json/wp/v2/users/?_fields=id,name,url produces user data, with some users having property url empty:
The GraphQL query below transforms this response:
Adding a default URL to those users whose url property is empty
Adding a link property to each user entry (composed using the user's name and URL values)
The response is:
Composable directives can nest one or more directives within them. When nesting more than one, we indicate this via argument affectDirectivesUnderPos, which contains the relative positions from that directive to its nested directives.
In the GraphQL query above, directive @underEachArrayItem (provided by the Field Value Iteration and Manipulation extension) is a composable directive. On its first occurrence, it is nesting only one directive, and argument affectDirectivesUnderPos can be omitted:
(Btw, notice that @underJSONObjectProperty is also a composable directive, nesting the @default directive).
On its second occurrence, it is nesting the 4 directives to its right, as indicated by argument affectDirectivesUnderPos with value [1, 2, 3, 4]:
🔥 Tips:
Directive @applyField (provided by the Field on Field extension) has two potential destinations for its output:
Providing argument passOnwardsAs: "someVariableName" will assign the new value to dynamic variable $someVariableName, from which it can be read by the upcoming nested directives:
Providing argument setResultInResponse: true will assign the new value again to the field (hence it will modify the response):
The REST API endpoint newapi.getpop.org/wp-json/newsletter/v1/subscriptions produces a collection of email subscription data, including the subscriber's email and language:
This GraphQL query prints only the emails from the API response, by extracting the email property from each entry and replacing the field value with it:
This example continues on the previous one, plus also converting the format of the emails in the response.
The GraphQL query below extracts the emails from the response of the API, and converts to upper case those from users whose language is English or German via the composable directive @if (provided by the Conditional Field Manipulation extension):
The response is:
Executing conditional logic in Gato GraphQL can be made dynamic: By passing a dynamic variable to @if(condition:) (and also to @unless(condition:)) that was evaluated on the queried object, logic will be executed or not depending on conditions from that entity.
This way, we can dynamically modify the response for some entities (and not others), on conditions such as: