Share
Embed
Transcript
33 slides
-
Who am I? •Yuki Akamatsu •id: ukstudio •Rails programmer •Working
at Cookpad ‣New service development dept. -
Our project •Named Tabedori ‣たべかたドリル •Improvement cooking skills •Everyone can
cooking without recipes -
Technology stack of Tabedori •Mobile app ‣ React Native +
TypeScript •GraphQL Server ‣ Ruby on Rails •Admin tool(CMS) ‣ React.js + TypeScript (SBQI2-4FSWFS .PCJMFBQQ "ENJOUPPM -
Query language •GraphQL is a query language for Web API
‣ And also server-side runtime • Like Node.js, Rails, AppSync, and other things -
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 -
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 -
Error • If there is something wrong, GraphQL server returns
a response what contains errors key -
Type definition •All resources and fields have a type •You
can fetch fields what are defined on Type -
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 -
Documentation •You can see documentation using IDE or documentation tools
•Documents are very useful for communication between client-side programmers and server-side programmers -
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 -
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 -
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 -
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 -
Define types and fields •graphql-ruby provides DSL for types and
fields definition •You can define Type as Ruby class -
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 -
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 -
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 -
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 -
Bad way •Use eager_load to single query to DB •It
is becoming unnecessary cost if you don’t need posts -
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 -
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 -
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 -
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 -
connection_type •Any object type has connection_type method •Just use connection_type
instead of [] in type specification -
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 -
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