Graphql - jaredgorski.org

Back to notes

Graphql

Keywords: data, api, graph, query, facebook, data manipulation, types, server
Date:
  • An approach to developing web APIs that allows for requesting and receiving data in a particular shape/structure
  • allows for more efficient requests since extraneous/unused data is eliminated from the API response (avoids the "over-fetching" problem in REST)

Sample query:

{
  orders {
    id
    productsList {
      product {
        name
        price
      }
      quantity
    }
    totalAmount
  }
}

Sample response:

{
  "data": {
    "orders": [
      {
        "id": 1,
        "productsList": [
          {
            "product": {
              "name": "orange",
              "price": 1.5
            },
            "quantity": 100
          }
        ],
        "totalAmount": 150
      }
    ]
  }
}
  • queries are handled by a GraphQL server
  • relies on a type system (which allows for custom types/structs) in order to execute a query
    • the most basic types are "scalar" values, which are basic to GraphQL and non-custom
    • all fields end up resolving to scalar values (any custom types are ultimately resolved into their parts, which are scalars like strings, numbers, etc.)
  • each field on each type can be thought of as a function (resolver), provided by the developer, which produces the value

Backlinks: