DEV Community

Cover image for What is GraphQL
Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

What is GraphQL

This article was originally published on bmf-tech.com.

What is GraphQL

  • Developed by Facebook
  • A query language for APIs
    • User-friendly because the data format of API requests and responses are similar
  • REST is an architecture (design), while GraphQL is a language (DSL)

Comparison of REST API and GraphQL

REST API Format

Send requests to endpoints using HTTP verbs

curl https://api.bmf-tech.com/v1/configs

[
    {
        "id": 1,
        "name": "title",
        "alias_name": "Title",
        "value": "bmf-tech",
        "created_at": "2017-09-25 23:08:23",
        "value": "bmf-tech",
        "deleted_at": null
    }
]
Enter fullscreen mode Exit fullscreen mode

GraphQL API Format

Send queries to a single endpoint

curl https://api.bmf-tech.com/api

configs {
  id,
  name,
  alias_name
  value,
  created_at,
  updated_at,
  deleted_at
}
Enter fullscreen mode Exit fullscreen mode
[
    {
        "id": 1,
        "name": "title",
        "alias_name": "Title",
        "value": "bmf-tech",
        "created_at": "2017-09-25 23:08:23",
        "value": "bmf-tech",
        "deleted_at": null
    }
]
Enter fullscreen mode Exit fullscreen mode
REST API GraphQL
Endpoints Multiple Single
HTTP Verbs Dependent Independent
Type System None Available
Versioning Required Yes No
Documentation Required Yes No
Resource Limitation Mainly call count Handled according to resource amount
  • Flexibly specify the desired data for a single endpoint

    • REST has fixed response data for each endpoint.
    • GraphQL allows specifying the desired data for a single endpoint to get the response data.
  • Resource limitation requires ingenuity

    • Handled according to resource amount
    • Need to consider methods for load calculation, such as based on the number of objects
  • Almost no need for documentation

    • API definition serves as documentation
    • The query structure and response data structure are almost the same

Points of Concern

  • Dependency on libraries

    • Requires libraries for parsing queries
  • Not necessarily better performance than REST API

    • Reduces the number of requests
    • Increases the amount of data per request
    • Need to control data volume in both REST API and GraphQL (e.g., pagination, field specification)
  • Monitoring

    • REST API can be monitored per endpoint
    • GraphQL is a single endpoint, making it difficult to monitor response performance per query. Some measures are needed.
    • Wait for ecosystem maturity or implement in-house
  • Caching

    • Cannot use HTTP cache
    • Should investigate various other aspects

Impressions

  • In applications with many components and complex UIs, where the number of requests increases and the client side struggles, there may be benefits to introducing it.
  • Considered using it with Rubel, but decided against it as it feels premature. Currently unnecessary.

References

Top comments (0)