Introduction to GraphQL
by Oleg Ilyenko / @easyangel
query MyProduct { product(id: 123) { name description picture { width height url } } }
{ "data": { "product": { "name": "Delicious Cake", "description": "Just taste it!" "picture": { "width": 150, "height": 150, "url": "http://..." } } } }
query MyProduct { products { picture(size: 300) { width, height, url } } }
{ "data": { "products": [ { "picture": { "width": 300, "height": 300, "url": "http://..." } }, ... ] } }
query MyProduct { products { thumb: picture(size: 100) { width } fullSize: picture(size: 500) { width } } }
{ "data": { "products": [ { "thumb": { "width": 100 }, "fullSize": { "width": 500 } }, ... ] } }
type Picture { width: Int! height: Int! url: String }
type Query { product(id: Int!): Product products: [Product] }
interface Identifiable { id: String! }
type Product implements Identifiable { id: String! name: String! description: String picture(size: Int): Picture }
mutation ChangeStaff { changeName(productId: 123, newName: "Cheesecake") { id, version } setDescription(productId: 123, description: "Delicious!") { id, version } } subscription ProductEvents { nameChanged(productId: 123) { name } productDeleted { id } }
{ hero { id name } } // Response { "data": { "hero": { "id": "2001", "name": "R2-D2" } } }
var ProductType = new GraphQLObjectType({ name: "Product", description: "Commodity available for purchase", fields: { id: {type: GraphQLString}, onStock: { type: GraphQLInt, resolve(product) { return product.onStock + 10; } } } });
val ProductType = ObjectType( "Product", "Commodity available for purchase", fields[Unit, Product]( Field("id", StringType, resolve = _.value.id), Field("onStock", IntType, resolve = _.value.onStock + 10)))
val ProductType = deriveObjectType[Unit, Product]()