header for authentication in django and graphql

header for authentication in django and graphql


0

{

"Authorization": "JWT token"

}

and in graphQL my query is :

query{

allStudents{

name

roll

}

}

but stil does’t work for me

I am expecting to secure my api end point because all will create tokenauth then can access data

import graphene

from graphene_django.types import DjangoObjectType

import graphql_jwt

from .models import Student

from graphql_jwt.shortcuts import get_token

from graphql_jwt.decorators import login_required

Define the GraphQL type for the Student model

class StudentType(DjangoObjectType):

    class Meta:

     model = Student

class Query(graphene.ObjectType):

# Query to retrieve all students

all_students = graphene.List(StudentType)


@login_required

def resolve_all_students(self, info):

    return Student.objects.all()


# Query to retrieve a single student by ID

student = graphene.Field(StudentType, id=graphene.Int())


def resolve_student(self, info, id):
    try:
        return Student.objects.get(pk=id)

    except Student.DoesNotExist:

        return None

class CreateStudent(graphene.Mutation):

class Arguments:

    name = graphene.String()

    roll = graphene.String()

student = graphene.Field(StudentType)

def mutate(self, info, name, roll):

    student = Student(name=name, roll=roll)

    student.save()

    return CreateStudent(student=student)

class UpdateStudent(graphene.Mutation):

class Arguments:

    id = graphene.Int()

    name = graphene.String()

    roll = graphene.String()

student = graphene.Field(StudentType)

def mutate(self, info, id, name, roll):

    try:

        student = Student.objects.get(pk=id)

        student.name = name

        student.roll = roll

        student.save()

        return UpdateStudent(student=student)

    except Student.DoesNotExist:

        return None

class DeleteStudent(graphene.Mutation):

class Arguments:

    id = graphene.Int()

success = graphene.Boolean()

def mutate(self, info, id):

    try:

        student = Student.objects.get(pk=id)

        student.delete()

        return DeleteStudent(success=True)

    except Student.DoesNotExist:

        return DeleteStudent(success=False)

class Mutation(graphene.ObjectType):

create_student = CreateStudent.Field()

update_student = UpdateStudent.Field()

delete_student = DeleteStudent.Field()

token_auth = graphql_jwt.ObtainJSONWebToken.Field()

verify_token = graphql_jwt.Verify.Field()

refresh_token = graphql_jwt.Refresh.Field()

Create the GraphQL schema

schema = graphene.Schema(query=Query, mutation=Mutation)

New contributor

hafeez.js is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


Load 5 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *