GraphQL on Rails

GraphQL on Rails

0c471249ee93ab17a011f3e8d2b1057a?s=128

AKAMATSU Yuki

January 23, 2019
Tweet

Transcript

  1. GraphQL on Rails 2019/01/23 Tokyo Rails#38 Yuki Akamatsu @cookpad

  2. Who am I? •Yuki Akamatsu •id: ukstudio •Rails programmer •Working

    at Cookpad ‣New service development dept.
  3. Our project •Named Tabedori ‣たべかたドリル •Improvement cooking skills •Everyone can

    cooking without recipes
  4. Technology stack of Tabedori •Mobile app ‣ React Native +

    TypeScript •GraphQL Server ‣ Ruby on Rails •Admin tool(CMS) ‣ React.js + TypeScript (SBQI2-4FSWFS .PCJMFBQQ "ENJOUPPM
  5. Agenda •What is GraphQL •How to implement GraphQL on Rails

  6. What is GraphQL

  7. Query language •GraphQL is a query language for Web API

    ‣ And also server-side runtime • Like Node.js, Rails, AppSync, and other things
  8. One endpoint •GraphQL server has only one endpoint •All queries

    are sent to the endpoint ‣ POST method only ‣ All responses are returned with 200 status $MJFOU 4FSWFS 1045HSBQIRM 3FUVSOSFTQPOTF POMZ
  9. GraphQL Query • You can request any fields you need

    • You don’t need to receive unnecessary data • You can get many resources in a single request • It’s better for performance
  10. Error • If there is something wrong, GraphQL server returns

    a response what contains errors key
  11. Type definition •All resources and fields have a type •You

    can fetch fields what are defined on Type
  12. Type is good •There is no mixing of multiple types

    •Null checking isn’t required ‣ Non-null constraint can be defined by using ! in the schema •Becoming documentation ‣ Can generate documentation from the schema
  13. Documentation •You can see documentation using IDE or documentation tools

    •Documents are very useful for communication between client-side programmers and server-side programmers
  14. GrahpiQL •https://github.com/graphql/ graphiql •Graph”i”QL is browser IDE for GraphQL ‣

    It can send requests to graphql server •You can see document quickly
  15. GraphiQL is awesome •GraphiQL make easy to request to graphql

    server ‣ You can try any queries easily and interactively •If you use desktop app, I recommend prisma/ graphql-playground ‣ I’m using this
  16. graphql-docs •https://github.com/ gjtorikian/graphql-docs •graphql-docs is gem that generate documentation as

    html from the schema •It might be a good if you want to host static file
  17. Public APIs •You can try GraphQL query on these sites

    •GitHub ‣ https://developer.github.com/v4/explorer/ •SWAPI ‣ https://graphql.org/swapi-graphql/ •And so on ‣ https://github.com/APIs-guru/graphql-apis
  18. How to implement GraphQL on Rails

  19. graphql-ruby •http://graphql- ruby.org/ •graphql-ruby is probably best way in now

  20. Define types and fields •graphql-ruby provides DSL for types and

    fields definition •You can define Type as Ruby class
  21. Define resolver (Query) •Resolver represents a single field •And can

    be used to fetch data from any data source ‣In this case, fetching from RDB using ActiveRecord •Each user is assigned to UserType
  22. Define resolver (User) •“object” is provided graphql-ruby •“object” is passed

    from parent type ‣In this case, “object” is User instance •If resolver isn’t defined, delegate to “object” by default ‣It means resolver is optional ‣In other words, these resolvers in this example are unnecessary
  23. It works •This is the end of the minimal implementation

    •I didn’t tell you about some processes ‣ e.g. You need to execute “rails g graphql:install” •What matters is defining types and resolvers
  24. Need to solve N+1 problem •The example has N+1 problem

    •If There are 20 users, this query calls select query from posts 20 times ‣It’ too bad
  25. Bad way •Use eager_load to single query to DB •It

    is becoming unnecessary cost if you don’t need posts
  26. graphql-batch •https://github.com/Shopify/graphql-batch •graphql-batch provides an executor which allows queries to

    be batched •It’s inspired by DataLoader created by Facebook
  27. AssociationLoader •Implement AssociationLoader ‣You can copy from graphql-batch’s repository •Just

    use AssociationLoader
  28. Why was it solved? •In brief, graphql-batch collects user’s id

    at first. •Then execute batched query ‣ “where(user_id: collected_user_id)” IUUQTFOHJOFFSJOHTIPQJGZDPNCMPHTFOHJOFFSJOHTPMWJOHUIFOQSPCMFNGPSHSBQIRMUISPVHICBUDIJOH
  29. Pagination •I recommend to using connection_type ‣ Pros: easy to

    implementation ‣ Cons: provides only Relay cursor-based pagination •You need to implement by yourself If you want to use another way
  30. Relay cursor-based pagination •Relay is framework of GraphQL created by

    Facebook •Cursor-based is suited for infinity scroll $VSTPS $VSTPS PQBRVF$VSTPS $VSTPS $VSTPS $VSTPS
  31. connection_type •Any object type has connection_type method •Just use connection_type

    instead of [] in type specification
  32. Conclusion •What is GraphQL ‣ GraphQL is query language for

    Web API ‣ GraphQL is strongly typed •How to implement GraphQL on Rails ‣ You would use graphql-ruby and graphql-batch ‣ connection_type is a way of pagination
  33. Thank you •There are some topics I didn’t talk ‣

    How to create/update/destroy data ‣ Uploading images, Error handling, Designing schema, and so on •The presentation may be insufficient due to my english ability •Please feel free to ask me in FAQ or after this talk ‣ Please could you speak slowly and simply