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. It 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 so you can 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 further information about the GraphQL API, see the following sections (among 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 are cached by Dispatcher and the CDN.
GraphQL Query Best Practices (Dispatcher and CDN)
Persisted queries are the recommended method to be used on publish instances as:
- they are cached
- they are managed centrally by AEM
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 requests 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.
GraphiQL Interface
An implementation of the standard GraphiQL interface is available for use with AEM GraphQL.
This interface lets you directly input, and test, queries.
For example:
http://localhost:4502/content/graphiql.html
It provides features such as syntax-highlighting, auto-complete, auto-suggest, together with a history and online documentation:
Use Cases for Author and Publish Environments
The use cases can depend on the type of AEM environment:
-
Publish environment; used to:
- Query data for JS application (standard use-case)
-
Author environment; used to:
-
Query data for “content management purposes”:
- GraphQL in AEM is a read-only API.
- The REST API can be used for CR(u)D operations.
-
Permissions
The permissions are required for accessing Assets.
GraphQL queries are run 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 do not become part of the result set.
Also, the user must have access to a GraphQL endpoint to be able to run GraphQL queries.
Schema Generation
GraphQL is a 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 complete these guidelines, 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 image 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 flexibility means that it is auto-generated each 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 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 gets updated. -
Whereas
Model-1
remains 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
results in the output of the schema with the Content-type: text/x-graphql-schema;charset=iso-8859-1
.