Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GraphQL Is Friendly

GraphQL Is Friendly

A brief introduction to the basics of GraphQL

Danielle Brook-Roberge

January 29, 2021
Tweet

More Decks by Danielle Brook-Roberge

Other Decks in Programming

Transcript

  1. What is GraphQL? GraphQL is a system for accessing backend

    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
  2. Why is it friendly? Unlike REST, GraphQL has a mandatory,

    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
  3. In GraphQL, we send the server the shape of the

    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
  4. query { page(id: "10") { name company { name pages

    { name } } } } { "data": { "page": { "name": "My Cool Page", "company": { "name": "My Company", "pages": [ {"name": "Page 1"}, {"name": "My Cool Page"} ] } } } } The Graph in GraphQL GraphQL Is Friendly (Danielle B.-R.) 6 / 11
  5. GraphQL separates non-mutating and mutating effects into queries and mutations,

    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
  6. The schema of a GraphQL API has the form of

    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
  7. Resolvers Each field in the GraphQL schema is associated with

    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
  8. The N+1 Problem The recursive execution of the resolvers in

    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