Building a GraphQL API
Contents
Build a Notes API with queries, mutations and live updates.
Introduction
GraphQL lets a client ask for exactly the data it needs, in a single request, against a schema it can discover for itself. This guide builds one from scratch on Enonic XP, in five short chapters.
By the end you will have:
-
a local development environment and a running Enonic application
-
a GraphQL schema with your own object types and resolvers
-
mutations that change data
-
an endpoint you can drive from cURL, or from whichever GraphQL client you prefer
-
and, as an optional last step, a subscription that streams events as they happen
Everything here is server-side — there is no UI to build, and every step ends with a command you can run to see it work.
What you will be working with
Two building blocks do the job, and it is worth knowing which does what.
The GraphQL Library is where your API takes shape. It defines the schema — the types, fields and resolvers that describe your data — and executes incoming operations against it.
A universal API is what makes that schema reachable. It is XP’s way of exposing an HTTP endpoint from an application: you declare where it is mounted and who may call it, and XP routes matching requests to your code. Without one, a schema is just an object in memory.
You will create both in the Hello GraphQL chapter, then build on them through mutations and subscriptions.
This edition covers GraphQL Library 3.x, which requires Enonic XP 8.0 or newer.
| This tutorial builds an API of your own design, with your own types and resolvers. If what you need is a ready-made GraphQL API for querying CMS content, you want Guillotine instead. |
The first step is to crank up the development environment.
Deep dive
This tutorial uses a small part of the library — enough to get a working API and no more.
The full reference covers the rest: every scalar, object and input type, interfaces, unions and enums, Relay-style connections for paging, and the publishers behind subscriptions. It lives at
and documents the library’s three modules — /lib/graphql for schemas, types and execution, /lib/graphql-connection for cursor-based paging, and /lib/graphql-rx for subscriptions.