Git Product home page Git Product logo

django-graphql-starter_pack's Introduction

Django GraphQL Project README

This README.md file provides instructions for setting up a Django project with GraphQL using the provided code snippets.

Prerequisites

Before starting, ensure you have the following installed on your system:

  • Python 3
  • Pip (Python package manager)

Project Setup

  1. Create a virtual environment:

    python3 -m venv venv
  2. Activate the virtual environment:

    source venv/bin/activate
  3. Install Django:

    pip install django
  4. Install GraphQL support using Graphene-Django:

    pip install graphene-django
  5. Create a new Django project and an app:

    django-admin startproject core .
    python3 manage.py startapp books
  6. Add the 'books' and 'graphene_django' app to INSTALLED_APPS in core/settings.py:

    # core/settings.py
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'graphene_django', # Add 'graphene_django' here
        'books',  # Add 'books' here
    ]
  7. Define a model in books/models.py:

    # books/models.py
    from django.db import models
    
    class Book(models.Model):
        title = models.CharField(max_length=100)
        excerpt = models.TextField()
    
        def __str__(self):
            return self.title
  8. Create database tables and apply migrations:

    python3 manage.py makemigrations
    python3 manage.py migrate
  9. Create a GraphQL schema file:

    touch books/schema.py
  10. Populate books/schema.py with GraphQL schema definitions:

    # books/schema.py
    import graphene
    from graphene_django import DjangoObjectType
    from .models import Book
    
    class BookType(DjangoObjectType):
        class Meta:
            model = Book
            fields = ("id", "title", "excerpt")
    
    class Query(graphene.ObjectType):
        allBooks = graphene.List(BookType)
    
        def resolve_allBooks(root, info):
            return Book.objects.all()
    
    schema = graphene.Schema(query=Query)
  11. Update core/urls.py to include the Books endpoint:

    # core/urls.py
    from django.contrib import admin
    from django.urls import path, include
    
    urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('books.urls'))
    ]
  12. Update books/urls.py to include the GraphQL endpoint:

    # core/urls.py
    from django.urls import path
    from graphene_django.views import GraphQLView
    from books.schema import schema
    
    urlpatterns = [
        # ...
        path("graphql", GraphQLView.as_view(graphiql=True, schema=schema)),
    ]
  13. Register the Book model in core/admin.py:

    # core/admin.py
    from django.contrib import admin
    from books.models import Book
    
    admin.site.register(Book)
  14. Create a superuser for the admin interface:

    python3 manage.py createsuperuser
  15. Visit http://localhost:8000/admin in your browser and log in with the superuser credentials you created.

  16. Add few books from the admin interface:

  17. Start the development server once more:

    python3 manage.py runserver
  18. Visit http://localhost:8000/graphql and play with queries

Now, you have set up a Django project with GraphQL support. You can use the GraphQL endpoint at http://localhost:8000/graphql to interact with your data. The Book model is available in the admin interface for data management.

django-graphql-starter_pack's People

Contributors

mpatwa98 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.