graphQL 中的用户定义字段
使用石墨烯-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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想这就是你需要的
I think this is what you need