Need advice about which tool to choose?Ask the StackShare community!

Django REST framework

1.9K
2K
+ 1
310
GraphQL

33.9K
27.8K
+ 1
310
Add tool

Django REST framework vs GraphQL: What are the differences?

Introduction

Django REST framework and GraphQL are two popular technologies used for building APIs in web applications. While both serve the purpose of creating APIs, they have significant differences in their approach and functionality. In this article, we will highlight the key differences between Django REST framework and GraphQL.

  1. Data Fetching: In Django REST framework, data is fetched using traditional RESTful principles, where each endpoint corresponds to a specific URL and HTTP method. The client explicitly requests the exact data it needs. On the other hand, GraphQL allows the client to specify the shape and structure of the response it expects. The client can request multiple resources and customize the data structure in a single request, reducing the number of round trips required.

  2. Flexibility and Overfetching/Underfetching: Django REST framework follows a predefined structure, where the server defines the structure of the response. This can lead to overfetching (receiving more data than needed) or underfetching (not receiving enough data), especially when dealing with complex data relationships. In contrast, GraphQL gives clients the power to request only the required fields, avoiding overfetching and underfetching issues. It allows clients to precisely define the shape of the response, reducing unnecessary data transmission.

  3. Backend Agnostic: Django REST framework is tightly coupled with Django, a popular Python web framework. It seamlessly integrates with Django models and follows the Django development patterns. GraphQL, on the other hand, is backend-agnostic, which means it can be used with any programming language or framework. This flexibility allows developers to easily integrate GraphQL with their existing backend systems.

  4. Versioning and Evolution: In Django REST framework, versioning APIs is typically done by appending a version number to the URL. This approach can lead to URL proliferation and version management issues. GraphQL, on the other hand, provides a built-in solution for versioning and evolution. The schema-first approach allows adding new fields and deprecating existing fields without breaking the existing queries. Clients can work with a single version of the schema while still benefiting from the capabilities offered by the updated schema.

  5. Caching and Performance: Django REST framework provides built-in support for HTTP caching, allowing clients to cache responses and improve performance. It utilizes HTTP headers like ETags and Last-Modified to control caching behavior. GraphQL, being transport-agnostic, doesn't have built-in support for caching. However, it provides flexibility in implementing caching strategies tailored to specific use cases. Caching in GraphQL can be implemented at various levels, such as per field, query, or operation, based on the nature of the data and client requirements.

  6. Tooling and Ecosystem: Django REST framework has a mature ecosystem and extensive built-in support for common features like authentication, serialization, pagination, and filtering. It provides comprehensive documentation, extensive libraries, and a wide user community. GraphQL, although relatively newer, has been gaining popularity rapidly. It also has a growing ecosystem with tools like relay, Apollo, and GraphiQL. GraphQL offers powerful developer tooling with its introspection capabilities, which allow clients to query the schema and understand the available fields and types.

In Summary, Django REST framework and GraphQL differ in their approach to data fetching, flexibility, backend compatibility, versioning, caching, and tooling. While Django REST framework adheres to REST principles and is tightly coupled with Django, GraphQL provides more flexibility, precise data retrieval, backend-agnostic support, schema evolution capabilities, and versatile caching strategies. Both technologies have their own strengths and are suited for different use cases.

Advice on Django REST framework and GraphQL
kristan-dev
Senior Solutions Analyst · | 8 upvotes · 375.6K views

My journey to developing REST APIs started with Flask Restful, and I've found it to be enough for the needs of my project back then. Now that I've started investing more time on personal projects, I've yet to decide if I should move to use Django for writing REST APIs. I often see job posts looking for Python+Django developers, but it's usually for full-stack developers. I'm primarily interested in Data Engineering, so most of my web projects are back end.

Should I continue with what I know (Flask) or move on to Django?

See more
Replies (1)
Rafael Torres
Technical Lead at 4Agile · | 9 upvotes · 365.7K views

If you want to be a Web developer with knowledge in another frontend and NoSql technology, maybe continue with Flask. However, if you want to create very fast solutions to grow up with a new business and merge these with data analysis and other tools, Django is the answer. Basically read more about the service architecture where you feel more comfortable, Microservice or Monolithic, but please will not married with any because they solve issues to different contexts.

See more
Saurav Pandit
Application Devloper at Bny Mellon · | 6 upvotes · 320.8K views

I have just started learning Python 3 weeks ago. I want to create a REST API using python. The API will be used to save form data in an Oracle database. The front end is using AngularJS 8 with Angular Material. In python, there are so many frameworks to develop REST APIs.

I am looking for some suggestions which REST framework to choose?

Here are some features I am looking for:

  • Easy integration and unit testing, like in Angular. We just want to run a command.

  • Code packaging, like in java maven project we can build and package. I am looking for something which I can push in as an artifact and deploy whole code as a package.

  • Support for swagger/ OpenAPI

  • Support for JSON Web Token

  • Support for test case coverage report

Framework can have features included or can be available by extension. Also, you can suggest a framework other than the ones I have mentioned.

See more
Replies (1)
Recommends
on
FlaskFlask
at

For starters flask provides a beautiful and easy way to create REST APIs. Also its supported by excellent beginner docs as well as a very active community. Another good thing with Flask is its widely available list of plugins which allow you to build as you go. Its also good in performance and can scale to a quite decent level. However, if you are sure your project is going to be fairly big, it would be better to start with Django as it provides a lot of features out of the box and is extremely stable in performance. Both these frameworks have support for Swagger, JWT, Coverage Report although you have to install plugins for them. Deploying both of these are fairly simple and there is huge documentation available. Django has one of the best documentations I have come across. I hope I was able to answer your queries.

See more
Decisions about Django REST framework and GraphQL
Shiqi Lin
Software Developer at BigClarity · | 3 upvotes · 228.4K views

We will use Django to set up our backend and Django REST Framework (DRF) for our API creation. The easiness of performing development tasks (eg. user authentication, URL routing, and schema migration) attracts our attention. Also, Django with PostgreSQL provides many benefits: 1. Some data types in Django will only work with PostgreSQL; 2. Django offers django.contrib.postgres to operate on PostgreSQL; 3. Django supports many features of PostgreSQL. Moreover, Django is compatible with Redis.

See more
Manage your open source components, licenses, and vulnerabilities
Learn More
Pros of Django REST framework
Pros of GraphQL
  • 66
    Easy to use
  • 65
    Browsable api
  • 53
    Great documentation
  • 50
    Customizable
  • 42
    Fast development
  • 9
    Easy to use, customizable, pluggable, serializer
  • 8
    Python
  • 7
    Django ORM
  • 5
    FastSerialize
  • 3
    Less code
  • 2
    Easy implementation
  • 75
    Schemas defined by the requests made by the user
  • 63
    Will replace RESTful interfaces
  • 62
    The future of API's
  • 49
    The future of databases
  • 13
    Self-documenting
  • 12
    Get many resources in a single request
  • 6
    Query Language
  • 6
    Ask for what you need, get exactly that
  • 3
    Fetch different resources in one request
  • 3
    Type system
  • 3
    Evolve your API without versions
  • 2
    Ease of client creation
  • 2
    GraphiQL
  • 2
    Easy setup
  • 1
    "Open" document
  • 1
    Fast prototyping
  • 1
    Supports subscription
  • 1
    Standard
  • 1
    Good for apps that query at build time. (SSR/Gatsby)
  • 1
    1. Describe your data
  • 1
    Better versioning
  • 1
    Backed by Facebook
  • 1
    Easy to learn

Sign up to add or upvote prosMake informed product decisions

Cons of Django REST framework
Cons of GraphQL
  • 2
    Bad documentation
  • 2
    Reimplements Django functionality
  • 1
    No support for URL Namespaces
  • 0
    Bad CSRF handling
  • 4
    Hard to migrate from GraphQL to another technology
  • 4
    More code to type.
  • 2
    Takes longer to build compared to schemaless.
  • 1
    No support for caching
  • 1
    All the pros sound like NFT pitches
  • 1
    No support for streaming
  • 1
    Works just like any other API at runtime
  • 1
    N+1 fetch problem
  • 1
    No built in security

Sign up to add or upvote consMake informed product decisions

What is Django REST framework?

It is a powerful and flexible toolkit that makes it easy to build Web APIs.

What is GraphQL?

GraphQL is a data query language and runtime designed and used at Facebook to request and deliver data to mobile and web apps since 2012.

Need advice about which tool to choose?Ask the StackShare community!

What companies use Django REST framework?
What companies use GraphQL?
Manage your open source components, licenses, and vulnerabilities
Learn More

Sign up to get full access to all the companiesMake informed product decisions

What tools integrate with Django REST framework?
What tools integrate with GraphQL?

Sign up to get full access to all the tool integrationsMake informed product decisions

Blog Posts

GitHubDockerReact+17
41
37605
GitHubPythonReact+42
49
40987
GitHubPythonNode.js+47
55
72884
PythonDjangoDjango REST framework+3
6
5731
What are some alternatives to Django REST framework and GraphQL?
Django
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Flask
Flask is intended for getting started very quickly and was developed with best intentions in mind.
Tastypie
Tastypie is a webservice API framework for Django. It provides a convenient, yet powerful and highly customizable abstraction for creating REST-style interfaces.
Swagger UI
Swagger UI is a dependency-free collection of HTML, Javascript, and CSS assets that dynamically generate beautiful documentation and sandbox from a Swagger-compliant API
Graphene
Graphene is a Python library for building GraphQL schemas/types fast and easily.
See all alternatives