This post will discuss a Ratpack server, using GraphQL-java 6.0. I should note, that as I did this work, the annotations finally release an upgrade. Doh.
GraphQL Java 6.0
I committed to upgrade. The annotations had not kept up so this meant a bit of a rewrite. Normally I’m pretty suspicious of gratuitous annotation use. They often mask too much of what’s really going on, and they tend to spray the information about one concern throughout your code, making it hard to locate coherent answers on a topic. That was exactly the case here. Leaving the annotations behind meant:
- I had to figure out what previous magic I now had to handle on my own.
- I had to determine just how deeply into my code they’d rooted themselves.
I tried to approach it intelligently, but in the end I went with brute force, I removed the dependency, and kept trying to build the project, ripping out references, until, while no longer working, the project built and the annotations were gone. Then I set about fixing what I’d broken.
What Was Missing?
Basically without the annotations there were two things I needed to repair:
- Defining the query schema
- Wiring the queries up to the data sources
Defining Your Schema
GraphQL-java 6.0 supports a domain specific language for defining your schema known as IDL. It’s a major win. First, it gets your schema, which is by definition, a single concept, into one place, and makes it coherent. Second, they didn’t go off and write “Yet Another DSL” but instead supported one that while not part of the official spec, is part of the reference implementation, and has traction in the community. Nice.
Wiring Up Your Data Sources
The best practice for this now is using the “DataFetcher” interface. The name is a bit misleading, since these aren’t just for your queries (i.e. fetching data) but also for your mutations (modifying data). The name is weak, but the interface and it’s use is a breeze.
To the Code
This didn’t change hardly at all. It’s still as simple as grappig a JSON map, pulling out the query and variables, and executing them:
Pretty straight forward.
Defining the Schema: IDL
So in this trivial example all I have are Company entities, defined with this bean:
And all I wanted to support was, get one, get all, save one, delete one. So I needed to define my Company type, two queries, and two mutations. Defining this in IDL was easy:
Loading The Schema
I just tucked my schema definition into my resources folder and then:
Wiring The Data to The Schema
In GraphQL-java, the way I chose to do this is with DataFetcher implementations. So for example, to find a company, by name, from a map it would look about like:
So that’s the way to “fetch” the data, but how do you connect this to your schema? You define a “RuntimeWiring”:
And then you associate that wiring with your schema you loaded:
Well that’s it. You’ve:
- Created a GraphQL dispather
- Defined your entites
- Defined your GraphQL schema
- Created queries
- Instantiated the schema, wired in the queries
Done. Take a look at my GraphQL server in Ratpack for the complete working code.