Django meets GraphQL: A Powerful Combination
- Assumption:
- I’m assuming that y’all have some basic knowledge about Graphql & Django.
- If not then kindly go through below link:
- Graphql: https://graphql.org/learn/
- Django:https://www.djangoproject.com/
- Introduction
- GraphQL is an open-source query language used to communicate data between the client and the server.
- In this document our focus will be on integrating a GraphQL API into a Django project and effectively using it to query data as per our requirements.
- ALWAYS FIRST STEP IS TO Set environment:
- Create Virtual Environment
- Enter the following command to create a new virtual environment with the name “myenv” (you can replace “myenv” with a your environment name):
- python -m venv myenv
- Once the virtual environment is created, you can activate it by entering the following command.
- source myenv/bin/activate
- Installation [ Django & Graphene-django ]
- First install django
- pip install django
- Then install graphene-django library
- pip install graphene-django
- Check requirements & freeze it into relevant file.
- Run command to freeze your requirements.txt file:-
- pip freeze > requirements.txt
- It will create requirements.txt file with all installed packages directory
- Your requirements.txt look like as below:
- Create django development project and relevant app in your project and configure with settings.py
- Create models in model.py for the project in your app.
- In the above image we create two models Class Category and Book. and added fields for the model Classes.
- In the Book model we have added ‘-date_created’ in Meta class for orders according to the date they were created at.
Let’s register models in the app/admin.py file
- Now run migrations to add our model in the database.
- python manage.py makemigrations
- python manage.py migrate
- Once migration is done, run python manage.py runserver if the app is displayed on your browser, then you are on the right track.
Integrating GraphQL into our project
- We will integrate GraphQL into our Django project.
- Now, Add graphene_django to INSTALLED_APPS in your settings.py file
Create schema
- GraphQL is a query language with a powerful type system that can be used to define an API’s data structures. The GraphQL Schema is used to represent this information.
- A schema is a contract between the client and the server that describes how the client can acquire the database.
- You’ll need to add a Schema, Object Type and view function that receives the GraphQL queries to be able to perform GraphQL queries in your web application.
- Let’s define our schema in the app directory. We’ll create a file called schema.py.
- Add above image code in schema.py file.
- We create schema for our two models (book and category). CategoryType and BookType is schema.
- We also included DjangoObjectType: which uses GraphQL to display all fields on a model.
- In ‘graphene.List’ List is object type we can use (nonNull, String, Enum)
- class Query: inherits from ‘graphene.ObjectType’ and provides the setting for our graphql queries.
- resolve_category , resolve_books are used to open up categories, books querysets. These methods take two parameters (root and info).
- graphene.Schema: this query brings in data from our type(database).
Conclusion:
In conclusion, Django and GraphQL are a powerful combination that can provide developers with a flexible and efficient way to build APIs. While Django provides a robust and scalable backend framework, GraphQL offers a query language that allows for more efficient and precise data retrieval.
By implementing GraphQL in a Django project, developers can take advantage of features such as declarative data fetching, schema introspection, and type validation, among others. This can lead to faster and more reliable development, as well as better performance and user experience.
Overall, the combination of Django and GraphQL offers a great solution for developers who want to build robust and efficient APIs. By leveraging the strengths of both technologies, developers can create applications that are not only scalable and maintainable but also provide a great user experience.
Originally published by: https://www.inexture.com/django-meets-graphql/
Comments
Post a Comment