简体   繁体   中英

User defined fields in graphQL

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?

I think this is what you need


    class EmployeeInfo(graphene.Mutation):

        employee = graphene.List(EmployeeType)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM