A Pair of Photographs to Mark Fifty Years

As of this September, fifty years have passed since the first two female INSEAD MBA candidates set foot on INSEAD’s Fontainebleau campus in the fall of 1967. See here for a video interview with…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Setting up GraphQL with Ruby on Rails

Restful APIs are pretty useful, but there is often the problem of over-fetching. The only way to get the name of a person is to fetch a person and get all of their attributes, including needless information like height, gender, race, etc. This is where graphQL comes in! In graphQL you have the ability to fetch specific attributes only, so say goodbye to fetching too much data! Today I will go over the basics of how to create a graphQL backend.

Start by creating a file.

Add gem “graphql” to the gem file.

Create some sample data. For example using Yu-Gi-OH: duelist, decks, and cards. The relationship

Generate a model for players.

Generate a model for cards.

Generate a model for decks.

For decks and cards however, the relationship is a little different. Intuitively you might say a deck has many cards, which would be accurate since a deck generally contains 40 cards. However, in all Yu-Gi-Oh games you can make multiple decks, and you can re-use cards that were in a previous deck. (Imagine needing 6 copies of an ultra-rare card if you wanted to include 3 copies of it in two different decks?) So a card can also belong to many decks. Decks have many cards and card have many decks, so you need a join table for the two.

Now run:

Now we need to add the relationships to the models.

For duelists.

For cards.

For decks.

For deckcards.

Now let’s create some seed data in db > seed.db.

Now we need to create graphQL object types.

By default it assumes a value can be null, so if a field is mandatory write null: false next to it.

Now for the query form. This is basically the graphQL version of the controllers when making a restful api.

Write a query for getting all duelists:

The left side is where you can enter queries to retrieve data. Try entering:

On the right side you will get back an array of all the duelists, as well as their name, rank, and an array for their cards and decks.

Notice the change from “field :duelists, [DuelistType], null: true do” to “field :duelist, DuelistType, null: true do”. This is because the query to get all duelists returns an array of them all, while the query to find a single duelist returns only that duelist as an object.

You should get back only a single duelist object with the id of 1, Yugi in this case.

Add the same all search and single search queries for cards and decks. It should end up looking like:

You can now run all or single queries on each of them.

We now need to build for mutations, which allows us to post, modify, and delete items from the database. Create a root mutation.

Inside write the following code:

Now we need to create a mutation to create, update, and delete duelists, cards, and decks.

Modify the create, update, and delete files for each one.

For create duelist:

Update duelist:

Delete duelist:

Do the equivalent for all three, changing the class names and attributes to fit to the corresponding model.

You can try adding a duelist by running the following query:

Now if you fetch all duelists again with

You will see that our new duelist, Marik has been added to the list!

To update a duelist enter:

If you get all duelists again you will see that duelist with id number 4, formerly known as Marik, will now have his name changed to “Zane” and his rank changed to 1.

To delete the newest duelest enter:

Now if you get all duelists again you will see that Zane is gone, and only the original one are left.

Congratulations, your graphQL API is all set up! We have successfully set up a backend api where seed data can be created, objects can be fetched (and specific attributes from them can be fetched too!), and objects can be added, updated, or destroyed! My next article will go into how to connect this to a react app using Apollo!

Add a comment

Related posts:

Broken Promises

A poetic past life recollection to process soulprint trauma through channel writing. “Broken Promises” is published by Raven Shea.

Before I get Super Super Rich!

Life before someone gets super rich is an unknown for most of people. This is because very few people really believe it will happen to them. They simply do not believe that, in a time not far far…

29 Keyboard Shortcuts That Will Save You One Hour A Day

Most people waste hours of their lives because ⌘+C/V is the only shortcut they use. I was one of those people and made time-wasting gestures like You might question whether such tiny actions will…