graphQL 中的用户定义字段

发布于 2025-01-15 11:56:39 字数 1411 浏览 0 评论 0原文

使用石墨烯-Django 制作员工管理系统,我有一个 JsonB 字段,我不知道其形状,因为每个公司都会定义该形状。

这是模型:

    class Employee(models.Model):
        bonuses = models.JSONField(default=dict)
        #...OTHER FIELDS

这是类型:

    class EmployeeType(DjangoObjectType):
        class Meta:
            model = Employee
            fields = "__all__"

这是突变类:

    class EmployeeInfo(graphene.Mutation):

        employee = graphene.List(EmployeeType)

        class Arguments:
            bonuses = GenericScalar()
            #...OTHER FIELDS

        def mutate(self, info, **kwargs):
             #DOING STUFF
            return EmployeeInfo(employee=employee)

现在,假设一家公司想要向具有以下模式的开发人员提供奖金:

    export const EMPLOYEE_INFO = gql`
        mutation MutateEmployee(
                    $employeeOfTheMonth: Int
                    $mostPR: Int
                    $profitSharing: Int
        ) {
            employeeInfo(
                bonuses:{
                    employeeOfTheMonth: $employeeOfTheMonth
                    mostPR: $mostPR
                    profitSharing: $profitSharing
                }
                #...OTHER FIELDS
            ){....}
         }
     `

这是我当前的设置,问题是我在数据库中仅获得奖金的空值场地。请注意,我使用的是没有记录的 GenericScalar,我不知道这是否是错误的标量。

如果这是一家餐厅,显然奖金会有所不同,这就是为什么我需要这样的设置。

如何定义一个采用用户定义形状的字段?

Making an employee management system with graphene-Django and I have a JsonB field that I don't know the shape of because each company will defined the shape.

Here's the model:

    class Employee(models.Model):
        bonuses = models.JSONField(default=dict)
        #...OTHER FIELDS

here's the type:

    class EmployeeType(DjangoObjectType):
        class Meta:
            model = Employee
            fields = "__all__"

Here's the mutation class:

    class EmployeeInfo(graphene.Mutation):

        employee = graphene.List(EmployeeType)

        class Arguments:
            bonuses = GenericScalar()
            #...OTHER FIELDS

        def mutate(self, info, **kwargs):
             #DOING STUFF
            return EmployeeInfo(employee=employee)

Now, say a company wants to give bonuses to a developer with the following schema:

    export const EMPLOYEE_INFO = gql`
        mutation MutateEmployee(
                    $employeeOfTheMonth: Int
                    $mostPR: Int
                    $profitSharing: Int
        ) {
            employeeInfo(
                bonuses:{
                    employeeOfTheMonth: $employeeOfTheMonth
                    mostPR: $mostPR
                    profitSharing: $profitSharing
                }
                #...OTHER FIELDS
            ){....}
         }
     `

This is my current setup, the problem is I get only null values in the database for the bonuses field. Notice that I'm using GenericScalar which is not documented and I don't know if that's the wrong scalar.

If this is a restaurant, obviously the bonuses will be different and that's why I need a setup like this.

How can I define a field that will take user defined shapes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

止于盛夏 2025-01-22 11:56:39

我想这就是你需要的


    class EmployeeInfo(graphene.Mutation):

        employee = graphene.List(EmployeeType)

        class Arguments:
            bonuses = graphene.JSONString()
            #...OTHER FIELDS

I think this is what you need


    class EmployeeInfo(graphene.Mutation):

        employee = graphene.List(EmployeeType)

        class Arguments:
            bonuses = graphene.JSONString()
            #...OTHER FIELDS
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文