I would like to know how to properly create mutation for creating this django model:
class Company(models.Model):
class Meta:
db_table = 'companies'
app_label = 'core'
default_permissions = ()
name = models.CharField(unique=True, max_length=50, null=False)
email = models.EmailField(unique=True, null=False)
phone_number = models.CharField(max_length=13, null=True)
address = models.TextField(max_length=100, null=False)
crn = models.CharField(max_length=20, null=False)
tax = models.CharField(max_length=20, null=False)
parent = models.ForeignKey('self', null=True, on_delete=models.CASCADE)
currency = models.ForeignKey(Currency, null=False, on_delete=models.CASCADE)
country = models.ForeignKey(Country, null=False, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
As you see, there are three Foreign keys. For model Currency, Country and Parent(self). Company DjangoObjectType looks very simple like this:
class CompanyType(DjangoObjectType):
class Meta:
model = Company
And finally my mutation class CreateCompany have Currency, Country and Self(Parent) defined like graphene.Field()
:
class CompanyInput(graphene.InputObjectType):
name = graphene.String(required=True)
email = graphene.String(required=True)
address = graphene.String(required=True)
crn = graphene.String(required=True)
tax = graphene.String(required=True)
currency = graphene.Field(CurrencyType)
country = graphene.Field(CountryType)
parent = graphene.Field(CompanyType)
phone_number = graphene.String()
class CreateCompany(graphene.Mutation):
company = graphene.Field(CompanyType)
class Arguments:
company_data = CompanyInput(required=True)
@staticmethod
def mutate(root, info, company_data):
company = Company.objects.create(**company_data)
return CreateCompany(company=company)
When i want to start django server, Assertion error will be raised.
AssertionError: CompanyInput.currency field type must be Input Type but got: CurrencyType.
I was finding some good tutorial for one to many foreign key for a long time, so if someone know how to implement this solution nice and clear I would be very glad.
PS: Please can you also show me example of GraphQL query, so I would know how to call that mutation? Thank you very much.
CurrencyInput
and the associated graphql query? I am having trouble with a create mutation because of a foreign-key field, I've tried implementing your above approach but obviously, I am short of something important. – Discretionary