Enabling low-level editing of persisted queries

In GraphQL, directives are functions that enable to modify the result from a field. For instance, a directive @upperCase will transform the value of the field into uppercase format.

There are 2 types of directives: those that are applied to the schema and are executed always, on every query; and those that are applied to the query, by the user or the application on the client-side.

The GraphQL API for WordPress implements plenty of functionality through directives to be applied to the schema. The corresponding directives are added to the query via some user interface.

That is the case, for instance, with providing Cache Control and Access Control. Cache Control works by applying directive @cacheControl on the schema, and carried out by the plugin through the Cache Control List user interface:

Defining a cache control policy
Defining a cache control policy

Some examples of schema-type directives which regulate Access Control are:

  • @disableAccess
  • @validateIsUserLoggedIn
  • @validateIsUserNotLoggedIn
  • @validateDoesLoggedInUserHaveAnyRole.
  • @validateDoesLoggedInUserHaveAnyCapability

Enabling low-level editing permalink

Module "Low-Level Persisted Query Editing" is disabled by default. When enabled, all directives to be applied to the schema become available in the GraphiQL editor when editing persisted queries.

Schema-type directives
Schema-type directives

This way, and add the schema-type directives already in the GraphQL query when editing the persisted query, and skip the user interface.

For instance, defining Cache Control can be done directly in the persisted query, by setting directive @cacheControl with argument maxAge on the field:

Schema-type directives available in the Persisted queries editor
Schema-type directives available in the Persisted queries editor