Technical Interview Handbook

Technical Interview Handbook

  • Notes
  • About Me

›Concepts

Introduction

  • Getting Started

Best Practices

  • Spring Project

Messaging

    Basics

    • Messaging Guide

    Rabbit MQ

    • Rabbit MQ Guide

Logging

  • ELK Guide

Concepts

  • GraphQL vs Rest

ORM

  • Hibernate Mapping Guide

Database

  • Mongo DB Guide

Enterprise Search

  • Apache Solr Guide

UI

  • ReactJS Guide

Build Tool

  • Maven Guide

Testing

  • Junit5 & Mockito Guide

CI/CD

  • Docker Desktop Installation
  • Docker Guide

About Me

  • About Me

GraphQL vs Rest

Protocol Transition

YearProtocol
1981RPC(Remote Procedure Call)
1991CORBA(Common Object Request Broker Architecture)
1998SOAP(Simple Object Access Protocol
2000REST(REpresentation State Transfer)
2012GraphQL(Graph Query Language)

GraphQl

  • Query language for APIs
  • Dynamic fulfilling of queries with existing data
  • Single endpoint of POST with query
  • Customized data fetching : Reducing the need for unrelevant data as opposed to REST static endpoints sending huge response
  • Easier to evolve over time with developer tools
  • Any UI Change is faster addressed to with GraphQL approach

History

  • Developed since 2012 by Lee Brown(Facebook) due to data-fetching needs
  • Published(Open sourced) in 2015
  • Currently under Linux Foundation umbrella

REST vs GraphQL Approaches

RESTGraphQL
Considers everything as resources and fetches themConsiders everything as connected graph
Requires multiple round trips if data to be fetched from different endpointsA single POST API call with differnt queries solves the problem
Over and under fetching of dataCustomized data fetching
New endpoint needed for different resource with custom dataNo new endpoint needed. Only server needs to handle such requests
Good with simple applicationsNot good with simple applications as types,queries & resolvers need to be defined
Simple error handlingComplex error handling
Entry point into data is multiple endpointsList of fields & Mutation types is the entry point

Query Example

  • HTTP GET : http://myapi/graphql?query={me{name}}
{
  me {
    name
  }
}
  • HTTP POST
{
  "query": "...",
  "operationName": "...",
  "variables": { "myVariable": "someValue", ... }
}
  • Response
{
  "data": { ... },
  "errors": [ ... ]
}

Expectations with GraphQL

  • Both frontend and backend needs to know data structure. But only backend will work with database
  • HTTP Specification for Caching at network layer not followed as in REST. So, needs Caching of data. Caching enables offline data loading

Common Use Cases for GraphQL

  • Different I/Os(Mobile, Website, Echo, Tab, etc.) needing different set of attributes
  • GraphQl Client invoking API Gateway. While API Gateway invoking REST Applications

Best Practices to follow with GraphQL Client

  • Loading current usable component before the other components which would be needed later for rendering, thereby reducing load time
  • Data attribute nomenclature must be unique. No two table columns must have same names to remove amiguity
  • Persisted ID & variable caching so as to enable offine loading
  • Storing common data in global store(Eg With Redux Global Store with React.js)
  • Authorizing users to access specific data based on user_type in API must be properly defined using access control with GraphQL

Limitations of GraphQL

  • Limited tools eg. API Analytics
  • REST endpoint dominates in 3rd party tools which would be needed for project building

Apollo Platform

  • Implementation of GraphQl which transfers data between cloud(Server) to frontend app(Client)
  • Popular choice of using GraphQL in Javascript based apps

In memory Data Caching

  • Multiple data fetch for the same request can be effectively solved by use of Facebook's DataLoader Library
  • Implementation Steps
  • Migration from Solr to GraphQL

Conclusion

  • Meticulous analysis of application needs to be done to decide which one to go ahead with REST or GraphQL
  • Play around the GraphQL playgrounds available online and finalize which one to proceed with

References

  • https://graphql.org/code/
  • Differences between both
  • Best Practices for GraphQL
  • Access Control with GraphQL
  • Access Control Implementation Example
  • Authorization ways with Apollo
← ELK GuideHibernate Mapping Guide →
  • Protocol Transition
  • GraphQl
  • History
  • REST vs GraphQL Approaches
  • Query Example
  • Expectations with GraphQL
  • Common Use Cases for GraphQL
  • Best Practices to follow with GraphQL Client
  • Limitations of GraphQL
  • Apollo Platform
  • In memory Data Caching
  • Conclusion
  • References
Technical Interview Handbook
Docs
Getting Started (or other categories)Guides (or other categories)API Reference (or other categories)
Community
User ShowcaseStack OverflowProject ChatTwitter
More
BlogGitHubStar
Copyright © 2020 anupama-sinha