Interacting with the GraphQL APIChanging the path to where a field is printed in the response [PRO]
Changing the path to where a field is printed in the response [PRO]
This question appeared on Reddit:
I have:
I need
frontmatter.date
to bepublishedAt
:Problem is, when I do this, I end up with:
Instead of (which is what I need):
Is it even possible to alias nested fields like this?
In other words, is it possible to tell the GraphQL server to flatten the shape of the response? And, if so, how to do it?
Here is a solution with Gato GraphQL, making use of the following extensions:
- Multiple Query Execution, to
@export
the value of a variable across GraphQL operations - PHP Functions via Schema, to print this value again on the desired location via the
_echo
field
With @export
, we can have a first query operation export some result to a variable, and then declare a second query operation that will read this variable and print it on the expected location in the response:
...and then executing the query (passing ?operationName=PrintRelocatedDate
) will produce this response: