Skip to content

Good example of graphene-django covering multiple FK and choice field[Question] #527

@MilanRgm

Description

@MilanRgm

I apologize this is a question. I feel very difficult to find tutorial which covers most of the parts like nested relation, choice field mainly in the mutation part. I find query part in most of the tutorials. Though, I have asked this same question in SO, I am posting the same here either so most of the newbies like me get benefit from the nested relation and choice field concept when mutating.

The below code is related to creating a company. But my code covers only company table not the brand and business model which are related to company.

Here it is

models.py

class Company(models.Model):

    owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    name = models.CharField(max_length=150, blank=False, null=False)
    slug = models.SlugField()
    email = models.EmailField(max_length=150, blank=False, null=False)


class Brand(models.Model):

    ZERO = 'Zero'
    ZEROTOTEN = 'ZTT'
    TENTOTWENTY = 'TTT'

    NUMBER_OF_FRANCHISES = (
        (ZERO, '0'),
        (ZEROTOTEN, '0 - 10'),
        (TENTOTWENTY, '10 - 20'),
    )

    company = models.ForeignKey(Company, related_name='company_brand', on_delete=models.CASCADE)
    name = models.CharField(max_length=150, blank=False, null=False)
    franchising_date = models.DateTimeField()
    number_of_franchises = models.CharField(max_length=5, choices=NUMBER_OF_FRANCHISES, default=None)

class BusinessModel(models.Model):

    company = models.ForeignKey(Company, related_name='company_business_model', on_delete=models.CASCADE)
    industry = models.ForeignKey(Industry, null=True, related_name='industry', on_delete=models.SET_NULL)
    segments = models.ForeignKey(Segment, on_delete=models.SET_NULL, null=True)
    total_investment = models.CharField(max_length=50, choices=TOTAL_INVESTMENT, default=None)
    franchise_fee = models.CharField(max_length=50, choices=FRANCHISE_FEE, default=None)

mutation.py

class CompanyInput(graphene.InputObjectType):

        name = graphene.String(description='Name of your company')
        email = graphene.String(description='Email of your company')
        phone_number = graphene.String(description='Phone number of your company')
        director = graphene.String(description='Director of your company')
        franchise_head = graphene.String(description='Franchise Head of your company')


class BrandInput(graphene.InputObjectType):

    company = graphene.List(CompanyInput)
    name = graphene.String()
    franchising_date = graphene.String()
    number_of_franchises = graphene.String()


class BusinessModelInput(graphene.InputObjectType):

    company = graphene.List(CompanyInput)
    industry = graphene.List(IndustryInput)
    segments = graphene.String(SegmentInput)
    total_investment = graphene.String()
    franchise_fee = graphene.String()


# only a table for company can be created with this way but I have no idea on how can i create brand and business model table either as they are 
# linked to company. Because a company can have brand and business model.

class CreateCompany(graphene.Mutation):

    class Arguments:

        input = CompanyInput(description="These fields are required", required=True)

    class Meta:

        description = "Creates a new company"
        # model = models.Company

    errors = graphene.String()
    company = graphene.Field(CompanyNode)

    @staticmethod
    def mutate(root, info, input=None):
        if not info.context.user.is_authenticated:
            return CreateCompany(errors=json.dumps('Please Login to list your company'))
        company = models.Company.objects.create(owner=info.context.user,
                                                name=input.name,
                                                email=input.email,
                                                )
        return CreateCompany(company=company, errors=None)

I appreciate this community in sharing their knowledge in advance.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions