GraphQL - alexanderteplov/computer-science GitHub Wiki
GraphQL
Main traits
It's database agnostic.
It's transport agnostic.
It's a client-server RPC-like architecture:
On a server, we define a function and specify in our client a request pointing to this function and providing its input. In response, we get its output.
The main difference with RPC is the capability of invoking several nesting functions per one request. So in one roundtrip, we can receive data we usually should make several requests for.
It's strongly typed. Works with statical analyzers. Compatible with TypeScript.
Known problems
Authorization. Not implemented from the box. It's not evident how to implement it properly. As for identification, JWT is a common choice.
N+1 problem. With nesting, requests may affect performance getting information by separate requests to the DB. To overcome, it may use the DataLoader utility. DataLoader batches requests and caches the results.
API complexity. Bad GraphQL API is much worse than a bad REST API. It's a great effort to build a good one.
Bundle size. GraphQL requests include in the bundle.
Versioning. There is no out-of-the-box solution for it.
Components of a GraphQL web service
Network transport system. The most often (and best provided with tools) it's the HTTPS.
Network server. Mostly it's an HTTP server, and the most common is Express (e.g., Apollo Server uses it).
GraphQL server. All customs servers are built over the original GraphQL.js Server. Example: Apollo Server.
GraphQL client. You may not use any of a client and send all the queries and mutations by hand, but it's a pain. The most known and feature-rich clients are Relay and Apollo Client.
Terms
Types - the way GraphQL describes any entities as objects in its own Type language. It gives us: scalar, enumeration, lists, union and input types, interfaces. And two special types: Query and Mutation types.
Queries - messages from a client to a server to ask about information. A client specifies fields it wants, their hierarchy, and values. For example: query { stuff { eggs shirt pizza }}.
Mutations - another type of messages from a client to a server. Unlike queries, they aren't for asking about information but for making changes, mutations (in a DB, cache, or anywhere).
Resolvers - they are functions matching fields described in a query with someplace and a way to get them (e.g., make an SQL request to a DB). Or the same way deal with mutations received from a client.
Schema - the Schema links all types and resolvers together, documenting for both developers and GraphQL packages your API.
Good client features
Dealing with a network (handling requests)
Storing data and accessing them
Normalizing data keeping in a store (for deduplication and atomic usage)
Pub/sub mechanism for synchronization data between a store and interface components
Immediate applying changes to an interface with restoring previous when failed