如何向石墨烯中的 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'])
它失败了,因为参数不被接受。我错过了什么?