Download the PHP package wwwision/cr-graphql without Composer
On this page you can find all versions of the php package wwwision/cr-graphql. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wwwision/cr-graphql
More information about wwwision/cr-graphql
Files in wwwision/cr-graphql
Package cr-graphql
Short Description Simple GraphQL API for the Neos Content Repository
License MIT
Informations about the package cr-graphql
Wwwision.CR.GraphQL
Simple GraphQL Adapter for the Neos Content Repository
Description
This package provides a simple GraphQL API for Neos Content Repositories. It can be used in a Neos distribution or with a standalone Content Repository.
Disclaimer: This is merely an experiment. Feel free to use it or copy and adjust it to your needs, but please be aware of the limitations:
Limitations
- The API just provides read access to nodes of the live workspace (this might change slightly in the future, but this won't become a fully fledged CR API!)
- The API is just a slim wrapper on top of the Content Repository PHP API. Other than the default Neos rendering, there is no caching in place yet!
- CR nodes can be nested infinitely, GraphQL queries can't (see examples below)
Installation
Install this package via composer:
Routes
This package comes with corresponding routes, but they won't be active by default.
This can be changed via some Settings.yaml
:
Note: The path
variable defines the URL path, the GraphQL API will be exposed to, with the above example
this will be https://your-server.tld/graphql
.
Adjust policies
If installed in a Neos distribution, the GraphQL controller is usually
not allowed to be called by unauthenticated users.
This can be changed with the following lines in a Configuration/Policy.yaml
file:
Usage
If installed correctly, you should be able to query the GraphQL endpoint. You can try it via cURL:
This should return something like
Node properties
The properties of a node are represented via a NodeProperties
scalar.
In practice this means, that the properties will be converted to plain JSON in the result.
This package uses the Symfony Serializer to convert
non-scalar properties.
The common object types (DateTime, node references, assets & images) are covered by custom Normalizers.
You can easily configure additional types or change the behavior of the existing ones.
Add custom normalizers
This package already provides a AssetNormalizer
that converts asset to an JSON object like:
To add a custom conversion for Video assets we could create a new Normalizer:
And register it via Settings.yaml
:
Note: We need to set the position
to "start" so that this new normalizer is evaluated before the existing
AssetNormalizer
(since that also supports Video
properties).
Replace existing normalizers
If you want to replace/remove an existing normalizer you can do so by overriding the corresponding settings:
Example Queries
A couple of example GraphQL queries:
Get a single Node by its id
The result on the Neos.Demo site would be something like:
Get all document nodes recursively
GraphQL doesn't support recursive queries (for some good reasons)
but it's possible to use fragments in order to get around that limitation.
The following query will fetch the root node (/sites
), all site nodes (for example /sites/neosdemo
) and then all nodes
below that implement the Neos.Neos:Document
node type up to 5 levels:
The result could look like this:
Get all content nodes on a given document node, recursively
As mentioned above, endless recursion is not possible. But with the following query you can fetch all content and content collection nodes underneath the node with the specified identifier up to 5 levels:
With the following variables:
All versions of cr-graphql with dependencies
t3n/graphql Version ^2.1 | ^3.0
neos/content-repository Version ^5.0 | ^7.0
symfony/serializer Version ^5.1