def convert_serializer_field_to_list_of_string(field):
return (graphene.List, graphene.String)
python类String()的实例源码
def test_should_char_convert_string():
assert_conversion(serializers.CharField, graphene.String)
def test_should_slug_convert_string():
assert_conversion(serializers.SlugField, graphene.String)
def test_should_url_convert_string():
assert_conversion(serializers.URLField, graphene.String)
def test_should_choice_convert_string():
assert_conversion(serializers.ChoiceField, graphene.String, choices=[])
def test_should_base_field_convert_string():
assert_conversion(serializers.Field, graphene.String)
def test_should_regex_convert_string():
assert_conversion(serializers.RegexField, graphene.String, regex='[0-9]+')
def test_should_duration_convert_string():
assert_conversion(serializers.DurationField, graphene.String)
def test_should_file_convert_string():
assert_conversion(serializers.FileField, graphene.String)
def test_should_filepath_convert_string():
assert_conversion(serializers.FilePathField, graphene.String, path='/')
def test_should_ip_convert_string():
assert_conversion(serializers.IPAddressField, graphene.String)
def test_should_image_convert_string():
assert_conversion(serializers.ImageField, graphene.String)
def convert_column_to_string(type, column, registry=None):
return graphene.String(
description=get_column_doc(column),
required=not(is_column_nullable(column)),
)
def convert_ndb_string_property(ndb_prop, registry=None):
return convert_ndb_scalar_property(String, ndb_prop)
def convert_ndb_key_propety(ndb_key_prop, registry=None):
"""
Two conventions for handling KeyProperties:
#1.
Given:
store_key = ndb.KeyProperty(...)
Result is 2 fields:
store_id = graphene.String() -> resolves to store_key.urlsafe()
store = NdbKeyField() -> resolves to entity
#2.
Given:
store = ndb.KeyProperty(...)
Result is 2 fields:
store_id = graphene.String() -> resolves to store_key.urlsafe()
store = NdbKeyField() -> resolves to entity
"""
is_repeated = ndb_key_prop._repeated
name = ndb_key_prop._code_name
if name.endswith('_key') or name.endswith('_keys'):
# Case #1 - name is of form 'store_key' or 'store_keys'
string_prop_name = rreplace(name, '_key', '_id', 1)
resolved_prop_name = name[:-4] if name.endswith('_key') else p.plural(name[:-5])
else:
# Case #2 - name is of form 'store'
singular_name = p.singular_noun(name) if p.singular_noun(name) else name
string_prop_name = singular_name + '_ids' if is_repeated else singular_name + '_id'
resolved_prop_name = name
return [
ConversionResult(name=string_prop_name, field=DynamicNdbKeyStringField(ndb_key_prop, registry=registry)),
ConversionResult(name=resolved_prop_name, field=DynamicNdbKeyReferenceField(ndb_key_prop, registry=registry))
]
def convert_computed_property(ndb_computed_prop, registry=None):
return convert_ndb_scalar_property(String, ndb_computed_prop)
def testGET_support_json_variables(self):
response = self.app.get('/graphql', params=dict(
query='query helloWho($who: String){ greet(who: $who) }',
variables=json.dumps({'who': "ekampf"})
))
response_dict = json.loads(response.body)
self.assertDictEqual(
response_dict.get('data'), {'greet': 'Hello ekampf!'}
)
def testPOST_support_json_variables(self):
response = self.app.post('/graphql', params=json.dumps(dict(
query='query helloWho($who: String){ greet(who: $who) }',
variables={'who': "ekampf"}
)))
response_dict = json.loads(response.body)
self.assertDictEqual(
response_dict.get('data'), {'greet': 'Hello ekampf!'}
)
def test_handles_poorly_formed_variables(self):
for method in (self.get, self.post):
response = method('/graphql', expect_errors=True, params=dict(
query='query helloWho($who: String){ greet(who: $who) }',
variables='who:You'
))
response_data = json.loads(response.body)
self.assertEqual(response.status_int, 400)
self.assertEqual(response_data['errors'][0]['message'], 'Variables are invalid JSON.')
def testStringProperty_shouldConvertToString(self):
self.__assert_conversion(ndb.StringProperty, graphene.String)