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. GraphQL on Rails
    2019/01/23 Tokyo Rails#38
    Yuki Akamatsu @cookpad

    View Slide

  2. Who am I?
    •Yuki Akamatsu
    •id: ukstudio
    •Rails programmer
    •Working at Cookpad
    ‣New service development
    dept.

    View Slide

  3. Our project
    •Named Tabedori
    ‣たべかたドリル
    •Improvement cooking
    skills
    •Everyone can cooking
    without recipes

    View Slide

  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

    View Slide

  5. Agenda
    •What is GraphQL
    •How to implement GraphQL on Rails

    View Slide

  6. What is GraphQL

    View Slide

  7. Query language
    •GraphQL is a query language for Web API
    ‣ And also server-side runtime
    • Like Node.js, Rails, AppSync, and other things

    View Slide

  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

    View Slide

  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

    View Slide

  10. Error
    • If there is something
    wrong, GraphQL server
    returns a response
    what contains errors
    key

    View Slide

  11. Type definition
    •All resources and fields
    have a type
    •You can fetch fields
    what are defined on
    Type

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  18. How to implement
    GraphQL on Rails

    View Slide

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

    View Slide

  20. Define types and fields
    •graphql-ruby provides
    DSL for types and fields
    definition
    •You can define Type as
    Ruby class

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  25. Bad way
    •Use eager_load to
    single query to DB
    •It is becoming
    unnecessary cost if you
    don’t need posts

    View Slide

  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

    View Slide

  27. AssociationLoader
    •Implement
    AssociationLoader
    ‣You can copy from
    graphql-batch’s
    repository
    •Just use
    AssociationLoader

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  31. connection_type
    •Any object type has
    connection_type method
    •Just use connection_type instead
    of [] in type specification

    View Slide

  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

    View Slide

  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

    View Slide