I am creating a simple GraphQL request app in the backend using Django with graphene-django package.
Waht I need is to inherit the Input
inner class in the create mutation to the Input
class for the update mutation.
Can I do this using any way, and if not can any one help me solving this kind of issue, and to know that I know how to solve it but I don't want to rewrite the same field in the create and in the update mutations for the same model or type.
This is a sample of my code:
import graphene
from graphene import relay
from BoD.models import Meeting
from BoD.types.meeting import MeetingType
class CreateMeeting(relay.ClientIDMutation):
"""Mutation to create a new meeting."""
class Input:
created_by_id = graphene.Int(required=True)
subject = graphene.String(required=True)
location = graphene.String(required=True)
date = graphene.DateTime(required=True, datetime_input=graphene.DateTime(required=True))
start_time = graphene.Time(required=True)
end_time = graphene.Time(required=True)
meeting = graphene.Field(MeetingType)
@classmethod
def mutate_and_get_payload(cls, root, info, **input):
"""Create a new meeting."""
meeting = Meeting.objects.create(**input)
return CreateMeeting(meeting=meeting)
class UpdateMeeting(CreateMeeting):
"""Mutation to update a meeting."""
class Input(CreateMeeting.Input):
id = graphene.Int(required=True)
class MeetingMutate:
class Meta:
abstract = True
created_by_id = graphene.Int(required=True)
subject = graphene.String(required=True)
location = graphene.String(required=True)
date = graphene.DateTime(required=True, datetime_input=graphene.DateTime(required=True))
start_time = graphene.Time(required=True)
end_time = graphene.Time(required=True)
class CreateMeeting(relay.ClientIDMutation):
"""Mutation to create a new meeting."""
class Input(MeetingMutate.Meta):
pass
meeting = graphene.Field(MeetingType)
@classmethod
def mutate_and_get_payload(cls, root, info, **input):
"""Create a new meeting."""
meeting = Meeting.objects.create(**input)
return CreateMeeting(meeting=meeting)
class UpdateMeeting(CreateMeeting):
"""Mutation to update a meeting."""
class Input(MeetingMutate.Meta):
id = graphene.Int(required=True)
meeting = graphene.Field(MeetingType)
@classmethod
def mutate_and_get_payload(cls, root, info, **input):
"""Update a meeting."""
meeting = Meeting.objects.get(pk=input['id'])
meeting.update(**input)
meeting.save()
return UpdateMeeting(meeting=meeting)
meeting = graphene.Field(MeetingType)
@classmethod
def mutate_and_get_payload(cls, root, info, **input):
"""Update a meeting."""
meeting = Meeting.objects.get(pk=input['id'])
meeting.update(**input)
meeting.save()
return UpdateMeeting(meeting=meeting)
But this code gives me this error:
TypeError at /graphql
Cannot create a consistent method resolution
order (MRO) for bases InputObjectType, Input
I have created this code to solve my problem until anyone helps me with the issue.
The code looks like this:
import graphene
from graphene import relay
from BoD.models import Meeting
from BoD.types.meeting import MeetingType
class MeetingInput(graphene.InputObjectType):
created_by_id = graphene.Int(required=True)
subject = graphene.String(required=True)
location = graphene.String(required=True)
date = graphene.DateTime(required=True)
start_time = graphene.Time(required=True)
end_time = graphene.Time(required=True)
class CreateMeeting(relay.ClientIDMutation):
"""Mutation to create a new meeting."""
class Input:
input = MeetingInput(required=True)
meeting = graphene.Field(MeetingType)
@classmethod
def mutate_and_get_payload(cls, root, info, input):
"""Create a new meeting."""
meeting = Meeting.objects.create(**input)
return CreateMeeting(meeting=meeting)
class UpdateMeeting(relay.ClientIDMutation):
"""Mutation to update a meeting."""
class Input:
input = MeetingInput(required=True)
id = graphene.Int(required=True)
meeting = graphene.Field(MeetingType)
@classmethod
def mutate_and_get_payload(cls, root, info, input):
"""Update a meeting."""
meeting = Meeting.objects.get(id=input.id)
meeting.update(**input)
meeting.save()
return UpdateMeeting(meeting=meeting)