The GraphQL API
GraphQL is:
-
“…a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.”.
See GraphQL.org
-
“…an open spec for a flexible API layer. Put GraphQL over your existing backends to build products faster than ever before…”.
See Explore GraphQL.
-
“…a data query language and specification developed internally by Facebook in 2012 before being publicly open sourced in 2015. It provides an alternative to REST-based architectures with the purpose of increasing developer productivity and minimizing amounts of data transferred. GraphQL is used in production by hundreds of organizations of all sizes…”
See GraphQL Foundation.
For information about the GraphQL API, see the following sections (amongst many other resources):
The GraphQL for AEM implementation is based on the standard GraphQL Java Library. See:
GraphQL Terminology
GraphQL uses the following:
-
- Schemas are generated by AEM based on the Content Fragment Models.
- Using your schemas, GraphQL presents the types and operations allowed for the GraphQL for AEM implementation.
-
-
The path in AEM that responds to GraphQL queries, and provides access to the GraphQL schemas.
-
See Enabling your GraphQL Endpoint for further details.
-
See the (GraphQL.org) Introduction to GraphQL for comprehensive details, including the Best Practices.
GraphQL Query Types
With GraphQL you can perform queries to return either:
-
A single entry
AEM provides capabilities to convert queries (both types) to Persisted Queries, that can be cached by Dispatcher and the CDN.
GraphQL Query Best Practices (Dispatcher and CDN)
The Persisted Queries are the recommended method to be used on publish instances as:
- they are cached
- they are managed centrally by AEM as a Cloud Service
GraphQL queries using POST requests are not recommended as they are not cached, so on a default instance the Dispatcher is configured to block such queries.
While GraphQL also supports GET requests, these can hit limits (for example, the length of the URL) that can be avoided using Persisted Queries.
See Enable caching of persisted queries for further details.
- Create a Cloud Manager environment variable called
ENABLE_GRAPHQL_ENDPOINT
- with the value
true
GraphiQL IDE
You can test and debug GraphQL queries using the GraphiQL IDE.
Use Cases for Author, Preview and Publish
The use cases can depend on the type of AEM as a Cloud Service environment:
-
Publish environment; used to:
- Query data for JS application (standard use-case)
-
Preview environment; used to:
- Preview queries prior to deploying on the Publish environment
- Query data for JS application (standard use-case)
- Preview queries prior to deploying on the Publish environment
-
Author environment; used to:
-
Query data for “content management purposes”:
- GraphQL in AEM as a Cloud Service is currently a read-only API.
- The REST API can be used for CR(u)D operations.
-
Permissions
The permissions are those required for accessing Assets.
GraphQL queries are executed with the permission of the AEM user of the underlying request. If the user does not have read access to some fragments (stored as Assets), they will not become part of the result set.
Also, the user must have access to a GraphQL endpoint to be able to execute GraphQL queries.
Schema Generation
GraphQL is a strongly-typed API, which means that data must be clearly structured and organized by type.
The GraphQL specification provides a series of guidelines on how to create a robust API for interrogating data on a certain instance. To do this, a client must fetch the Schema, which contains all the types necessary for a query.
For Content Fragments, the GraphQL schemas (structure and types) are based on Enabled Content Fragment Models and their data types.
For example, if a user created a Content Fragment Model called Article
, then AEM generates a GraphQL type ArticleModel
. The fields within this type correspond to the fields and data types defined in the model. In addition, it creates some entrypoints for the queries that operate on this type, such as articleByPath
or articleList
.
-
A Content Fragment Model:
-
The corresponding GraphQL schema (output from GraphiQL automatic documentation):
This shows that the generated type
ArticleModel
contains several fields.-
Three of them have been controlled by the user:
author
,main
andreferencearticle
. -
The other fields were added automatically by AEM, and represent helpful methods to provide information about a certain Content Fragment; in this example, (the helper fields)
_path
,_metadata
,_variations
.
-
-
After a user creates a Content Fragment based on the Article model, it can then be interrogated through GraphQL. For examples, see the Sample Queries (based on a sample Content Fragment structure for use with GraphQL).
In GraphQL for AEM, the schema is flexible. This means that it is auto-generated each and every time a Content Fragment Model is created, updated or deleted. The data schema caches are also refreshed when you update a Content Fragment Model.
The data schema caches are also refreshed when you update a Content Fragment Model.
The Sites GraphQL service listens (in the background) for any modifications made to a Content Fragment Model. When updates are detected, only that part of the schema is regenerated. This optimization saves time and provides stability.
So for example, if you:
-
Install a package containing
Content-Fragment-Model-1
andContent-Fragment-Model-2
:- GraphQL types for
Model-1
andModel-2
are generated.
- GraphQL types for
-
Then modify
Content-Fragment-Model-2
:-
Only the
Model-2
GraphQL type will get updated. -
Whereas
Model-1
will remain the same.
-
The schema is served through the same endpoint as the GraphQL queries, with the client handling the fact that the schema is called with the extension GQLschema
. For example, performing a simple GET
request on /content/cq:graphql/global/endpoint.GQLschema
will result in the output of the schema with the Content-type: text/x-graphql-schema;charset=iso-8859-1
.