Python graphene 模块,Int() 实例源码
我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用graphene.Int()。
def get_field_args(field):
types = {
str: graphene.String,
bool: graphene.Boolean,
int: graphene.Int,
float: graphene.Float,
}
try:
args = inspect.signature(field)
result = {}
for name, arg in args.parameters.items():
if arg.default is not None:
custom_type = types[type(arg.default)]
else:
custom_type = graphene.String
result.update({name: custom_type()})
return result
except (KeyError, ValueError):
return {}
def schema():
class Query(graphene.ObjectType):
test_string = graphene.String()
def resolve_test_string(self, args, context, info):
return 'works'
# Todo: Implement case conversion for arg names
class Subscription(graphene.ObjectType):
test_subscription = graphene.String()
test_context = graphene.String()
test_filter = graphene.String(filterBoolean=graphene.Boolean())
test_filter_multi = graphene.String(
filterBoolean=graphene.Boolean(),
a=graphene.String(),
b=graphene.Int())
test_channel_options = graphene.String()
def resolve_test_subscription(self, info):
return self
def resolve_test_context(self, info):
return context
def resolve_test_filter(self, info):
return 'good_filter' if args.get('filterBoolean') else 'bad_filter'
def resolve_test_filter_multi(self, info):
return 'good_filter' if args.get('filterBoolean') else 'bad_filter'
def resolve_test_channel_options(self, info):
return self
return graphene.Schema(query=Query, subscription=Subscription)
def test_should_number_convert_int():
assert_attribute_conversion(NumberAttribute(), graphene.Int)
def convert_fields(attrs_mongo_doc):
""" Return a tuple od dicts,
@param attrs_mongo_doc: dict of attributes of the mongoengine.Document,that is accessible by Document._fields
fields: is the pure fields,key is the name of field,and values is the repective graphene field
fields_mutation: is the fields that is used in mutation,that will go direct in args of mutation
operators_mutation: is the fields that user set values to be saved on document
operators_single: is the fields with operators that will be in the query for a single result
operators_list: same of operators single,but these are avaliable only for query of lists
"""
fields, fields_mutation, operators_mutation, operators_single = {}, {}, {}
for f_name, mongo_field in attrs_mongo_doc.items():
field = RelationMongoGraphene(name=f_name, mongo_field=mongo_field)
fields[f_name] = field.graphene
operators_single.update(field.operators)
fields_mutation[f_name] = field.mutation
if not isinstance(mongo_field, ObjectIdField):
operators_mutation[f_name] = field.mutation
operators_list = operators_single.copy()
operators_single['skip'] = graphene.Int()
operators_list.update({p: graphene.Int() for p in special_query_parameters})
return fields, operators_single, operators_list
def test_should_positive_integer_convert_int():
assert_conversion(models.PositiveIntegerField, graphene.Int)
def test_should_positive_small_convert_int():
assert_conversion(models.PositiveSmallIntegerField, graphene.Int)
def test_should_small_integer_convert_int():
assert_conversion(models.SmallIntegerField, graphene.Int)
def test_should_big_integer_convert_int():
assert_conversion(models.BigIntegerField, graphene.Int)
def test_should_integer_convert_int():
assert_conversion(forms.IntegerField, graphene.Int)
def test_should_integer_convert_int():
assert_conversion(serializers.IntegerField, graphene.Int)
def test_should_list_convert_to_list():
class StringListField(serializers.ListField):
child = serializers.CharField()
field_a = assert_conversion(
serializers.ListField,
graphene.List,
child=serializers.IntegerField(min_value=0, max_value=100)
)
assert field_a.of_type == graphene.Int
field_b = assert_conversion(StringListField, graphene.List)
assert field_b.of_type == graphene.String
def plural():
return graphene.List(StatementGraph, is_startpoint=graphene.Boolean(), issue_uid=graphene.Int())
def convert_ndb_int_property(ndb_prop, registry=None):
return convert_ndb_scalar_property(Int, ndb_prop)