'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?
Solution 1:[1]
I think this is what you need
class EmployeeInfo(graphene.Mutation):
employee = graphene.List(EmployeeType)
class Arguments:
bonuses = graphene.JSONString()
#...OTHER FIELDS
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | rymanso |