GraphQL on Rails

GraphQL on Rails

0c471249ee93ab17a011f3e8d2b1057a?s=128

AKAMATSU Yuki

January 23, 2019
Tweet

Transcript

  1. 2.
  2. 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
  3. 7.

    Query language •GraphQL is a query language for Web API

    ‣ And also server-side runtime • Like Node.js, Rails, AppSync, and other things
  4. 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
  5. 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
  6. 10.
  7. 11.

    Type definition •All resources and fields have a type •You

    can fetch fields what are defined on Type
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 20.

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

    fields definition •You can define Type as Ruby class
  15. 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
  16. 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
  17. 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
  18. 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
  19. 25.

    Bad way •Use eager_load to single query to DB •It

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