Building a GraphQL API

Contents

Build a Notes API with queries, mutations and live updates.

Introduction

One request, and exactly the fields you asked for:

{ getNotes { id title } }
{"data":{"getNotes":[{"id":"es0c2g0v8gu75","title":"My Note"}]}}

That endpoint is what you will build here, in five short chapters: a Notes API on Enonic XP with your own types and resolvers, mutations that change data, and a subscription that streams changes as they happen. Everything is server-side — there is no UI to build, and every chapter ends with a command you can run to see it work.

Two pieces do the job. The GraphQL Library is where the 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, XP’s way of exposing an HTTP endpoint from an application with its mount path and access rules declared next to the code. Without one, a schema is just an object in memory.

You will create both in the Hello GraphQL chapter and build on them from there. This edition covers GraphQL Library 3.x, which requires Enonic XP 8.0 or newer.

This tutorial builds an API of your own design. If what you need is a ready-made GraphQL API for querying CMS content, you want Guillotine instead.
Ready…​Set…​Code!

The first step is to crank up the development environment.

Beyond this tutorial

These chapters use a small part of the library — enough for a working API and no more. The full reference covers the rest, including interfaces, unions, enums, input types and Relay-style connections, across 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.


Contents

Contents