def test_field_none_type_raises_error(): s = Schema() f = Field(None) f.contribute_to_class(ot, 'field_name') with raises(Exception) as excinfo: f.internal_field(s) assert str(excinfo.value) == "Internal type for field ObjectType.field_name is None"
def test_field_resolve_type_custom(): s = Schema() f = Field('self') f.contribute_to_class(ot, 'field_name') field_type = f.get_object_type(s) assert field_type == ot
def test_field_none_type_raises_error(): s = Schema() f = Field(None) f.contribute_to_class(MyOt, 'field_name') with raises(Exception) as excinfo: s.T(f) assert str( excinfo.value) == "Internal type for field MyOt.field_name is None"
def test_field_added_in_meta(): opt = Options(Meta) class ObjectType(object): pass opt.contribute_to_class(ObjectType, '_meta') f = Field(None) f.attname = 'string_field' opt.add_field(f) assert f in opt.fields
def test_field_resolve_type_custom(): class MyCustomType(ObjectType): pass class OtherType(ObjectType): pass s = Schema() f = Field('MyCustomType') f.contribute_to_class(OtherType, 'field_name') field_type = f.get_object_type(s) assert field_type == MyCustomType
def inner_field(self, schema): from graphene.relay.types import BaseNode node_field = BaseNode.get_definitions(schema).node_field def resolver(instance, args, info): global_id = args.get('id') resolved_global_id = from_global_id(global_id) if resolved_global_id.type == self.field_object_type._meta.type_name: return node_field.resolver(instance, args, info) args = OrderedDict([(a.name, a) for a in node_field.args]) field = Field(self.field_object_type, id=args['id'], resolve=resolver) field.contribute_to_class(self.object_type, self.field_name) return field
def test_field_orders_wrong_type(): field = Field(None) try: assert not field < 1 except TypeError: # Fix exception raising in Python3+ pass
def test_field_resolve_type_custom(): class MyCustomType(object): pass class Schema(object): def get_type(self, name): if name == 'MyCustomType': return MyCustomType s = Schema() f = Field('MyCustomType') f.contribute_to_class(ot, 'field_name') field_type = f.get_object_type(s) assert field_type == MyCustomType
class Human(Character): friends = List(Character) pet = Field(Pet) def resolve_name(self, *args): return 'Peter' def resolve_friend(self, *args): return Human(object()) def resolve_pet(self, *args): return Pet(object())
def test_field_resolve_type_custom(): class MyCustomType(ObjectType): pass f = Field('MyCustomType') class OtherType(ObjectType): field_name = f s = Schema() s.query = OtherType s.register(MyCustomType) assert s.T(f).type == s.T(MyCustomType)
def test_field_name_use_name_if_exists(): f = Field(GraphQLString, name='my_custom_name') f.contribute_to_class(MyOt, 'field_name') assert f.name == 'my_custom_name'
def test_field_name(): f = Field(GraphQLString) f.contribute_to_class(MyOt, 'field_name') assert f.name is None assert f.attname == 'field_name'
def test_field_type(): f = Field(GraphQLString) f.contribute_to_class(MyOt, 'field_name') assert isinstance(schema.T(f), GraphQLField) assert schema.T(f).type == GraphQLString
def test_field_orders(): f1 = Field(None) f2 = Field(None) assert f1 < f2
def test_field_eq(): f1 = Field(None) f2 = Field(None) assert f1 != f2
def test_field_name_automatic_camelcase(): f = Field(GraphQLString) f.contribute_to_class(MyOt, 'field_name') assert f.name == 'fieldName'
def test_field_type(): f = Field(GraphQLString) f.contribute_to_class(ot, 'field_name') assert isinstance(f.internal_field(schema), GraphQLField) assert f.internal_type(schema) == GraphQLString
def test_field_no_contributed_raises_error(): f = Field(GraphQLString) with raises(Exception) as excinfo: f.internal_field(schema)
def test_field_eq_wrong_type(): field = Field(None) assert field != 1
def test_field_hash(): f1 = Field(None) f2 = Field(None) assert hash(f1) != hash(f2)
def test_field_no_contributed_raises_error(): f = Field(GraphQLString) with raises(Exception): schema.T(f)
class Query(ObjectType): f = Field(Character) def resolve_f(self, args, info): return Human()
def test_field_name_automatic_camelcase(): f = Field(GraphQLString) f.contribute_to_class(ot, 'field_name') assert f.name == 'fieldName'