Project

bx-testing-ground

A ColdBox 8 application on BoxLang, used as a testing ground. It currently hosts the bookshelf demo for bx-graphql 0.3: a small GraphQL API over an in-memory shelf of books, its schema generated from annotated beans and services, with a console on the home page for trying it. It's the demo from the bx-graphql 0.2 post, rebuilt with ColdBox conventions: WireBox instead of hand wiring, a handler instead of a BoxExpress route.

A console to try it

Example queries grouped by subject, a query editor, variables, an operation name and a response pane. Any request can be copied as a curl command.

Code-first, end to end

Types, inputs, enums, queries and mutations come from annotated classes, with @graphqlCrud fields for authors and a few parts written by hand.

Guards and limits

Authorization by annotation on functions, a property and a hand-written resolver, masked errors, depth and complexity limits, and introspection turned off.

Wired by WireBox

instanceFactory hands every class bx-graphql needs to WireBox, so services and resolvers are singletons with their dependencies injected.

Run it

You need CommandBox. The server definition in server.json brings its own BoxLang engine and Java 21.

bashgit clone https://github.com/robertz/bx-testing-ground.git
cd bx-testing-ground
box install
box server start

CommandBox picks a free port and opens the home page. The shelf starts with 25 books by 20 authors, so every example in the console works straight away.

The API

RouteWhat it does
POST /graphqlRuns a query. JSON body with query and optional variables and operationName.
GET /graphql/schemaThe generated SDL as plain text. Add ?full=true for the whole schema.

Replace the port with your server's:

bashcurl -s http://127.0.0.1:8080/graphql -H "Content-Type: application/json" -H "X-Roles: admin" -d '{"query":"{ stats { books authors } }"}'

Queries: book, books, author, authors, search, everything, and stats (admin). Mutations: addBook, updateBook, review, saveAuthor, and removeBook and removeAuthor (admin). The X-Roles header stands in for the signed-in user's roles.

What it demonstrates

FeatureWhere
A type and an input from one beanapp/models/beans/Book.bx, Review.bx
Functions as fields, with argumentsBook.averageRating(), Book.hoursToRead()
Queries and mutations from service functionsapp/models/services/BookService.bx
Partial updates: null versus left outBookService.updateBook(), BookChanges.bx
Batch loadingBook.author() and the authors loader
Interfaces, unions and a hand-written typeapp/models/graphql/schema/shared.graphqls
A custom scalarIsbn, in GraphQLEngine.bx
Authorization by annotationAdminService.bx, Book.note(), BookService.removeBook()
A guarded property and a guarded resolverBook.paid, AuthorResolver.spent()
Structured and masked errorsstructuredErrors, onError and maskErrors, in GraphQLEngine.bx
CRUD fields from a bean@graphqlCrud on Author.bx, carried out by AuthorService.bx
Request bodies and operationNamehandle(), called from handlers/Graphql.bx
Depth and complexity limits, introspection offmaxQueryDepth (6), maxQueryComplexity (100) and introspection, in GraphQLEngine.bx

Things to know

  • The shelf lives in memory. A restart or reinit puts it back to the seeded 25 books.
  • X-Roles is trusted as sent. Don't expose this app as it is.
  • Introspection is off, so tools that ask the endpoint for its schema get a refusal: use GET /graphql/schema?full=true.

Tests

bashbox testbox run

The server must be running.