services, originally developed by Facebook. It is an alternative to the standard (?) REST method for building backend APIs. Like REST, GraphQL is generally communicated via HTTP, but it uses it in a very different way. GraphQL Is Friendly (Danielle B.-R.) 2 / 11
strongly-typed schema, built right into the standard. GraphQL schemas are self-describing; the GraphQL backend provides schema details to any interested client The standard specifies, to a limited extent, the execution model for incoming queries. This execution model is dead simple to work with. GraphQL Is Friendly (Danielle B.-R.) 3 / 11
JSON we want and the server fills it in. Fields in the query can take parameters that control the data we return. query { company(id: "50") { name id pages { name } } } { "data": { "company": { "name": "My Company", "id": "50", "pages": [ {"name": "Page 1"}, {"name": "My Cool Page"} ] } } } Queries GraphQL Is Friendly (Danielle B.-R.) 5 / 11
respectively. Mutations work much the same as queries but are conceptually fairly distinct. Mutations can return values indicating the result of the mutation. mutation RenamePageMutation{ renamePage(id: "22", name: "My New Name") { errors page { id name } } } Mutations GraphQL Is Friendly (Danielle B.-R.) 7 / 11
a recursive dictionary, with defined, typed fields. It is rooted in a top-level query type and a top-level mutation type. Ideally, each level of the schema beyond the root will map to a domain object in the backend. Each field and argument in the schema can have a documentation string associated with it, which is exposed through the API to all potential clients. type Query { myPages: [Page]! page(uuid: String!): Page } type Page { uuid: String! name: String! contents: String! } type Mutation { createPage(name: String): Page } Schemas and Types GraphQL Is Friendly (Danielle B.-R.) 9 / 11
a function, called a resolver, that returns the field value. The resolver has access to the arguments of the field, and also to the value of the parent field. Typically, the GraphQL library will provide default resolvers that just access a field of the same name on the parent value. This execution model is very easy to work with, as often we just need to write down types for the fields on the existing domain object. GraphQL Is Friendly (Danielle B.-R.) 10 / 11
the graph makes it difficult to make optimized database queries from GraphQL. With a simple implementation of the programming model, each non-leaf node will need to make its own query for any child nodes it has. This rapidly leads to making a large number of queries for any kind of deeply-nested structure. This is called the N+1 problem because for a top-level object with N children and children of those children it causes N+1 queries. GraphQL Is Friendly (Danielle B.-R.) 11 / 11