Graphene Django - Mutation with one to many relation foreign key
Asked Answered
S

1

14

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.

Servomechanical answered 20/11, 2018 at 10:35 Comment(4)
Have you found a solution to this issue?Kristopher
@Kristopher Yes. To graphene.Field goes InputType not Type object. As you see class CompanyInput. All you need to do is change for example graphene.Field(CurrencyType) to graphene.Field(CurrencyInput). The same class for currency like CompanyInput for company.Servomechanical
Could you please write up the answer along with what you defined for 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
Did any of you manage to get this working? Would love to see an example of it.Guyon
S
11

For those, which are still searching for the answer.

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(CurrencyInput)
    country = graphene.Field(CountryInput)
    parent = graphene.Field(CompanyInput)
    phone_number = graphene.String()

class CurrencyInput(graphene.InputObjectType):
    name = graphene.String()
    code = graphene.String()
    character = graphene.String()

class CountryInput(graphene.InputObjectType):
    name = graphene.String()
    code = 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)

As you can see, I just replaced CompanyType, CurrencyType and CountryType objects for input objects because Input objects specifying INPUT which user type to query (request).

Type objects specifying return object which mutation returns, when everything was successfully. So when you just look at class CreateCompany, company is object which will be returned when mutation is success (Is CompanyType object) because we creating company and we wants response of object company.

As Arguments class there is CompanyInput which has nested inputs like currency or country or self (its like object in object).

Static method mutate will call Django create function and this created object will be assigned to our company object which is CompnyType and this will be that response.

(Of course you can call another function than create when you want to implement some business logic before and after creating but mutation method must return specific object or objects which was or were defined as response. For me company in CreateCompany class. Of course there can be more objects or lists of objects. It only depends on you.)

Servomechanical answered 23/4, 2019 at 7:24 Comment(4)
Could you, please, show the example of your query? I'm wondering how did you provide the ForeignKey input in the mutation queryGeneviegenevieve
Hi. Foreign keys can be handled two ways. Firstly like example above currency, country and parent are in model foreign keys, but in that example you will create new record in table and assign that record to Company object as FK. Second example (which I hope you want) is that object which is FK is already created in table and you just want to assign it to Company object. It is very similar. For example with currency as FK it will be in input like: currency_id = graphene.Int() and in mutation you will simply provide that number corresponding to FK of currency object.Servomechanical
Yeah, thanks. I was thinking about the passing the whole foreign object into the mutation query instead the ID only, regarding GraphQL InputType (thought to keep them consistent) but later I decided to avoid such weird thing and used the ID only.Geneviegenevieve
Oh so even when using InputObjectType we just have to provide the ID of the desired object and then somehow it fetches it to be able to assign it ?Sicklebill

© 2022 - 2024 — McMap. All rights reserved.