GraphQL
&
graphql-ruby
HTTP REST: Lots of Requests
HTTP REST: Response Structure?
Two weeks ago
This week
{
id: 1,
name: "Chan's Great Adventure",
genres: [
"Adventure",
"Comedy",
"Romance"
],
sequels: [
{ /* ... */ },
{ /* ... */ },
{ /* ... */ },
{ /* ... */ }
]
}
{
id: 1,
name: "Chan's Great Adventure",
genres: [
"Adventure",
"Comedy",
"Romance"
]
}
HTTP REST: Overfetching
What I get
What I care about
{
id: 1,
name: "Chan's Great Adventure",
genres: [
"Adventure",
"Comedy",
"Romance"
],
sequels: [
{ /* ... */ },
{ /* ... */ },
{ /* ... */ },
{ /* ... */ }
],
pi: 3.1415926535897932384626433832795028841971693993751
}
{
name: "Chan's Great Adventure",
}
GraphQL
Request
Response
query getChansAdventure {
movie(id: 1) {
name,
sequels {
name
}
}
}
{
data: {
movie: {
name: "Chan's Great Adventure",
sequels: [
"Chan in Trouble",
"Chan Goes for the Gold",
"Chan Maxes Out",
"SpyKids 5: Origins of Chan"
]
}
}
}
Explicit, Nested
graphql-ruby
Build a Schema
Make a Request
MovieType = GraphQL::Type.new do
name "Movie"
description "Full-length feature film"
fields({
name: field(type: !type.String),
staring: field(type: type[ActorType]),
sequels: field(type: type[MovieType]),
})
end
# ...
Schema = GraphQL::Schema.new(
query: QueryType,
mutation: MutationType,
)
query_string = "
query getChan {
movie(id: 1) { name }
}"
query = GraphQL::Query.new(
Schema,
query_string
)
puts query.execute
GraphQL & graphql-ruby
- http://facebook.github.io/graphql/
- github: rmosolgo/graphql-ruby
GraphQL Ruby Lightning Talk
By Robert Mosolgo
GraphQL Ruby Lightning Talk
- 1,127