00:00:06.530
All right, hi everyone! First of all, sorry for this; I'm feeling pretty cold.
00:00:11.780
Today, I'll be talking about GraphQL. We are short on time, and I want to give a brief idea about what GraphQL is, then compare it to how you typically build or use REST APIs in your Rails applications.
00:00:24.380
My name is Vipul, and I go by 'vipul' on Twitter. I run a consulting company called 'Aloon', which is still pretty new and growing. We tend to blog a lot on various topics ranging from Ruby to Rails and new things we are working on.
00:00:38.239
In addition to my work, I try to travel and work from different locations. If you haven't heard of Nomad List, and you're using it, we should definitely chat! It's a great area where many people come to work together.
00:01:04.309
Traveling creates weird situations and adventures for me. For instance, last year, I was stuck in a typhoon in Kyoto and survived a major earthquake. I also experienced a blackout in Manhattan after 40 years, and a few weeks back, I witnessed an unusual event in Singapore.
00:01:17.030
These experiences make me reflect on my life, but I'm really happy to be here and hope to meet all of you for a much less eventful discussion.
00:01:24.050
Today, we'll discuss what GraphQL is and how you can utilize it in your Rails applications. GraphQL was introduced around 2012 and became public in 2015. At that time, our team faced numerous challenges with maintaining REST APIs, particularly around versioning across various applications.
00:02:29.440
Some specific concerns we faced included the challenge of accessing resources with many related objects. This often led to N + 1 queries, causing issues with under- or over-querying data. Since REST endpoints are fixed, you end up either having to make multiple trips for more data or fetching too much data.
00:03:03.110
Maintaining APIs in large applications can become a headache. For instance, multiple API endpoints in Rails might use the same model methods, and changing something in one endpoint could disrupt others, making backward compatibility and versioning a significant challenge.
00:04:01.850
This complex situation ultimately led us to adopt GraphQL, which is now widely used by several companies such as GitHub and Shopify.
00:04:15.109
To give a brief overview of GraphQL, you start by defining a schema for accessing information. It allows for defining queries where you can specify exactly what information you want and receive only that, instead of over-fetching or under-fetching data.
00:04:39.830
For example, if you have a model, such as a post that has comments, you can directly request the specific fields you want from both the post and its comments. GraphQL provides a strong type system that supports type safety both on the client and server sides.
00:05:29.900
One significant advantage of GraphQL is its management of versions and deprecations. You can advertise new fields and mark others as deprecated, making it easier for clients to adapt to these changes without breaking their existing functionalities.
00:05:43.099
Before we delve deeper into GraphQL, I’ll give a brief demo of an application called 'ChangeLog'. This application allows users to connect their GitHub repositories, retrieve commits, and publish blog posts about new features, all powered by GraphQL.
00:06:07.670
When loading a page, the application calls GraphQL endpoints to fetch the required data efficiently. You can inspect the types of information being accessed, and the client library utilized is Apollo, which facilitates formatted queries.
00:06:51.849
Now, with this basic introduction to GraphQL, let's look at how to implement it in Rails using the GraphQL Ruby gem. The key concepts to understand are the schema, queries, mutations, and subscriptions. We'll go through these concepts and compare them with traditional REST API usage.
00:08:12.389
To get started with GraphQL, you will add the 'graphql' gem to your Rails application. This sets up a basic structure for using GraphQL, allowing you to use generators to create types based on your ActiveRecord models.
00:08:59.410
You'll start by defining types in your schema, which act similarly to database migrations. Each type—like a post—will define its fields, including associations. For example, a post can have several comments, which can be established as an array.
00:09:39.029
Once you have defined types, the next step is to create queries. You define how to retrieve information from the types you've created. For instance, you can set up a query that returns a specific post by its ID and uses the defined post type structure to deliver the result.
00:10:56.850
With your queries established, you’ll integrate them with the schema. This works similarly to how ActiveRecord connects to your database. You then pass user-generated queries through this schema for execution, which returns a formatted response.
00:12:18.240
GraphQL utilizes a Schema Definition Language (SDL) for defining your types and relationships. You can create types like users, complete with required fields and relationships, similar to how you would in an ActiveRecord model.
00:12:55.539
When it comes to queries and mutations, you can perform fetching and updating tasks respectively. Both work under the GraphQL schema and can encompass various data types, including custom ones.
00:14:13.140
Mutations specifically handle changing data within your application. For example, if you want to publish a commit as a blog post, you’ll define what the response should look like and handle all necessary business logic.
00:15:44.640
GraphQL subscriptions allow for real-time updates in your application, implemented through Action Cable, but we’ll set that aside for now.
00:16:36.930
When comparing GraphQL to REST API in a Rails application, validating input and error handling are similar processes. GraphQL checks incoming queries against its schema before executing them.
00:18:04.070
It will return errors if a client tries to access unsupported fields, effectively preventing unnecessary requests to the server.
00:18:38.030
Another advantage of GraphQL is its introspection capability, which allows users to discover what queries and fields are available without external documentation. This simplifies team communication.
00:19:44.130
Using visibility rules, you can control which fields are accessible based on user authentication status, enabling schema-driven development where teams work based on defined schemas.
00:20:45.990
Performance concerns are important when using GraphQL. Batching requests can help reduce the number of queries made, allowing multiple requests to return simultaneously.
00:21:37.680
Caching strategies must be adapted when using GraphQL since it typically involves a single endpoint, unlike REST APIs. You can use unique IDs paired with type names to manage caching more effectively.
00:22:50.490
Versioning in GraphQL is another compelling feature, allowing developers to mark fields for deprecation and communicate this effectively to clients for a smoother transition.
00:24:32.220
As you transition from REST to GraphQL, you can progressively implement GraphQL in your existing applications, starting by converting individual REST controllers to use GraphQL.
00:25:35.160
This approach lets you handle legacy infrastructure gradually, ultimately allowing for a mix of REST and GraphQL API endpoints as your application evolves.
00:26:54.720
I'm out of time, but I'm happy to discuss further if you have any questions or if you're using GraphQL in your applications. Thank you!