วันพฤหัสบดีที่ 16 พฤษภาคม พ.ศ. 2562

GraphQL


  • Query language and runtime for web APIs. 
  • Developed by FB.
  • While typical REST APIs require loading from multiple URLs, GraphQL APIs get all the data your app needs in a single request. Apps using GraphQL can be quick even on slow mobile network connections.
  • A GraphQL server can process a client query using data from separate sources and present the results in a unified graph.
  • GraphQL is a data query and manipulation language that allows specifying what data is to be retrieved ("declarative data fetching") or modified. 
  • https://graphql.org/
  • Graphql is a middleware as it can hide heterogeneous data sources like using RDB, Nosql, S3 together from frontend. This is also called database-diagnostic.
  • Why People Use GraphQL for Relational Data Relational databases are built on relationships (Foreign Keys), and GraphQL is built on graphs.
  • In a REST API, getting a user, their posts, and the comments on those posts might require three different API calls. In GraphQL, you write one query, and the backend handles the relational complexity in one go.
  • When you use it with a relational database, the process typically looks like this:  

    1. The Schema: You define a GraphQL schema that mirrors your database tables (e.g., User, Post, Comment).

    2. The Resolvers: You write "resolver" functions. When a user asks for a User, the resolver runs a SQL query like SELECT * FROM users WHERE id = 1.  

    3. The Mapping: If a user asks for a User and their Posts, the resolver handles the "Join" logic, fetching the related data from the posts table.