Vipul A M

Summarized using AI

Beyond REST in Rails

Vipul A M • September 06, 2019 • Bangkok, Thailand

In Vipul A M's talk 'Beyond REST in Rails' at RubyConf TH 2019, he explores the adoption of GraphQL as an alternative to REST APIs in Ruby on Rails applications. The session starts with an overview of GraphQL, introduced in 2012 and made public in 2015, highlighting challenges faced with traditional REST APIs—especially concerning versioning and over-fetching or under-fetching data due to fixed endpoints.

Key points discussed include:

  • Introduction to GraphQL: An alternative to REST that allows clients to request specific data, minimizing issues like N + 1 queries.
  • Complexity of REST APIs: The challenges of maintaining multiple endpoints and ensuring backwards compatibility in large applications are examined as motivation for transitioning to GraphQL.
  • Schema Definition: GraphQL relies on a defined schema where types and relationships are established, enabling precise queries.
  • Types and Queries: Types are created based on ActiveRecord models, and queries are set up to retrieve data efficiently by specifying which fields to return.
  • Mutations and Subscriptions: Mutations are used to change data, while subscriptions facilitate real-time updates. The talk briefly mentions how these work but focuses primarily on queries and types.
  • Error Handling and Validation: GraphQL validates incoming queries against the schema before execution, returning errors for unsupported fields.
  • Introspection Feature: A notable advantage of GraphQL enabling discovery of available queries and fields without needing external documentation, enhancing team collaboration.
  • Caching and Performance: Adjusted caching strategies are necessary for GraphQL since it operates through a single endpoint. Batching is recommended to improve efficiency.
  • Versioning and Deprecation: Developers can deprecate fields and communicate changes, easing the transition between API versions.
  • Transition Strategy: The speaker advises on a gradual adoption of GraphQL within existing applications, suggesting to start by replacing individual REST controllers with GraphQL.

The session concludes with a call for questions, emphasizing the potential benefits of using GraphQL in enhancing data access patterns in Rails applications.

Beyond REST in Rails
Vipul A M • September 06, 2019 • Bangkok, Thailand

GraphQL is changing how data is being served in Ruby and Rails products from GitHub to KickStarter.

This talk is based on experiences from moving away from Rails REST routing to GraphQL Ruby in production Applications.

Lets explore features like Introspection and others in GraphQL Ruby/Rails.

RubyConf TH 2019

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!
Explore all talks recorded at RubyConf TH 2019
+6