GraphQL Fundamentals: GraphQL Architecture – Clients and Server
Ashwin Ashok
Actively seeking full-time Data Engineer job | Cloud Data Engineering (ETL Data Pipeline Development) - Python, Advanced SQL, PySpark, Databricks, Snowflake, Airflow, dbt, Tableau, AWS | MS CSE @ SUNY - Buffalo
A)?? Use Cases where GraphQL is powerful
1.??? GraphQL server with a connected database
A single web server implements the GraphQL specification. The GraphQL server resolves the query payload, fetches the required information from the database, constructs the response object and returns it to the client.
GraphQL works well with all available transport-layer protocols – TCP, Web Sockets to name few. GraphQL can be used across SQL (AWS Aurora) to NoSQL (MongoDB) databases.
2.??? GraphQL layer integrate with existing systems
Integrating multiple existing systems into a single, extensive GraphQL API. Use case of companies using legacy systems with multiple APIs want to simplify the high maintenance cost.
Legacy systems are complex making it hard to build products around these giant systems. GraphQL can be used to combine these existing systems and hide their complexity with a single GraphQL API. New client appications can request this 1 API to fetch all the required data they need irrespective of data sources format.
3.??? GraphQL with Hybrid system – Database and existing systems
GraphQL fetches data from a database as well as from an existing system allowing flexibility, pushing all complexity on the server and simplifying client end.
B)?? Resolver Functions
Each field in a GraphQL query or mutation corresponds to exactly one function called resolver its main purpose is to fetch the data of that one field. When a query is received on the server, it invokes the resolver functions of all the fields to collect all the data to zip into a format as described and returned to the client.
C)??? GraphQL Client Libraries
GraphQL is super useful for front-end developers taking away inconveniences and shortcomings of REST APIs majoring over- ?and under-fetching.
?Unlike REST APIs, GraphQL uses a declarative approach to fetch data in 2 steps:
1.???? Describe data requirements
2.???? Display data in UI
All networking and way to store of data are abstracted away. Relay (Facebook’s GraphQL client) and Apollo Client (open-source GraphQL client used in major development platforms) are client libraries which does that.
领英推è
A) Clients
Using GraphQL in the frontend leads to develop new abstractions and help implement common functionalities on the client-side.
1.??? Send Queries and Mutations Directly
Abstracting out the networking tasks building the HTTP to load data for an API, with GraphQL we can declare the data requirements, the server resolves the request and responds with the required data.
2.??? View Layer Integrations and UI updates
Based on the platforms and frameworks on the client, UI can be handled using different approaches. With React, GraphQL clients use higher-order components to fetch data and make it available to prop of the components. It goes well with functional reactive programming techniques.
3.???? Caching Query Results
With GraphQL, data is normalized beforehand. The nested query results are flattened and the store will contain only individual records that can be referenced with a globally unique ID.
4.??? Build-time Schema Validation & Optimizations
GraphQL API can include validation and optimize the queries that the client wants at the build-time. Build environment having access to the schema, can parse the GraphQL code, compare it with information from the schema, can help in catching typos and errors beforehand.
B) Server
GraphQL is not only for front-end developers, API itself being implemented on the server-side, it is server developer focus on describing the data available leaving all simple tasks to the client.
1.??? GraphQL execution
Apart from describing schemas and a query language, GraphQL allows to specify the execution algorithm to how queries are transformed into results. The query is traversed field-by-field executing resolvers.
Every field is associated with a type, server extracts the resolvers for the fields. The resolvers are executed hierarchically as specified in the query with running the algorithm for each item in the list one-by-one, put all the results in a correct format and returns it. GraphQL comes with default resolvers not needing to define for every field.
2.??? Batched Resolving
If there is a resolver fetching from a backend API or database, backend can be called many times during execution of one query, GraphQL wraps the fetching function in a utility that will wait for all resolvers to run and fetch each item once. With API supporting batched requests, we can do a single fetch to the backend