Python graphene 模块,ID 实例源码
我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用graphene.ID。
def test_add_operators_to_field_reference_field():
from mongoengine import ReferenceField, Document, StringField
from graphene_mongo.operators import gen_operators_of_field, allowed_operators
from graphene_mongo.fields import respective_special_fields
class Other(Document):
name = StringField()
class Test(Document):
test = ReferenceField(Other)
field = Test.test
r_graphene = respective_special_fields[type(field)]
applied_operators = gen_operators_of_field('test', field, r_graphene('test', field), allowed_operators(field))
assert sorted(list(applied_operators.keys())) == format_fields(['in', 'nin', 'ne'])
assert isinstance(applied_operators['test__in'], graphene.List)
assert isinstance(applied_operators['test__nin'], graphene.List)
assert isinstance(applied_operators['test__ne'], graphene.ID)
def field_to_id(m_field, g_field):
""" We need this because if we want to do a query using the id,we will pass a string to args with the id of the
document that we want,but graphene needs a ID field,instead of Field. This function convert to right thing."""
if isinstance(m_field, ReferenceField):
return graphene.ID()
elif (isinstance(m_field, ListField) or isinstance(m_field, SortedListField)) and \
isinstance(m_field.field, ReferenceField):
""" Pass here if it is a ListField or SortedListField of ReferenceField """
return graphene.List(graphene.ID)
else:
return g_field
def test_should_auto_convert_id():
assert_conversion(models.AutoField, graphene.ID, primary_key=True)
def test_should_manytoone_convert_connectionorlist():
field = forms.ModelChoiceField(Reporter.objects.all())
graphene_type = convert_form_field(field)
assert isinstance(graphene_type, graphene.ID)