...
Any change made to XpereienCentral that affects the GraphQL schema requires the schema to be fully regenerated. This includes, but is not limited to, enabling/disabling a Content Type or making a change to a Modular Content Template. The schema is regenerated automatically upon receiving the first query after the change that affected the schema was made.
Adding
...
Custom Queries
To add new queries to the API we have to create a new service which implements the GraphQLDeliveryApi.Provider
. A query can be added by creating a public method with the GraphQL SPQR annotations including the @GraphQLQuery
annotation.
...
For this example let's start by creating a Service component using the Quick Start guide. This will generate a plugin for you with a service component. This will provide you with the following files in the com.gxwebmanager.helloworld.helloworldservice folder.
Activator.java
api/
package.html
HelloWorldServiceService.java
WCBConstants.java
service/
package.html
HelloWorldServiceServiceImpl.java
Now we're going to edit Activator.java
and HelloWorldServiceServiceImpl.java
. In order to be picked up by the GraphQL API the HelloWorldServiceServiceImpl.java
needs to implement the GraphQLDeliveryApi.Provider
interface. In addition any methods that should be exposed in the API need to be annotated using the GraphQL SPQR
annotations. The file below contains a complete example of the file.
...
Let’s add a method to our custom element, for example the method String getFavoriteClient()
which returns ones favorite GraphQL client. After adding the method to our interface and implementation, we also have to add it to our Pojo (if we want to expose it via the GraphQL API that is, if not we don’t have to do anything and it won’t be exposed).
Code Block | ||
---|---|---|
| ||
public interface Browser {
public String getName();
public String getVersion();
} |
After creating the Browser implementation, adding the Browser
getBrowser()
method to our interface and implementation like we normally do, we now also have to create a new Pojo for the Browser
interface, which we will name BrowserPojo, and add the browser variable to the GraphQLTestElementPojo
(if we want to expose the variable via the GraphQL API that is, if we don’t want to expose it we don’t have to do anything, since that is the default behavior).
We start by creating the BrowserPojo
class. This is the class that will be used by GraphQL and thus requires the GraphQL annotations. Below is the sample code for the BrowserPojo
class.
Code Block | ||
---|---|---|
| ||
@GraphQLType(name = "Browser", description = "An object representing a Browser.")
public class BrowserPojo {
private final String name;
private final String version;
public BrowserPojo(Browser browser) {
this.name = browser.getName();
this.version browser.getVersion();
}
@GraphQLQuery(description = "Returns the name of the browser.")
public String getName() {
return name;
}
@GraphQLQuery(description = "Returns the version of the browser.")
public String getVersion() {
return version;
}
}
|
Next we will have to make three changes to the GraphQLTestElementPojo
class:
First we have to create a new class variable where we can store our favorite client in (so it can be cached):
private final BrowserPojo browser
;Next we have to set the value of this variable in the constructor, which usually is just calling a getter, but in this case we have to convert the browser to a
BrowserPojo
and make sure it’s not NULL to prevent a NullPointerException from being thrown. So we would add the following line to the constructor:Code Block theme Eclipse this.browser = mediaItemVersion.getBrowser() != null ? new BrowserPojo(mediaItemVersion.getBrowser()) : null;
Lastly we also have to add a new getter including the
@GraphQLQuery
annotation so the browser can be requested via the GraphQL API:Code Block theme Eclipse @GraphQLQuery(description = "Returns the browser of the media item.") public BrowserPojo getBrowser() { return browser; }
And that’s it. We have now added support for a new custom element to our GraphQL API including a new method with a new class definition. This process is identical for custom Media Items, Page Metadata and Form Fragments. The only difference is the base Pojo class which our custom Pojo classes extend from. These being MediaItemArticleVersionPojo for custom Media Items, FormFragmentPojo for custom Form Fragments, PageMetaDataPojo for custom Page Metadata and ElementPojo form custom Elements.