Friday, October 16, 2020

GraphQL Introduction

After being publicly released by Facebook in 2015 GraphQL Specification has seen a staggering adoption in the software industry. Below are some facts about it:

  • GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data. GraphQL was introduced internally at Facebook in 2012 to address the issue with RESTful APIs around over-fetching/under-fetching of data for their mobile application.
  • GraphQL Spec has many implementations which open-source-community created. This was after first RecatJS implementation was created by Facebook. Implementations are present for languages such as: Javascript/Typescript, Go, Ruby, Scala, Elixir, Python, Java. Few popular implementations are Apollo, AWS AppSync, Prisma, Hasura, etc. With this increasing adoption, GraphQL Tooling & Ecosystem has also seen a huge surge.
  • GraphQL has 3 request types: queries (for getting data from the API), mutations (for changing data via the API), and subscriptions (long-lived connections for streaming data from the API).
  • GraphQL is Typed. Contrary to REST; the GraphQL does runtime datatype checking & sub-selection to limit fields returned.
  • GraphQL schemas are composed of Resolvers. That means data can be pulled from anywhere/any source-system independently. A resolver is a function that resolves a value for a type or field in a schema. Resolvers can be asynchronous too! A resolver function takes four arguments (in that order):
    • 1. parent: The result of the previous resolver call (more info).
    • 2. args: The arguments of the resolver’s field.
    • 3. context: A custom object each resolver can read from/write to.
    • 4. info: Contains the query AST and more execution information. In other words it contains, Meta-data about the request.
  • GraphQL can return multiple resources in one round trip to server.
  • GraphQL ensures no over-fetching as well as no under-fetching. API Clients are in full control about what they want to query. If some client wants a single field to query or make an update then it can do so without need of additional API Endpoints or a new version of the same API.
  • GraphQL is Introspectable. GraphiQL Tool exploits exactly that to provide client generation, query generation, suggestions & documentation. Think of it as Java Reflection.
  • One of the reasons customers choose GraphQL is the power of Subscriptions. These are notifications that are sent immediately to clients when data has changed by a mutation. AppSync subscriptions are implemented using Websockets, and are directly tied to a mutation in the schema. The AppSync SDKs and AWS Amplify Library allow clients to subscribe to these real-time notifications. AppSync Subscriptions have many uses outside standard API CRUD operations. They can be used for inter-client communication, such as a mobile or web chat application. Subscription notifications can also be used to provide asynchronous responses to long-running requests. The initial request returns quickly, while the full result can be sent via subscription when it’s complete (local resolvers are useful for this pattern).
  • While there's nothing that prevents a GraphQL service from being versioned just like any other REST API, GraphQL takes a strong opinion on avoiding versioning by providing the tools for the continuous evolution of a GraphQL schema.

No comments: