def test_prints_unions(): FooType = GraphQLObjectType( name='Foo', fields={ 'bool': GraphQLField(GraphQLBoolean), }, ) BarType = GraphQLObjectType( name='Bar', fields={ 'str': GraphQLField(GraphQLString), }, ) SingleUnion = GraphQLUnionType(name='SingleUnion', resolve_type=lambda *_: None, types=[FooType]) MultipleUnion = GraphQLUnionType( name='MultipleUnion', resolve_type=lambda *_: None, types=[FooType, BarType], ) Root = GraphQLObjectType(name='Root', fields=OrderedDict([ ('single', GraphQLField(SingleUnion)), ('multiple', GraphQLField(MultipleUnion)), ])) Schema = GraphQLSchema(Root) output = print_for_test(Schema) assert output == '''
def test_builds_a_schema_with_a_union(): DogType = GraphQLObjectType( name='Dog', fields=lambda: { 'bestFriend': GraphQLField(FriendlyType) } ) HumanType = GraphQLObjectType( name='Human', fields=lambda: { 'bestFriend': GraphQLField(FriendlyType) } ) FriendlyType = GraphQLUnionType( name='Friendly', resolve_type=lambda: None, types=[DogType, HumanType] ) schema = GraphQLSchema( query=GraphQLObjectType( name='WithUnion', fields={ 'friendly': GraphQLField(FriendlyType) } ) ) _test_schema(schema)
def internal_type(cls, schema): if cls._meta.abstract: raise Exception("Abstract ObjectTypes don't have a specific type.") return GraphQLUnionType( cls._meta.type_name, types=list(map(schema.T, cls._meta.types)), resolve_type=partial(cls._resolve_type, schema), description=cls._meta.description, )
def test_prohibits_putting_non_object_types_in_unions(): bad_union_types = [ GraphQLInt, GraphQLNonNull(GraphQLInt), GraphQLList(GraphQLInt), InterfaceType, UnionType, EnumType, InputObjectType ] for x in bad_union_types: with raises(Exception) as excinfo: GraphQLUnionType('BadUnion', [x]) assert 'Union BadUnion may only contain object types, it cannot contain: ' + str(x) + '.' \ == str(excinfo.value)
def internal_type(cls, schema): if cls._meta.is_interface: return GraphQLInterfaceType( cls._meta.type_name, description=cls._meta.description, resolve_type=partial(cls._resolve_type, schema), fields=partial(cls.get_fields, schema) ) elif cls._meta.is_union: return GraphQLUnionType( cls._meta.type_name, types=cls._meta.types, description=cls._meta.description, ) return GraphQLObjectType( cls._meta.type_name, description=cls._meta.description, interfaces=[schema.T(i) for i in cls._meta.interfaces], fields=partial(cls.get_fields, schema), is_type_of=getattr(cls, 'is_type_of', None) )
interfaces=[NamedType], fields={ 'name': GraphQLField(GraphQLString), 'meows': GraphQLField(GraphQLBoolean), }, is_type_of=lambda value: isinstance(value, Cat)) def resolve_pet_type(value): if isinstance(value, Dog): return DogType if isinstance(value, Cat): return CatType PetType = GraphQLUnionType('Pet', [DogType, CatType], resolve_type=resolve_pet_type) PersonType = GraphQLObjectType( name='Person', interfaces=[NamedType], fields={ 'name': GraphQLField(GraphQLString), 'pets': GraphQLField(GraphQLList(PetType)), 'friends': GraphQLField(GraphQLList(NamedType)), }, is_type_of=lambda value: isinstance(value, Person)) schema = GraphQLSchema(PersonType) garfield = Cat('Garfield', False) odie = Dog('Odie', True)
'Query', { 'article': GraphQLField(BlogArticle, args={ 'id': GraphQLArgument(GraphQLString), }), 'feed': GraphQLField(GraphQLList(BlogArticle)) }) BlogMutation = GraphQLObjectType('Mutation', {'writeArticle': GraphQLField(BlogArticle)}) ObjectType = GraphQLObjectType('Object', {}) InterfaceType = GraphQLInterfaceType('Interface') UnionType = GraphQLUnionType('Union', [ObjectType]) EnumType = GraphQLEnumType('Enum', {}) InputObjectType = GraphQLInputObjectType('InputObject', {}) def test_defines_a_query_only_schema(): BlogSchema = GraphQLSchema(BlogQuery) assert BlogSchema.get_query_type() == BlogQuery article_field = BlogQuery.get_fields()['article'] assert article_field.type == BlogArticle assert article_field.type.name == 'Article' assert article_field.name == 'article' article_field_type = article_field.type
GraphQLBoolean, args={ 'x': GraphQLArgument(GraphQLInt), 'y': GraphQLArgument(GraphQLInt) } ) }, interfaces=[Being, Pet], is_type_of=lambda: None) Cat = GraphQLObjectType('Cat', lambda: { 'furColor': GraphQLField(FurColor), 'name': GraphQLField(GraphQLString, { 'surname': GraphQLArgument(GraphQLBoolean), }) }, interfaces=[Being, Pet], is_type_of=lambda: None) CatOrDog = GraphQLUnionType('CatOrDog', [Dog, Cat]) Intelligent = GraphQLInterfaceType('Intelligent', { 'iq': GraphQLField(GraphQLInt), }) Human = GraphQLObjectType( name='Human', interfaces=[Being, Intelligent], is_type_of=lambda: None, fields={ 'name': GraphQLField(GraphQLString, { 'surname': GraphQLArgument(GraphQLBoolean), }), 'pets': GraphQLField(GraphQLList(Pet)), 'iq': GraphQLField(GraphQLInt),
GraphQLField(GraphQLList(BlogArticle)) }) BlogMutation = GraphQLObjectType('Mutation', {'writeArticle': GraphQLField(BlogArticle)}) BlogSubscription = GraphQLObjectType( 'Subscription', { 'articleSubscribe': GraphQLField(args={'id': GraphQLArgument(GraphQLString)}, type=BlogArticle) }) ObjectType = GraphQLObjectType('Object', {}) InterfaceType = GraphQLInterfaceType('Interface') UnionType = GraphQLUnionType('Union', [ObjectType], resolve_type=lambda: None) EnumType = GraphQLEnumType('Enum', {'foo': GraphQLEnumValue()}) InputObjectType = GraphQLInputObjectType('InputObject', {}) def test_defines_a_query_only_schema(): BlogSchema = GraphQLSchema(BlogQuery) assert BlogSchema.get_query_type() == BlogQuery article_field = BlogQuery.get_fields()['article'] assert article_field.type == BlogArticle assert article_field.type.name == 'Article' assert article_field.name == 'article' article_field_type = article_field.type
BarType = GraphQLObjectType(name='Bar', interfaces=[SomeInterfaceType], fields=lambda: OrderedDict([ ('name', GraphQLField(GraphQLString)), ('some', GraphQLField(SomeInterfaceType)), ('foo', GraphQLField(FooType)), ])) BizType = GraphQLObjectType(name='Biz', fields=lambda: OrderedDict([ ('fizz', GraphQLField(GraphQLString)), ])) SomeUnionType = GraphQLUnionType( name='SomeUnion', resolve_type=lambda: FooType, types=[FooType, BizType], ) test_schema = GraphQLSchema(query=GraphQLObjectType( name='Query', fields=lambda: OrderedDict([ ('foo', GraphQLField(FooType)), ('someUnion', GraphQLField(SomeUnionType)), ('someInterface', GraphQLField( SomeInterfaceType, args={'id': GraphQLArgument(GraphQLNonNull(GraphQLID))}, )), ])))
Dog = GraphQLObjectType('Dog', { 'name': GraphQLField(GraphQLString, { 'surname': GraphQLArgument(GraphQLBoolean), }), 'nickname': GraphQLField(GraphQLString), 'barks': GraphQLField(GraphQLBoolean), 'doesKnowCommand': GraphQLField(GraphQLBoolean, { 'dogCommand': GraphQLArgument(DogCommand) }) }, interfaces=[Pet]) Cat = GraphQLObjectType('Cat', lambda: { 'furColor': GraphQLField(FurColor) }, interfaces=[Pet]) CatOrDog = GraphQLUnionType('CatOrDog', [Dog, Cat]) Human = GraphQLObjectType('Human', { 'name': GraphQLField(GraphQLString, { 'surname': GraphQLArgument(GraphQLBoolean), }), 'pets': GraphQLField(GraphQLList(Pet)), }) FurColor = GraphQLEnumType('FurColor', { 'BROWN': GraphQLEnumValue(0), 'BLACK': GraphQLEnumValue(1), 'TAN': GraphQLEnumValue(2), 'SPOTTED': GraphQLEnumValue(3), })