GraphQL is a query language. One can run query on the data provided by your application, in our case TYPO3 CMS. GraphQL is very much like running query on data sent by REST API. It is more like using REST data an advanced features.
About GraphQL
- GraphQL allows us to run query on data exposed by our APIs
- GraphQL grabs only what is required instead of all the data like in case of a REST
- You can grab data from multiple resources in one single request
- It comes with powerful tool GraphiQL for development
Visit https://graphql.org/ for more details.
GraphiQL
- By default GraphQL endpoint exposed at port number 8080
- In order to enable debugging mode with additional validation, error handling and reporting - add to the query string "?debug=1" to the endpoint
Install GraphQL in TYPO3
To enable GraphQL in TYPO3 you would need to install the "cms-graphql" extension. Please get the latest code and installation instruction from the github page at https://github.com/TYPO3-Initiatives/graphql
It depends on externals libs like
- "hoa/compiler"
- "webmozart/assert"
- "webonyx/graphql-php"
Use composer for smooth installation.
TYPO3 and GraphQL
The TYPO3-Initiatives program organizes and supports many strategic initiatives for development of TYPO3 and long term goals. Such initiatives help achieve targeted results. One such TYPO3-Initiative project is adding GraphQL support to TYPO3.
The project is being carried out under the TYPO3 Datahandler & Persistence Initiative. One of the main goals of this initiative is to clean up the existing data handling layers in TYPO3 and provide a unified persistence layer for all TYPO3 components (frontend, backend and Extbase)
Using GraphQL in TYPO3 Extension
After installing the required extensions you can now use GraphpQL in your TYPO3 extesion. An example code is as follows.
In the controller of your extension:
use TYPO3\CMS\GraphQL;
/**
* T3PMController
*/
class T3PMController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
/**
* action graphql
* @return void
*/
public function graphqlAction() {
$reader = new \TYPO3\CMS\GraphQL\EntityReader();
$result = $reader->execute('
{
tx_t3pm_domain_model_project {
uid,
title
}
}
');
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($result, 'result');
}
}