在 graphene/graphene_django 中扩展查询参数
Extending query arguments in graphene/graphene_django
如何向石墨烯中的 GraphQL 查询添加非字段参数?这是一个用例示例。我希望能够做到:
{
hsv(h: 40, s: 128, v: 54) {
r
g
b
name
}
使用此 Django 模型:
from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator,
class Color(models.Model):
name = models.CharField(
"name",
max_length=24,
null=False, blank=False)
r = models.IntegerField(
"red", null=False, blank=False,
validators=[MinValueValidator(0), MinValueValidator(255)]
)
g = models.IntegerField(
"green", null=False, blank=False,
validators=[MinValueValidator(0), MinValueValidator(255)]
)
b = models.IntegerField(
"blue", null=False, blank=False,
validators=[MinValueValidator(0), MinValueValidator(255)]
)
以及此 GraphQL 对象类型和基于它的查询:
from graphene import ObjectType, IntegerField, Field, relay
from graphene_django import DjangoObjectType
from .django import Color
from colorsys import hsv_to_rgb
class ColorNode(DjangoObjectType):
r = IntegerField()
g = IntegerField()
b = IntegerField()
class Meta:
model = Color
class Query(ObjectType):
rgb = relay.node.Field(ColorNode)
hsv = relay.node.Field(ColorNode)
named = relay.node.Field(ColorNode)
def resolve_rgb(self, args, context, info):
if not all(map(lambda x: x in args, ['r', 'g', 'b'])):
# Arguments missing
return None
return Color.objects.get(**args)
def resolve_hsv(self, args, context, info):
if not all(map(lambda x: x in args, ['h', 's', 'v'])):
# Arguments missing
return None
r, g, b = hsv_to_rgb(args['h'], args['s'], args['v'])
return Color.objects.get(r=r, g=g, b=b)
def resolve_named(self, args, context, info):
if not 'name' in args:
# Arguments missing
return None
return Color.objects.get(name=args['name'])
它失败了,因为参数没有被接受。我错过了什么?
原来答案很简单。要向解析器添加参数,请在字段的构造函数中声明参数,如下所示:
rgb = relay.node.Field(ColorNode,
r=graphene.String(),
g=graphene.String(),
b=graphene.String())
hsv = relay.node.Field(ColorNode,
h=graphene.String(),
s=graphene.String(),
v=graphene.String()))
named = relay.node.Field(ColorNode,
name=graphene.String())
参数随后可以包含在查询中,如上所示。
如何向石墨烯中的 GraphQL 查询添加非字段参数?这是一个用例示例。我希望能够做到:
{
hsv(h: 40, s: 128, v: 54) {
r
g
b
name
}
使用此 Django 模型:
from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator,
class Color(models.Model):
name = models.CharField(
"name",
max_length=24,
null=False, blank=False)
r = models.IntegerField(
"red", null=False, blank=False,
validators=[MinValueValidator(0), MinValueValidator(255)]
)
g = models.IntegerField(
"green", null=False, blank=False,
validators=[MinValueValidator(0), MinValueValidator(255)]
)
b = models.IntegerField(
"blue", null=False, blank=False,
validators=[MinValueValidator(0), MinValueValidator(255)]
)
以及此 GraphQL 对象类型和基于它的查询:
from graphene import ObjectType, IntegerField, Field, relay
from graphene_django import DjangoObjectType
from .django import Color
from colorsys import hsv_to_rgb
class ColorNode(DjangoObjectType):
r = IntegerField()
g = IntegerField()
b = IntegerField()
class Meta:
model = Color
class Query(ObjectType):
rgb = relay.node.Field(ColorNode)
hsv = relay.node.Field(ColorNode)
named = relay.node.Field(ColorNode)
def resolve_rgb(self, args, context, info):
if not all(map(lambda x: x in args, ['r', 'g', 'b'])):
# Arguments missing
return None
return Color.objects.get(**args)
def resolve_hsv(self, args, context, info):
if not all(map(lambda x: x in args, ['h', 's', 'v'])):
# Arguments missing
return None
r, g, b = hsv_to_rgb(args['h'], args['s'], args['v'])
return Color.objects.get(r=r, g=g, b=b)
def resolve_named(self, args, context, info):
if not 'name' in args:
# Arguments missing
return None
return Color.objects.get(name=args['name'])
它失败了,因为参数没有被接受。我错过了什么?
原来答案很简单。要向解析器添加参数,请在字段的构造函数中声明参数,如下所示:
rgb = relay.node.Field(ColorNode,
r=graphene.String(),
g=graphene.String(),
b=graphene.String())
hsv = relay.node.Field(ColorNode,
h=graphene.String(),
s=graphene.String(),
v=graphene.String()))
named = relay.node.Field(ColorNode,
name=graphene.String())
参数随后可以包含在查询中,如上所示。