GraphQL Vs RestAPI
GraphQL Vs RestAPI
GraphQL
vs
REST API
Know the
difference!!
swipe
brijpandeyji
GraphQL
GraphQL is a query language for
your API, as well as a runtime for
executing those queries against
your data.
Developed
Developed byby Facebook
Facebook and
and
released
released in
in 2015
2015
REST API
It is an architecture style to
develop web applications. It uses
HTTP protocol as a
communication interface.
Introduced
Introduced by
by Roy
Roy Fielding
Fielding
in
in 2000
2000
The biggest difference between GraphQL and REST is the manner in which data
is sent to the client. In a REST architecture, the client makes an HTTP request
and data is sent as an HTTP response, while in GraphQL, the client requests data
with queries.
swipe
brijpandeyji
Structure
2
Flexibility
3
Efficiency
4
Caching
5
Data Fetching
6
API Evolution
7
Versioning
swipe
brijpandeyji
Structure Based on
endpoints, request
and response
GraphQL
GraphQL APIs, have a single
endpoint that is used to query,
mutate, and subscribe to data.
swipe
brijpandeyji
Flexibility
GraphQL
GraphQL allows clients to request
exactly the data they need, in a
single request.
More Flexible
REST API
REST APIs are typically more rigid
and require multiple requests to
retrieve all the data needed for a
particular use case.
Less Flexible
swipe
brijpandeyji
Efficiency
GraphQL
GraphQL can be more efficient
than REST because it allows the
client to request all the data it
needs in a single request.
REST API
It is less efficient than GraphQL as
it may need to make multiple
requests to different endpoints to
retrieve the data needed.
swipe
brijpandeyji
Caching
It is a technique used to improve the
performance of an API by reducing
the number of requests made to the
server.
GraphQL
In GraphQL, the response is
tailored to the specific request, so
it is more difficult to cache.
REST API
REST APIs can be cached more
easily, as the response from a
specific endpoint will always be
the same.
Note
While
While caching
caching can
can improve
improve performance,
performance, itit can
can also
also lead
lead to
to
outdated
outdated or
or stale
stale data
data being
being served
served to
to clients
clients ifif the
the cache
cache is
is
not
not properly
properly invalidated
invalidated or
or refreshed.
refreshed.
swipe
brijpandeyji
Data Fetching
GraphQL
With GraphQL, the client can
request exactly the data it needs,
reducing the amount of data
transferred over the network.
REST API
REST APIs can sometimes suffer
from over fetching or under
fetching, where the client receives
more or less data than it needs.
Note
swipe
brijpandeyji
API Evolution
GraphQL
In GraphQL, the API can evolve
more flexibly, as the client can
request only the fields it needs
and ignore the rest.
REST API
In a REST API, adding a new field
or endpoint requires a new
version of the API. This makes API
evolution harder.
swipe
brijpandeyji
Versioning
GraphQL
GraphQL APIs do not require
versioning because the client can
request any combination of fields.
REST API
REST APIs typically require
versioning when the shape of the
data changes.
swipe
brijpandeyji
Conclusion
Overall
GraphQL
GraphQL and
and REST
REST are
are both
both
useful
useful tools
tools for
for building
building APIs,
APIs,
and
and which
which one
one you
you choose
choose
depends
depends on
on the
the specific
specific needs
needs
of
of your
your project.
project.