GraphQL Plugin API

Table of Contents

Setup

Add the interface GraphQLPlugin to your plugin class. You will need to implement the createRootSchema() method to return the object type of your plugin. This type is added to the Gentics Mesh GraphQL API and can be accessed via the pluginApi field.

Each plugin will be added to the pluginApi field using the apiName as the key for the new field:

pluginApi {
    myPluginA {
        text
    }
    myPluginB {
        text
        myField {
            myInfo
        }
    }
}
The createRootSchema() method will be invoked for every query. It is thus advised to only return GraphQL schema which has been generated up-front.

Context

In the datafetcher which will load the data it is possible to access the context via env.getContext(). This will return a GraphQLPluginContext object that contains methods to access user and other request information.

Table 1. GraphQLPluginContext methods excerpt
Method Description

GraphQLPluginContext#projectName()

Name of current project.

GraphQLPluginContext#projectUuid()

Uuid of current project.

GraphQLPluginContext#branchName()

Name of current active branch.

GraphQLPluginContext#branchUuid()

Uuid of current active branch.

GraphQLPluginContext#principal()

User information.

Example
…
.dataFetcher(env -> {
    GraphQLPluginContext ctx = env.getContext();
    // We can check for which project the query was executed
    System.out.println("Project Name: " + ctx.projectName());
    System.out.println("Project Uuid: " + ctx.projectUuid());
    // We can also access the user
    System.out.println("User: " + ctx.principal().encodePrettily());
    return "hello-world";
})
…

It is also possible to use the GraphQL SDL to define the schema. The GraphQL Book Library Plugin Example shows how to setup the API using a static schema definition.