django graphene without model

To use Django Graphene without a model, follow these steps:

  1. Install Django and Graphene: Make sure you have Django and Graphene installed in your project. You can use pip to install both packages:
pip install django graphene
  1. Create a GraphQL Schema: In your Django project, create a new file called schema.py (or any other name you prefer) to define your GraphQL schema. Import the necessary classes from Graphene:
from graphene import ObjectType, String, Schema
  1. Define a Query class: Create a new class that inherits from ObjectType. This class will represent your GraphQL query type. Inside the class, define the fields that you want to expose in your API. Each field should be an instance of a Graphene type, such as String:
class Query(ObjectType):
    hello = String()

    def resolve_hello(self, info):
        return "Hello, world!"
  1. Create a Schema: Instantiate a Schema object and pass your Query class as an argument. This will create the GraphQL schema for your API:
schema = Schema(query=Query)
  1. Test the API: You can now test your API by executing GraphQL queries. In your Django project, create a new view that handles GraphQL requests. Import the necessary classes and functions:
from django.http import HttpResponse
from graphene_django.views import GraphQLView
  1. Configure the URL: In your project's urls.py, add a URL pattern for your GraphQL view:
urlpatterns = [
    # ...
    path("graphql", GraphQLView.as_view(graphiql=True)),
    # ...
]
  1. Start the server: Run your Django server and navigate to the GraphQL endpoint in your browser. By default, it will be http://localhost:8000/graphql. You should see the GraphiQL interface, where you can test your API by executing queries.

That's it! You have successfully set up Django Graphene without using a model. You can now define more fields and types in your Query class to build a more complex API.