Python graphene.Dynamic() Examples

The following are 24 code examples of graphene.Dynamic(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module graphene , or try the search function .
Example #1
Source File: test_converter.py    From graphene-django with MIT License 6 votes vote down vote up
def test_should_manytomany_convert_connectionorlist_list():
    class A(DjangoObjectType):
        class Meta:
            model = Reporter

    graphene_field = convert_django_field(
        Reporter._meta.local_many_to_many[0], A._meta.registry
    )
    assert isinstance(graphene_field, graphene.Dynamic)
    dynamic_field = graphene_field.get_type()
    assert isinstance(dynamic_field, graphene.Field)
    # A NonNull List of NonNull A ([A!]!)
    # https://github.com/graphql-python/graphene-django/issues/448
    assert isinstance(dynamic_field.type, NonNull)
    assert isinstance(dynamic_field.type.of_type, graphene.List)
    assert isinstance(dynamic_field.type.of_type.of_type, NonNull)
    assert dynamic_field.type.of_type.of_type.of_type == A 
Example #2
Source File: test_converter.py    From graphene-mongo with MIT License 6 votes vote down vote up
def test_should_self_reference_convert_dynamic():
    class P(MongoengineObjectType):
        class Meta:
            model = Player
            interfaces = (graphene.Node,)

    dynamic_field = convert_mongoengine_field(
        Player._fields["opponent"], P._meta.registry
    )
    assert isinstance(dynamic_field, graphene.Dynamic)
    graphene_type = dynamic_field.get_type()
    assert isinstance(graphene_type, graphene.Field)
    assert graphene_type.type == P

    graphene_field = convert_mongoengine_field(
        Player._fields["players"], P._meta.registry
    )
    assert isinstance(graphene_field, MongoengineConnectionField) 
Example #3
Source File: converter.py    From graphene-mongo with MIT License 6 votes vote down vote up
def convert_lazy_field_to_dynamic(field, registry=None):
    model = field.document_type

    def lazy_resolver(root, *args, **kwargs):
        if getattr(root, field.name or field.db_name):
            return getattr(root, field.name or field.db_name).fetch()

    def dynamic_type():
        _type = registry.get_type_for_model(model)
        if not _type:
            return None
        return graphene.Field(
            _type,
            resolver=lazy_resolver,
            description=get_field_description(field, registry),
        )

    return graphene.Dynamic(dynamic_type) 
Example #4
Source File: converter.py    From graphene-gae with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def convert_local_structured_property(ndb_structured_property, registry=None):
    is_required = ndb_structured_property._required
    is_repeated = ndb_structured_property._repeated
    model = ndb_structured_property._modelclass
    name = ndb_structured_property._code_name

    def dynamic_type():
        _type = registry.get_type_for_model(model)
        if not _type:
            return None

        if is_repeated:
            _type = List(_type)

        if is_required:
            _type = NonNull(_type)

        return Field(_type)

    field = Dynamic(dynamic_type)
    return ConversionResult(name=name, field=field) 
Example #5
Source File: converter.py    From graphene-mongo with MIT License 5 votes vote down vote up
def convert_field_to_list(field, registry=None):
    base_type = convert_mongoengine_field(field.field, registry=registry)
    if isinstance(base_type, graphene.Field):
        return graphene.List(
            base_type._type,
            description=get_field_description(field, registry),
            required=field.required
        )
    if isinstance(base_type, (graphene.Dynamic)):
        base_type = base_type.get_type()
        if base_type is None:
            return
        base_type = base_type._type

    if graphene.is_node(base_type):
        return base_type._meta.connection_field_class(base_type)

    # Non-relationship field
    relations = (mongoengine.ReferenceField, mongoengine.EmbeddedDocumentField)
    if not isinstance(base_type, (graphene.List, graphene.NonNull)) and not isinstance(
        field.field, relations
    ):
        base_type = type(base_type)

    return graphene.List(
        base_type,
        description=get_field_description(field, registry),
        required=field.required,
    ) 
Example #6
Source File: test_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def test_should_onetoone_reverse_convert_model():
    class A(DjangoObjectType):
        class Meta:
            model = FilmDetails

    graphene_field = convert_django_field(Film.details.related, A._meta.registry)
    assert isinstance(graphene_field, graphene.Dynamic)
    dynamic_field = graphene_field.get_type()
    assert isinstance(dynamic_field, graphene.Field)
    assert dynamic_field.type == A 
Example #7
Source File: test_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def test_should_manytoone_convert_connectionorlist():
    class A(DjangoObjectType):
        class Meta:
            model = Article

    graphene_field = convert_django_field(Reporter.articles.rel, A._meta.registry)
    assert isinstance(graphene_field, graphene.Dynamic)
    dynamic_field = graphene_field.get_type()
    assert isinstance(dynamic_field, graphene.Field)
    # a NonNull List of NonNull A ([A!]!)
    assert isinstance(dynamic_field.type, NonNull)
    assert isinstance(dynamic_field.type.of_type, graphene.List)
    assert isinstance(dynamic_field.type.of_type.of_type, NonNull)
    assert dynamic_field.type.of_type.of_type.of_type == A 
Example #8
Source File: test_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def test_should_manytomany_convert_connectionorlist_connection():
    class A(DjangoObjectType):
        class Meta:
            model = Reporter
            interfaces = (Node,)

    graphene_field = convert_django_field(
        Reporter._meta.local_many_to_many[0], A._meta.registry
    )
    assert isinstance(graphene_field, graphene.Dynamic)
    dynamic_field = graphene_field.get_type()
    assert isinstance(dynamic_field, ConnectionField)
    assert dynamic_field.type.of_type == A._meta.connection 
Example #9
Source File: test_converter.py    From graphene-mongo with MIT License 5 votes vote down vote up
def test_should_convert_none_lazily():
    registry.reset_global_registry()
    dynamic_field = convert_mongoengine_field(
        Editor._fields["company"], registry.get_global_registry()
    )
    assert isinstance(dynamic_field, graphene.Dynamic)
    graphene_type = dynamic_field.get_type()
    assert graphene_type is None 
Example #10
Source File: test_converter.py    From graphene-mongo with MIT License 5 votes vote down vote up
def test_should_convert_none():
    registry.reset_global_registry()
    dynamic_field = convert_mongoengine_field(
        EmbeddedArticle._fields["editor"], registry.get_global_registry()
    )
    assert isinstance(dynamic_field, graphene.Dynamic)
    graphene_type = dynamic_field.get_type()
    assert graphene_type is None 
Example #11
Source File: test_converter.py    From graphene-mongo with MIT License 5 votes vote down vote up
def test_should_lazy_reference_convert_dynamic():
    class P(MongoengineObjectType):
        class Meta:
            model = Publisher
            interfaces = (graphene.Node,)

    dynamic_field = convert_mongoengine_field(
        Editor._fields["company"], P._meta.registry
    )

    assert isinstance(dynamic_field, graphene.Dynamic)
    graphene_type = dynamic_field.get_type()
    assert isinstance(graphene_type, graphene.Field)
    assert graphene_type.type == P 
Example #12
Source File: test_converter.py    From graphene-mongo with MIT License 5 votes vote down vote up
def test_should_reference_convert_dynamic():
    class E(MongoengineObjectType):
        class Meta:
            model = Editor
            interfaces = (graphene.Node,)

    dynamic_field = convert_mongoengine_field(
        EmbeddedArticle._fields["editor"], E._meta.registry
    )
    assert isinstance(dynamic_field, graphene.Dynamic)
    graphene_type = dynamic_field.get_type()
    assert isinstance(graphene_type, graphene.Field)
    assert graphene_type.type == E 
Example #13
Source File: converter.py    From graphene-mongo with MIT License 5 votes vote down vote up
def convert_field_to_dynamic(field, registry=None):
    model = field.document_type

    def dynamic_type():
        _type = registry.get_type_for_model(model)
        if not _type:
            return None
        return graphene.Field(_type, description=get_field_description(field, registry))

    return graphene.Dynamic(dynamic_type) 
Example #14
Source File: test_converter.py    From graphene-sqlalchemy with MIT License 5 votes vote down vote up
def test_should_onetoone_convert_field():
    class A(SQLAlchemyObjectType):
        class Meta:
            model = Article
            interfaces = (Node,)

    dynamic_field = convert_sqlalchemy_relationship(
        Reporter.favorite_article.property, A, default_connection_field_factory, True, 'orm_field_name',
    )
    assert isinstance(dynamic_field, graphene.Dynamic)
    graphene_type = dynamic_field.get_type()
    assert isinstance(graphene_type, graphene.Field)
    assert graphene_type.type == A 
Example #15
Source File: test_converter.py    From graphene-sqlalchemy with MIT License 5 votes vote down vote up
def test_should_manytoone_convert_connectionorlist_list():
    class A(SQLAlchemyObjectType):
        class Meta:
            model = Reporter

    dynamic_field = convert_sqlalchemy_relationship(
        Article.reporter.property, A, default_connection_field_factory, True, 'orm_field_name',
    )
    assert isinstance(dynamic_field, graphene.Dynamic)
    graphene_type = dynamic_field.get_type()
    assert isinstance(graphene_type, graphene.Field)
    assert graphene_type.type == A 
Example #16
Source File: test_converter.py    From graphene-sqlalchemy with MIT License 5 votes vote down vote up
def test_should_manytoone_convert_connectionorlist():
    class A(SQLAlchemyObjectType):
        class Meta:
            model = Article

    dynamic_field = convert_sqlalchemy_relationship(
        Reporter.pets.property, A, default_connection_field_factory, True, 'orm_field_name',
    )
    assert isinstance(dynamic_field, graphene.Dynamic)
    assert not dynamic_field.get_type() 
Example #17
Source File: test_converter.py    From graphene-sqlalchemy with MIT License 5 votes vote down vote up
def test_should_manytomany_convert_connectionorlist_connection():
    class A(SQLAlchemyObjectType):
        class Meta:
            model = Pet
            interfaces = (Node,)

    dynamic_field = convert_sqlalchemy_relationship(
        Reporter.pets.property, A, default_connection_field_factory, True, 'orm_field_name',
    )
    assert isinstance(dynamic_field, graphene.Dynamic)
    assert isinstance(dynamic_field.get_type(), UnsortedSQLAlchemyConnectionField) 
Example #18
Source File: test_converter.py    From graphene-sqlalchemy with MIT License 5 votes vote down vote up
def test_should_manytomany_convert_connectionorlist_list():
    class A(SQLAlchemyObjectType):
        class Meta:
            model = Pet

    dynamic_field = convert_sqlalchemy_relationship(
        Reporter.pets.property, A, default_connection_field_factory, True, 'orm_field_name',
    )
    assert isinstance(dynamic_field, graphene.Dynamic)
    graphene_type = dynamic_field.get_type()
    assert isinstance(graphene_type, graphene.Field)
    assert isinstance(graphene_type.type, graphene.List)
    assert graphene_type.type.of_type == A 
Example #19
Source File: test_converter.py    From graphene-sqlalchemy with MIT License 5 votes vote down vote up
def test_should_manytomany_convert_connectionorlist():
    class A(SQLAlchemyObjectType):
        class Meta:
            model = Article

    dynamic_field = convert_sqlalchemy_relationship(
        Reporter.pets.property, A, default_connection_field_factory, True, 'orm_field_name',
    )
    assert isinstance(dynamic_field, graphene.Dynamic)
    assert not dynamic_field.get_type() 
Example #20
Source File: test_converter.py    From graphql-pynamodb with MIT License 5 votes vote down vote up
def test_should_onetomany_convert_field():
    class A(PynamoObjectType):
        class Meta:
            model = Article
            interfaces = (Node,)

    dynamic_field = convert_pynamo_attribute(Reporter.articles, Reporter.articles, A._meta.registry)
    assert isinstance(dynamic_field, Dynamic)
    graphene_type = dynamic_field.get_type()
    assert isinstance(graphene_type, PynamoConnectionField) 
Example #21
Source File: test_converter.py    From graphql-pynamodb with MIT License 5 votes vote down vote up
def test_should_onetomany_none_for_unknown_type():
    class ModelA(Model):
        pass

    class ModelB(Model):
        a = OneToMany(ModelA)

    class A(PynamoObjectType):
        class Meta:
            model = ModelB

    dynamic_field = convert_pynamo_attribute(ModelB.a, ModelB.a, A._meta.registry)
    assert isinstance(dynamic_field, Dynamic)
    assert dynamic_field.get_type() is None 
Example #22
Source File: test_converter.py    From graphql-pynamodb with MIT License 5 votes vote down vote up
def test_should_onetomany_convert_nonnode_field():
    class A(PynamoObjectType):
        class Meta:
            model = Article

    dynamic_field = convert_pynamo_attribute(Reporter.articles, Reporter.articles, A._meta.registry)
    assert isinstance(dynamic_field, Dynamic)
    graphene_type = dynamic_field.get_type()
    assert isinstance(graphene_type, graphene.Field)
    assert graphene_type.type == graphene.List(A) 
Example #23
Source File: test_converter.py    From graphql-pynamodb with MIT License 5 votes vote down vote up
def test_should_onetoone_convert_field():
    class A(PynamoObjectType):
        class Meta:
            model = Article
            interfaces = [relay.Node]

    dynamic_field = convert_pynamo_attribute(Reporter.favorite_article, Reporter.favorite_article, A._meta.registry)
    assert isinstance(dynamic_field, Dynamic)
    graphene_type = dynamic_field.get_type()
    assert isinstance(graphene_type, graphene.Field)
    assert graphene_type.type == A 
Example #24
Source File: converter.py    From graphql-pynamodb with MIT License 5 votes vote down vote up
def convert_relationship_to_dynamic(type, attribute, registry=None):
    def dynamic_type():
        _type = registry.get_type_for_model(attribute.model)
        if not _type:
            return None

        if isinstance(attribute, OneToOne):
            return Field(_type)

        if isinstance(attribute, OneToMany):
            if _type._meta.connection:
                return PynamoConnectionField(_type)
            return Field(List(_type))

    return Dynamic(dynamic_type)