石墨烯Django-具有一对多关系外键的变异 [英] Graphene Django - Mutation with one to many relation foreign key

查看:108
本文介绍了石墨烯Django-具有一对多关系外键的变异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何为创建此Django模型正确创建突变:

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)

如您所见,有三个外键。对于模型货币国家父母(自己)公司DjangoObjectType 看起来非常简单:

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

最后是我的突变类 CreateCompany 定义了货币国家自我(父母),例如 graphene.Field()

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)

当我要启动django服务器时,将引发断言错误。

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:还可以向我展示GraphQL查询的示例,所以我知道如何称呼该突变?

PS: Please can you also show me example of GraphQL query, so I would know how to call that mutation? Thank you very much.

推荐答案

对于那些仍在寻找答案的人。

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)

如您所见,我只是将CompanyType,CurrencyType和CountryType对象替换为输入对象,因为Input对象指定INPUT来查询(请求)哪种用户类型。

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).

类型对象,指定在成功完成所有操作后突变返回的返回对象。因此,当您仅查看类CreateCompany时,company是对象,当成功完成更改后将返回该对象(Is CompanyType对象),因为我们创建了Company并希望对象Company的响应。

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.

作为Arguments类,有CompanyInput,它嵌套了诸如货币或国家或自身(其类似于对象中的对象)之类的输入。

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

静态方法mutate将调用Django create函数,并将此创建的对象分配给我们的公司对象CompnyType,并将其作为响应。

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.

(当然,当您想要在创建前后实现一些业务逻辑时,您可以调用create之外的其他函数,但是mutation方法必须返回一个或多个被定义为响应的特定对象。对我来说Company,位于CreateCompany类中。当然,可以有更多的对象或对象列表。仅取决于您。)

(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.)

这篇关于石墨烯Django-具有一对多关系外键的变异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆