05. GraphQL

RESTful API Limitations

Endpoints are often designed to match a client. But clients get updated endpoints may not; this may lead to over-fetching or under-fetching.

Over-fetching: download more data than you need. e.g. need list of usernames, but get list of users and all available fields

Under-fetching: download less data than you need. e.g. want information about friends; first fetch list of friend IDs, then make request to get user data about each friend.

GraphQL

A specification for how you specify data (c.f. strong typing) and how you query it.

Example:

type TypeName {
  nonNullString: String!,
  nullArray: [String]
}

// String is a *scalar* type; a base type

All GraphQL queries return a 200 response code (unless there is a network error). Errors are returned in user-defined fields.

GraphQL:

Queries are in a JSON-like structure e.g.

{
  assets(10) {
    id,
    url,
    nestedObject: {
      prop1,
      prop2
    }
  }
}

Automated Testing

Mocha and Chai