This GraphQL query retrieves the post content, appending an "Edit this post" link at the bottom of the content for the admin user only:
For admin users, the response will be:
For non-admin users, the response will be:
Having the GraphQL server (given all the possible multiple conditions) dynamically compute the required value for a field:
Simplifies the logic of the application, as there is a single source of truth, the code becomes DRY, and clients don't need to implement the corresponding logic anymore
Makes the application more reliable, specially when multiple clients access data from the server, as different implementations of the same logic can be non-identical, potentially leading to bugs (more so when clients are based on different technologies, such as JavaScript for a website, Java for an Android app, Swift for an iPhone app, and others)
When Multiple Query Execution is enabled, directives @include and @skip can also be applied to operations. This way, we can execute an operation or not depending on the value of some dynamic variable.
In the query below, only one of the two operations will be executed:
RetrieveContentForAdminUser is executed only when $isAdminUser is true
RetrieveContentForNonAdminUser is executed only when $isAdminUser is false
Let's provide two different responses for the post's content field depending on the user being an admin or not:
The first operation uses content as an alias, and computes the field's value dynamically, appending fields originalContent and wpAdminEditURL together via _sprintf
The second operation retrieves the content field directly
Now we have two operations that may be executed, however we can provide only one ?operationName=... when executing the query.
Then, we add operation ExecuteAll that depends on both RetrieveContentForAdminUser and RetrieveContentForNonAdminUser, containing the simple field id (because we must query something in the operation):
Invoking the endpoint with ?operationName=ExecuteAll will now load both operations, however only one of them will be actually executed.