Python graphene.String() Examples

The following are 30 code examples of graphene.String(). 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-sqlalchemy with MIT License 6 votes vote down vote up
def test_should_composite_convert():
    registry = Registry()

    class CompositeClass:
        def __init__(self, col1, col2):
            self.col1 = col1
            self.col2 = col2

    @convert_sqlalchemy_composite.register(CompositeClass, registry)
    def convert_composite_class(composite, registry):
        return graphene.String(description=composite.doc)

    field = convert_sqlalchemy_composite(
        composite(CompositeClass, (Column(types.Unicode(50)), Column(types.Unicode(50))), doc="Custom Help Text"),
        registry,
        mock_resolver,
    )
    assert isinstance(field, graphene.String) 
Example #2
Source File: test_schema.py    From graphql-over-kafka with MIT License 6 votes vote down vote up
def test_does_not_auto_camel_case(self):

        # a query to test with a snake case field
        class TestQuery(ObjectType):
            test_field = String()

            def resolve_test_field(self, args, info):
                return 'hello'

        # assign the query to the schema
        self.schema.query = TestQuery

        # the query to test
        test_query = "query {test_field}"

        # execute the query
        resolved_query = self.schema.execute(test_query)

        assert 'test_field' in resolved_query.data, (
            "Schema did not have snake_case field."
        )

        assert resolved_query.data['test_field'] == 'hello', (
            "Snake_case field did not have the right value"
        ) 
Example #3
Source File: pagination.py    From graphene-django-extras with MIT License 6 votes vote down vote up
def to_graphql_fields(self):
        return {
            self.limit_query_param: Int(
                default_value=self.default_limit,
                description="Number of results to return per page. Default "
                "'default_limit': {}, and 'max_limit': {}".format(
                    self.default_limit, self.max_limit
                ),
            ),
            self.offset_query_param: Int(
                description="The initial index from which to return the results. Default: 0"
            ),
            self.ordering_param: String(
                description="A string or comma delimited string values that indicate the "
                "default ordering when obtaining lists of objects."
            ),
        } 
Example #4
Source File: service.py    From graphene-federation with MIT License 6 votes vote down vote up
def get_service_query(schema):
    sdl_str = get_sdl(schema, custom_entities)

    class _Service(ObjectType):
        sdl = String()

        def resolve_sdl(parent, _):
            return sdl_str

    class ServiceQuery(ObjectType):
        _service = Field(_Service, name="_service")

        def resolve__service(parent, info):
            return _Service()

    return ServiceQuery 
Example #5
Source File: test_converter.py    From graphene-django with MIT License 6 votes vote down vote up
def test_should_postgres_array_multiple_convert_list():
    field = assert_conversion(
        ArrayField, graphene.List, ArrayField(models.CharField(max_length=100))
    )
    assert isinstance(field.type, graphene.NonNull)
    assert isinstance(field.type.of_type, graphene.List)
    assert isinstance(field.type.of_type.of_type, graphene.List)
    assert isinstance(field.type.of_type.of_type.of_type, graphene.NonNull)
    assert field.type.of_type.of_type.of_type.of_type == graphene.String

    field = assert_conversion(
        ArrayField,
        graphene.List,
        ArrayField(models.CharField(max_length=100, null=True)),
    )
    assert isinstance(field.type, graphene.NonNull)
    assert isinstance(field.type.of_type, graphene.List)
    assert isinstance(field.type.of_type.of_type, graphene.List)
    assert field.type.of_type.of_type.of_type == graphene.String 
Example #6
Source File: serializer_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def convert_serializer_field(field, is_input=True, convert_choices_to_enum=True):
    """
    Converts a django rest frameworks field to a graphql field
    and marks the field as required if we are creating an input type
    and the field itself is required
    """

    if isinstance(field, serializers.ChoiceField) and not convert_choices_to_enum:
        graphql_type = graphene.String
    else:
        graphql_type = get_graphene_type_from_serializer_field(field)

    args = []
    kwargs = {"description": field.help_text, "required": is_input and field.required}

    # if it is a tuple or a list it means that we are returning
    # the graphql type and the child type
    if isinstance(graphql_type, (list, tuple)):
        kwargs["of_type"] = graphql_type[1]
        graphql_type = graphql_type[0]

    if isinstance(field, serializers.ModelSerializer):
        if is_input:
            graphql_type = convert_serializer_to_input_type(field.__class__)
        else:
            global_registry = get_global_registry()
            field_model = field.Meta.model
            args = [global_registry.get_type_for_model(field_model)]
    elif isinstance(field, serializers.ListSerializer):
        field = field.child
        if is_input:
            kwargs["of_type"] = convert_serializer_to_input_type(field.__class__)
        else:
            del kwargs["of_type"]
            global_registry = get_global_registry()
            field_model = field.Meta.model
            args = [global_registry.get_type_for_model(field_model)]

    return graphql_type(*args, **kwargs) 
Example #7
Source File: test_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def test_should_ipaddress_convert_string():
    assert_conversion(models.GenericIPAddressField, graphene.String) 
Example #8
Source File: test_field_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def test_should_char_convert_string():
    assert_conversion(serializers.CharField, graphene.String) 
Example #9
Source File: test_field_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def test_should_email_convert_string():
    assert_conversion(serializers.EmailField, graphene.String) 
Example #10
Source File: test_field_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def test_should_url_convert_string():
    assert_conversion(serializers.URLField, graphene.String) 
Example #11
Source File: test_field_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def test_should_choice_convert_string_if_enum_disabled():
    assert_conversion(
        serializers.ChoiceField,
        graphene.String,
        choices=[("h", "Hello"), ("w", "World")],
        source="word",
        convert_choices_to_enum=False,
    ) 
Example #12
Source File: test_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def test_should_text_convert_string():
    assert_conversion(models.TextField, graphene.String) 
Example #13
Source File: test_types.py    From graphene-django with MIT License 5 votes vote down vote up
def test_django_objecttype_fields_exist_on_model():
    with pytest.warns(UserWarning, match=r"Field name .* doesn't exist"):

        class Reporter(DjangoObjectType):
            class Meta:
                model = ReporterModel
                fields = ["first_name", "foo", "email"]

    with pytest.warns(
        UserWarning,
        match=r"Field name .* matches an attribute on Django model .* but it's not a model field",
    ) as record:

        class Reporter2(DjangoObjectType):
            class Meta:
                model = ReporterModel
                fields = ["first_name", "some_method", "email"]

    # Don't warn if selecting a custom field
    with pytest.warns(None) as record:

        class Reporter3(DjangoObjectType):
            custom_field = String()

            class Meta:
                model = ReporterModel
                fields = ["first_name", "custom_field", "email"]

    assert len(record) == 0 
Example #14
Source File: test_types.py    From graphene-django with MIT License 5 votes vote down vote up
def test_django_objecttype_convert_choices_enum_false(self, PetModel):
        class Pet(DjangoObjectType):
            class Meta:
                model = PetModel
                convert_choices_to_enum = False

        class Query(ObjectType):
            pet = Field(Pet)

        schema = Schema(query=Query)

        assert str(schema) == dedent(
            """\
        schema {
          query: Query
        }

        type Pet {
          id: ID!
          kind: String!
          cuteness: Int!
        }

        type Query {
          pet: Pet
        }
        """
        ) 
Example #15
Source File: test_types.py    From graphene-django with MIT License 5 votes vote down vote up
def test_django_objecttype_convert_choices_enum_empty_list(self, PetModel):
        class Pet(DjangoObjectType):
            class Meta:
                model = PetModel
                convert_choices_to_enum = []

        class Query(ObjectType):
            pet = Field(Pet)

        schema = Schema(query=Query)

        assert str(schema) == dedent(
            """\
        schema {
          query: Query
        }

        type Pet {
          id: ID!
          kind: String!
          cuteness: Int!
        }

        type Query {
          pet: Pet
        }
        """
        ) 
Example #16
Source File: test_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def test_should_char_convert_string():
    assert_conversion(models.CharField, graphene.String) 
Example #17
Source File: test_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def test_should_url_convert_string():
    assert_conversion(models.URLField, graphene.String) 
Example #18
Source File: converter.py    From graphene-django with MIT License 5 votes vote down vote up
def convert_form_field_to_string(field):
    return String(description=field.help_text, required=field.required) 
Example #19
Source File: test_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def test_should_postgres_array_convert_list():
    field = assert_conversion(
        ArrayField, graphene.List, models.CharField(max_length=100)
    )
    assert isinstance(field.type, graphene.NonNull)
    assert isinstance(field.type.of_type, graphene.List)
    assert isinstance(field.type.of_type.of_type, graphene.NonNull)
    assert field.type.of_type.of_type.of_type == graphene.String

    field = assert_conversion(
        ArrayField, graphene.List, models.CharField(max_length=100, null=True)
    )
    assert isinstance(field.type, graphene.NonNull)
    assert isinstance(field.type.of_type, graphene.List)
    assert field.type.of_type.of_type == graphene.String 
Example #20
Source File: test_util.py    From graphql-over-kafka with MIT License 5 votes vote down vote up
def test_serialize_native_type(self):
        # make sure it converts a native string to 'String'
        import nautilus.models.fields as fields
        assert serialize_native_type(fields.CharField()) == 'String', (
            "Could not serialize native type."
        ) 
Example #21
Source File: test_util.py    From graphql-over-kafka with MIT License 5 votes vote down vote up
def test_convert_typestring_to_api_native(self):
        # make sure it converts String to the correct class
        assert convert_typestring_to_api_native('String') == graphene.String, (
            "Could not convert String to native representation."
        ) 
Example #22
Source File: test_util.py    From graphql-over-kafka with MIT License 5 votes vote down vote up
def test_fields_for_model(self):
        # a mock to test with
        model = MockModel()
        # create the dictionary of fields for the model
        fields = fields_for_model(model)

        assert 'name' in fields and 'date' in fields and 'id' in fields , (
            "Could not create correct fields for model"
        )
        assert isinstance(fields['date'], graphene.String) , (
            "Field summary did not have the correct type"
        ) 
Example #23
Source File: test_converter.py    From graphene-mongo with MIT License 5 votes vote down vote up
def test_should_multipolygon_convert_field():
    graphene_type = convert_mongoengine_field(mongoengine.MultiPolygonField())
    assert isinstance(graphene_type, graphene.Field)
    assert graphene_type.type == advanced_types.MultiPolygonFieldType
    assert isinstance(graphene_type.type.type, graphene.String)
    assert isinstance(graphene_type.type.coordinates, graphene.List) 
Example #24
Source File: test_converter.py    From graphene-mongo with MIT License 5 votes vote down vote up
def test_should_polygon_covert_field():
    graphene_type = convert_mongoengine_field(mongoengine.PolygonField())
    assert isinstance(graphene_type, graphene.Field)
    assert graphene_type.type == advanced_types.PolygonFieldType
    assert isinstance(graphene_type.type.type, graphene.String)
    assert isinstance(graphene_type.type.coordinates, graphene.List) 
Example #25
Source File: test_converter.py    From graphene-mongo with MIT License 5 votes vote down vote up
def test_should_point_convert_field():
    graphene_type = convert_mongoengine_field(mongoengine.PointField())
    assert isinstance(graphene_type, graphene.Field)
    assert graphene_type.type == advanced_types.PointFieldType
    assert isinstance(graphene_type.type.type, graphene.String)
    assert isinstance(graphene_type.type.coordinates, graphene.List) 
Example #26
Source File: test_converter.py    From graphene-mongo with MIT License 5 votes vote down vote up
def test_should_url_convert_string():
    assert_conversion(mongoengine.URLField, graphene.String) 
Example #27
Source File: converter.py    From graphene-mongo with MIT License 5 votes vote down vote up
def convert_field_to_string(field, registry=None):
    return graphene.String(
        description=get_field_description(field, registry), required=field.required
    ) 
Example #28
Source File: test_converter.py    From graphene-mongo with MIT License 5 votes vote down vote up
def test_should_email_convert_string():
    assert_conversion(mongoengine.EmailField, graphene.String) 
Example #29
Source File: test_query.py    From graphene-mongo with MIT License 5 votes vote down vote up
def test_should_query_with_embedded_document(fixtures):
    class Query(graphene.ObjectType):
        professor_vector = graphene.Field(
            types.ProfessorVectorType, id=graphene.String()
        )

        def resolve_professor_vector(self, info, id):
            return models.ProfessorVector.objects(metadata__id=id).first()

    query = """
        query {
          professorVector(id: "5e06aa20-6805-4eef-a144-5615dedbe32b") {
            vec
            metadata {
                firstName
            }
          }
        }
    """

    expected = {
        "professorVector": {"vec": [1.0, 2.3], "metadata": {"firstName": "Steven"}}
    }
    schema = graphene.Schema(query=Query, types=[types.ProfessorVectorType])
    result = schema.execute(query)
    assert not result.errors
    assert result.data == expected 
Example #30
Source File: test_field_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def test_should_base_field_convert_string():
    assert_conversion(serializers.Field, graphene.String)