An Introduction to GraphQL — Part 2 — Server implementation

Photo by Chris Liverani on Unsplash

In part one, we covered how we got here, so how about some code?

I’ve picked Go for the code (server side) in this, no particular reason beyond that’s what I’ve been playing with lately (and also to show that we don’t just have to use JavaScript). The style of coding GraphQL libraries tends to be fairly similar between the languages, so you should be able to follow along.

So we start with the libraries we’re going to use. encoding/json because we’ll convert REST endpoints in to GraphQL for this example. github.com/graphql-go/* is the library we’re using here. net/http for our server, and the rest are just for our various dummy responses.

Now GraphQL has built in types and user types (which ultimately are made up of the built in types right at the bottom). So let’s define some of our types that make up our schema.

We’ve defined three types here. A user, which contains a name and a list of accounts. The account type which is used by the user. And an address type.

As you can see, these ultimately end up being a bunch of Strings, Ints and Floats.

Next up, we actually define our Query Schema. This is the same as the other types, but we define the resolvers here also (these can be in the types as well, so this is just an over encompassing type). In reality, we’d separate the resolvers into external functions for readability, but I’ve left them as anonymous functions for ease in this article.

So let’s break this down a bit. The easy bits are the “latest post”, “random” and “double” sections. Each of these has a type (String, Int and Int respectively), a Description (for our automatic documentation) and a Resolve function that does the actual work. By defining our return types as “NewNonNull” we can always guarantee these will be defined. To make these nullable would break the schema in future revisions, so make sure they will never be nullable. Err on the side of caution here and make them nullable (then check in the client side code).

Additionally our “double” section can take arguments. Once again, Non-nullable (ie. required field). The beauty of GraphQL is that if we try to call this without an argument, an error will be returned. Additionally if we try to call this with a String instead of an Int, an error will also be returned. Self documenting code is wonderful.

The “user” and “address” sections are practically the same, and are just a quick and dirty method of taking in JSON from a REST endpoint and passing it along. Specifying fields in your GraphQL request does all the magic of only passing back the required data. In a production system, the reality is that you’ll probably want to do a bit more checking of the response here.

Finally we set our schema query to be this type (later on, this is where we add a mutation type). Then we fire up the server.

For the full project, I’ve put up a basic repo on github. The mocks.sh is for adding mocks to a running Mountebank server.

So now we have a basic server, we can actually play with this without even writing a client. Run the server (typically “go get && go run main.go”) then browse to http://localhost:8090/graphql. You’ll be greeted with the following screen.

So a pretty simple interface. Enter your query on the left, hit play and you’ll get a result on the right. The beauty is you have documentation (tab on the far right) AND autocomplete.

Let’s start with something basic.

Enter that, click play, and you should see the following (with the number likely to be different).

Simple. But as this query is one big type, we can get multiple things at once, try the following

Hold up a minute, where are the commas, and what’s that test thing at the bottom? Well, to answer the first, we’re only JSON-like, not actual JSON. Feel free to add the commas if you like, but they’re entirely optional.

Now to the second part, the “test” at the bottom is aliasing a field. The beauty of this is it means you can request the same data multiple times just by giving it a different alias (think of a SQL query where you join a table to itself, you have to alias at least one of those tables to refer to it correctly).

Feel free to play around with this a little. Try entering a string into the “val” of double. Request a field that doesn’t exist, etc.

Now to get a bit more complex. Let’s query in to one of those more complex types.

Not much harder is it? You just have to specify the fields you’re after within the types. Here, the user type also has additional data, but we’re not requesting it, so it doesn’t come back. This is HUGE. You can save quite a lot of bandwidth and unnecessary processing here. Those of us with data caps will love you for it.

One of the last things I’ll be showing you in this intro is naming queries.

In your client side code, your queries are more likely to look like this, especially when using a library like Apollo that does some code generation for you. Otherwise it has no clue what to call your objects.

So querying data is all well and good, how do we go about changing data? That’s where mutations come in. Much like with REST, there’s nothing to physically stop you doing it in your normal queries (or GET with REST), but like all good programmers, you like you code to be correct don’t you?

So let's extend our little Go server (or check out the mutate branch if using the code from my github).

We need to add variable to store our mutating data to. Yes, not good practice having a global variable, but this is just example code. Add the latestPost line(extra code around for context).

Find where we are returning the latestPost in the query and update it to return the new variable instead.

Update the main function to store a default value for latestPost.

Finally we’ll create our mutation and add it to our schema

As you can see, it’s pretty simple. We take an argument for the new value, and just set it, then we return that latestPost value as we normally would in a query (this is standard practice to make updates easier on the client side).

So to actually change this value, fire up your browser and point it to your server to bring up the playground and enter the following.

All the same rules from queries still apply like naming your requests, but that’s all there is to it.

In the final part of this series, we’ll explore making a basic client to read this using Apollo in JavaScript.