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

GraphQL on Rails

GraphQL on Rails

AKAMATSU Yuki

January 23, 2019
Tweet

More Decks by AKAMATSU Yuki

Other Decks in Technology

Transcript

  1. Technology stack of Tabedori •Mobile app ‣ React Native +

    TypeScript •GraphQL Server ‣ Ruby on Rails •Admin tool(CMS) ‣ React.js + TypeScript (SBQI2-4FSWFS .PCJMFBQQ "ENJOUPPM
  2. Query language •GraphQL is a query language for Web API

    ‣ And also server-side runtime • Like Node.js, Rails, AppSync, and other things
  3. 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
  4. 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
  5. Type definition •All resources and fields have a type •You

    can fetch fields what are defined on Type
  6. 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
  7. Documentation •You can see documentation using IDE or documentation tools

    •Documents are very useful for communication between client-side programmers and server-side programmers
  8. 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
  9. 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
  10. 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
  11. 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
  12. Define types and fields •graphql-ruby provides DSL for types and

    fields definition •You can define Type as Ruby class
  13. 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
  14. 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
  15. 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
  16. 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
  17. Bad way •Use eager_load to single query to DB •It

    is becoming unnecessary cost if you don’t need posts
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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