Interacting with the GraphQL APIRunning Gato GraphQL without WordPress
Running Gato GraphQL without WordPress
Gato GraphQL has been built using standalone PHP components, managed via Composer, in such a way that all the PHP components making up the GraphQL server do not depend on WordPress!
As such, the GraphQL server can run as a standalone PHP application, and you can include it within any PHP application, based on WordPress or anything else.
If for some use case your application doesn't need to access WordPress data, then, at least for that use case, you are ready to go.
This video demonstrates such a use case: Interacting with GitHub's API, to download/install artifacts from GitHub Actions during development:
In the video, the GraphQL query executes an HTTP request to fetch the latest Gato GraphQL plugins generated in GitHub Actions, which are uploaded as artifacts when merging a pull request.
The URLs of the artifacts from the GraphQL response are then injected into WP-CLI, as to have the plugins automatically installed in a local DEV webserver, to run tests.
In this use case, as there is no WordPress data accessed at all, the GraphQL server can already run as a standalone PHP app.
Here is the in-detail explanation of the demo video.
We provide the GraphQL query to run under file retrieve-github-artifacts.gql.
The query connects to the GitHub API by getting the access token from env var GITHUB_ACCESS_TOKEN. It dynamically generates the full path for the actions/artifacts endpoint from the provided variables, and then it sends an HTTP request against it.
From the response, it then extracts the "download URL" from within each artifact item, and sends asynchronous HTTP requests against them. From the Location header of each of these "download URLs", we obtain the actual URL of the downloadable file.
Finally, it prints all URLs together separated by a space, to make it convenient to inject into WP-CLI.
The PHP logic directly loads the code from the Gato GraphQL plugin, and from the “Power Extensions” bundle (needed to send HTTP requests, and other functionality).
As a standalone PHP app, we must explicitly indicate what modules are initialized, and provide any non-default configuration.
For instance, we tell module SendHTTPRequests to allow connecting to https://api.github.com/repos, and module EnvironmentFields to allow accessing environment variable GITHUB_ACCESS_TOKEN.
Notice that the GraphQL schema is generated the first time the GraphQL query is executed, and cached to disk. This way, from the 2nd time onwards, none of the code to compute the schema is executed, making the execution faster.
Finally, the standalone app initializes the GraphQL server, executes the query against it, and prints the response.
To execute the GraphQL query, we run in the terminal (using jq to pretty print the JSON output):
Finally, to extract the artifact URLs from the GraphQL response, and inject them into WP-CLI, we run: