Skip to content

GraphQL

Hugh Gardiner edited this page Jun 5, 2019 · 2 revisions

Comparison to REST

  • Both GQL and REST send data over HTTP
  • GraphQL is client and schema driven whereas REST is more server driven
  • GraqhQL client specifies response body whereas REST has static server defined responses
  • REST to GraphQL Operations
    • GET -> Query
    • POST, PUT, UPDATE, DELETE -> Mutation
    • GraphQL also includes Subscription operation for pub/sub

Benefits/problems it solves

  • Overfetching and Underfetching
    • Client has the ability to specify desired fields rather than always receiving a static response from a REST endpoint
    • Solves the problem of mobile applications receiving too much data over the wire
  • Fewer requests needed to get all necessary information
  • Useful in rapidly changing data environments
  • Easily readable queries
  • Subscription data pushed from the server useful when your app needs to know about live events/updates
  • GraphQL Caching built into Apollo Client
  • Complexity of system is hidden by GraphQL
  • Allows for Alias of fields
  • GraphQL has a GUI Playground for interacting with the integrated server
  • Places power in the consumer to decide on the information they want
  • Easily allows for extraction of business logic out of frontend

Ease of setup

  • Due to the single endpoint implementation, server code only needs to expose a single route
  • Apollo client can be adopted incrementally
    • Encouraged to adopt holistically
    • GraphQL can be added as a gateway on top of an existing RESTful/database implementation

Drawbacks

  • Single endpoint prevents native HTTP caching
  • Web caching is rather complex
    • Relies on GraphQL client to implement caching
  • Queries always return HTTP code 200
  • Queries can become rather complex and lead to some performance issues
  • Adds complexity to apps that may otherwise be simple
  • File uploading not recommended
    • Can use Base64 encoding

Learning Curve/Migration

  • Learning curve is certainly higher than REST
    • Operation syntax is different
    • Resolver implementation becomes complex as data complexity increases
    • Requires different mindset when thinking about schema and resolver implementation
  • However, there is extensive amount of documentation
  • Apollo client
  • Large adoption by community = easy to google answers

Choosing between GraphQL vs. REST

  • Choose REST if:
    • Very static data structure that never changes
    • Complex queries are more manageable in REST
    • A need for easy caching of data
    • Heavily based on file uploading
  • Choose GraphQL if:
    • Needs of the client drive the server implementation
    • Clients are very mobile heavy
    • Desire to minimize number of API calls per request
    • Development process is quicker after learning curve especially when frequent changes in requirements