Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using and Optimising GraphQl with Rails

Ankita
October 26, 2018

Using and Optimising GraphQl with Rails

Starting out with REST-ful APIs is a natural step for most client-facing applications. As business requirements evolve continuously, maintaining REST endpoints can become increasingly cumbersome, potentially compromising developer experience and speed.

This presentations covers basic concepts of GraphQL and how we integrated it in our Rails application. It includes:

1. Introduction to GraphQL
2. Using GraphQL with Rails
3. Performance optimization of GraphQL endpoints

Ankita

October 26, 2018
Tweet

More Decks by Ankita

Other Decks in Technology

Transcript

  1. … and experimented with our code organization Stores a collection

    of related values e.g. statuses Fields are similar to functions that can accept inputs, resolvers, and return values Reusable container for field logic that can be re-used across many fields Key-value inputs for fields Collection of types which implement the some of the same fields GraphQL::Batch classes to batch database operations in a single query GraphQL middleware Commands that change the state of a system Definitions to expose fields on an object
  2. GraphQL did make the clients happy by... having a strongly

    typed schema (living documentation) clients could specify exactly what they needed clients could fetch all the data with a single request
  3. GraphQL’s additional flexibility comes at a cost - Queries can

    be large, especially when compared to REST - Queries can be constructed maliciously - Queries can be resource intensive
  4. … and whitelisted - APIs cannot be opened to public

    - Queries for outdated clients need to be kept
  5. N+1 Problem It's the problem caused when you need to

    make N + 1 SQL Queries, where N is the number of items.
  6. By default, each association is lazily loaded table get all

    customers customer customer customer get 1 item item get 1 item item get 1 item item
  7. N+1 in REST In REST, it is simpler because for

    each endpoint, you know exactly what the application needs and load only the resources that are needed Customer.includes(:items).where(ids: [...])
  8. N+1 in graphQL - We don't know what is needed

    at the time of writing code as the client can ask for anything they want. - We can't load everything all the time, as it puts unnecessary load on the server and database Customer.includes(????????) Customer.includes(:items) # for every request
  9. table get customers customer customer customer item item item graphQL-batch

    key1 key2 key3 get items using key1,2,3 How graphql-batch helps?
  10. How does graphql-batch do grouping? customer customer customer item item

    item graphQL-batch key1 key2 key3 get items using key1,2,3
  11. How does graphql-batch do grouping? customer customer customer item item

    item graphQL-batch key1 key2 key3 get items using key1,2,3
  12. Solution 1: make the query object the true same object

    QueryFactory Resolver graphql-batch query args same query object same query object
  13. Query Factory - Implicit behaviour - Hard to enforce developers

    to use the QueryFactory 100% of the times
  14. Overwrite the grouping function - Hacky (change default behaviour of

    the gem) - Hard for new members to understand
  15. Solution 3: Let’s graphql-batch call QueryFactory QueryFactory Resolver graphql-batch query

    args - Require all query services to have standardized contracts - Suitable for newly created projects model model & query args Query service
  16. Takeaways - graphQL is a good alternative of REST -

    Always think about performance when writing resolvers