Implementing GraphQL

Andrew Smoker
3 min readMay 19, 2021

I’ve been looking at a lot of job postings for Software Engineer positions lately and one thing I kept seeing was the company was looking for experience using GraphQL. This was not something we covered in bootcamp, so I didn’t know anything about it. Since it seemed like something that would add to my skill set, I decided to teach myself what it was and how to use it!

GraphQL is a query language for APIs so you can execute queries using your database on the server-side. There are many benefits to using it over regular calls to the database, but the main one is it gives you more flexibility. You can create queries based on your needs without over- and under-fetching data. Unlike REST, GraphQL allows you to request specific data you need as opposed to a fixed data structure. As websites and APIs have become more complex, the REST structure doesn’t always work and that is where GraphQL steps in.

Starting from scratch, I decided to follow a tutorial that would walk me through all the steps of setting up a new application for use with GraphQL. Here is a link to the site I used: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-ruby-on-rails-graphql-api. If you are just starting out, I would highly recommend walking through these steps.

A GraphQL server gives the client a schema for them to access data that outlines how to request certain information. In your app you set up Types, Queries and Mutations to handle the requests. Types set up which data model will be used and resolved with the request. Queries are for reading data and Mutations are for creating, updating or deleting data.

I practiced on a simple Note Taking application to help learn the syntax involved when writing queries, mutations and types. All of these files are organized in the graphic folder under /app.

Queries

class FetchNotes < Queries::BaseQuery
type [Types::NoteType], null: false
def resolve
Note.all.order(created_at: :desc)
end
end

Mutations

class AddNote < Mutations::BaseMutation  argument :params, Types::Input::NoteInputType, required: true  field :note, Types::NoteType, null: false  def resolve(params:)
note_params = Hash params
begin
note = Note.create!(note_params)
{note: note}
rescue ActiveRecord::RecordInvalid => e
GraphQL::ExecutionError.new(“Invalid attributes for #
{e.record.class}:”\
“ #{e.record.errors.full_messages.join(‘, ‘)}”)
end
endend

Types

class NoteType < Types::BaseObject
field :id, ID, null: false
field :title, String, null: true
field :body, String, null: true
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
end

You can see the type defines what my Note Object looks like with an id, title and body. The query, FetchNotes, describes what Type of object will be resolved and then defines the code in a resolve function to get to the data returned. The mutation, AddNote, is similar but accepts an argument of params and returns a field type of Note. The resolve function uses the params to create a new note and returns it as the Note. Error messages are also very convenient with GraphQL and will return exactly why a request didn’t work.

There is a lot to learn and it involves some complicated syntax, but I already understand the benefit of using GraphQL and why a lot of companies are using it. I encourage you to walk through the tutorial above and try this out for yourself!

--

--

Andrew Smoker

I am 34 years old and making a huge career change by attending Flatiron School’s Software Engineering Bootcamp. Excited to learn!