def test_sorts_values_if_not_using_ordered_dict():
    enum = GraphQLEnumType(name='Test', values={
        'c': GraphQLEnumValue(),
        'b': GraphQLEnumValue(),
        'a': GraphQLEnumValue(),
        'd': GraphQLEnumValue()
    })

    assert [v.name for v in enum.get_values()] == ['a', 'b', 'c', 'd']
def test_does_not_sort_values_when_using_ordered_dict():
    enum = GraphQLEnumType(name='Test', values=OrderedDict([
        ('c', GraphQLEnumValue()),
        ('b', GraphQLEnumValue()),
        ('a', GraphQLEnumValue()),
        ('d', GraphQLEnumValue()),
    ]))

    assert [v.name for v in enum.get_values()] == ['c', 'b', 'a', 'd']
Exemple #3
0
def test_does_not_sort_values_when_using_ordered_dict():
    enum = GraphQLEnumType(name='Test',
                           values=OrderedDict([
                               ('c', GraphQLEnumValue()),
                               ('b', GraphQLEnumValue()),
                               ('a', GraphQLEnumValue()),
                               ('d', GraphQLEnumValue()),
                           ]))

    assert [v.name for v in enum.get_values()] == ['c', 'b', 'a', 'd']
Exemple #4
0
def test_sorts_values_if_not_using_ordered_dict():
    enum = GraphQLEnumType(name='Test',
                           values={
                               'c': GraphQLEnumValue(),
                               'b': GraphQLEnumValue(),
                               'a': GraphQLEnumValue(),
                               'd': GraphQLEnumValue()
                           })

    assert [v.name for v in enum.get_values()] == ['a', 'b', 'c', 'd']
Exemple #5
0
def test_respects_the_includedeprecated_parameter_for_enum_values():
    TestEnum = GraphQLEnumType('TestEnum', OrderedDict([
        ('NONDEPRECATED', GraphQLEnumValue(0)),
        ('DEPRECATED', GraphQLEnumValue(1, deprecation_reason='Removed in 1.0')),
        ('ALSONONDEPRECATED', GraphQLEnumValue(2))
    ]))
    TestType = GraphQLObjectType('TestType', {
        'testEnum': GraphQLField(TestEnum)
    })
    schema = GraphQLSchema(TestType)
    request = '''{__type(name: "TestEnum") {
        name
        trueValues: enumValues(includeDeprecated: true) { name }
        falseValues: enumValues(includeDeprecated: false) { name }
        omittedValues: enumValues { name }
    } }'''
    result = graphql(schema, request)
    assert not result.errors
    assert result.data == {'__type': {
        'name': 'TestEnum',
        'trueValues': [{'name': 'NONDEPRECATED'}, {'name': 'DEPRECATED'},
                       {'name': 'ALSONONDEPRECATED'}],
        'falseValues': [{'name': 'NONDEPRECATED'},
                        {'name': 'ALSONONDEPRECATED'}],
        'omittedValues': [{'name': 'NONDEPRECATED'},
                          {'name': 'ALSONONDEPRECATED'}],
    }}
Exemple #6
0
def test_identifies_deprecated_enum_values():
    TestEnum = GraphQLEnumType('TestEnum', OrderedDict([
        ('NONDEPRECATED', GraphQLEnumValue(0)),
        ('DEPRECATED', GraphQLEnumValue(1, deprecation_reason='Removed in 1.0')),
        ('ALSONONDEPRECATED', GraphQLEnumValue(2))
    ]))
    TestType = GraphQLObjectType('TestType', {
        'testEnum': GraphQLField(TestEnum)
    })
    schema = GraphQLSchema(TestType)
    request = '''{__type(name: "TestEnum") {
        name
        enumValues(includeDeprecated: true) {
            name
            isDeprecated
            deprecationReason
        }
    } }'''
    result = graphql(schema, request)
    assert not result.errors
    assert result.data == {'__type': {
        'name': 'TestEnum',
        'enumValues': [
            {'name': 'NONDEPRECATED', 'isDeprecated': False, 'deprecationReason': None},
            {'name': 'DEPRECATED', 'isDeprecated': True, 'deprecationReason': 'Removed in 1.0'},
            {'name': 'ALSONONDEPRECATED', 'isDeprecated': False, 'deprecationReason': None},
        ]}}
def test_builds_a_schema_aware_of_deprecation():
    schema = GraphQLSchema(query=GraphQLObjectType(
        name='Simple',
        description='This is a simple type',
        fields=OrderedDict(
            [('shinyString',
              GraphQLField(type=GraphQLString,
                           description='This is a shiny string field')),
             ('deprecatedString',
              GraphQLField(type=GraphQLString,
                           description='This is a deprecated string field',
                           deprecation_reason='Use shinyString')),
             ('color',
              GraphQLField(type=GraphQLEnumType(
                  name='Color',
                  values=OrderedDict([
                      ('RED', GraphQLEnumValue(description='So rosy')),
                      ('GREEN', GraphQLEnumValue(description='So grassy')),
                      ('BLUE',
                       GraphQLEnumValue(description='So calming')),
                      ('MAUVE',
                       GraphQLEnumValue(
                           description='So sickening',
                           deprecation_reason='No longer in fashion')),
                  ]))))])))

    _test_schema(schema)
Exemple #8
0
    def internal_type(cls, schema):
        if cls._meta.abstract:
            raise Exception("Abstract Enum don't have a specific type.")

        values = {k: GraphQLEnumValue(v.value) for k, v in cls.__enum__.__members__.items()}
        # GraphQLEnumValue
        return GraphQLEnumType(
            cls._meta.type_name,
            values=values,
            description=cls._meta.description,
        )
def test_builds_a_schema_with_an_enum():
    FoodEnum = GraphQLEnumType(
        name='Food',
        description='Varieties of food stuffs',
        values=OrderedDict([
            ('VEGETABLES',
             GraphQLEnumValue(1, description='Foods that are vegetables.')),
            ('FRUITS', GraphQLEnumValue(2,
                                        description='Foods that are fruits.')),
            ('OILS', GraphQLEnumValue(3, description='Foods that are oils.')),
            ('DAIRY', GraphQLEnumValue(4,
                                       description='Foods that are dairy.')),
            ('MEAT', GraphQLEnumValue(5, description='Foods that are meat.')),
        ]))

    schema = GraphQLSchema(query=GraphQLObjectType(
        name='EnumFields',
        fields={
            'food':
            GraphQLField(FoodEnum,
                         description='Repeats the arg you give it',
                         args={
                             'kind':
                             GraphQLArgument(FoodEnum,
                                             description='what kind of food?')
                         })
        }))

    client_schema = _test_schema(schema)
    clientFoodEnum = client_schema.get_type('Food')
    assert isinstance(clientFoodEnum, GraphQLEnumType)

    assert clientFoodEnum.get_values() == [
        GraphQLEnumValue(name='VEGETABLES',
                         value='VEGETABLES',
                         description='Foods that are vegetables.',
                         deprecation_reason=None),
        GraphQLEnumValue(name='FRUITS',
                         value='FRUITS',
                         description='Foods that are fruits.',
                         deprecation_reason=None),
        GraphQLEnumValue(name='OILS',
                         value='OILS',
                         description='Foods that are oils.',
                         deprecation_reason=None),
        GraphQLEnumValue(name='DAIRY',
                         value='DAIRY',
                         description='Foods that are dairy.',
                         deprecation_reason=None),
        GraphQLEnumValue(name='MEAT',
                         value='MEAT',
                         description='Foods that are meat.',
                         deprecation_reason=None)
    ]
def test_print_enum():
    RGBType = GraphQLEnumType(name='RGB',
                              values=OrderedDict([
                                  ('RED', GraphQLEnumValue(0)),
                                  ('GREEN', GraphQLEnumValue(1)),
                                  ('BLUE', GraphQLEnumValue(2))
                              ]))

    Root = GraphQLObjectType(name='Root',
                             fields={'rgb': GraphQLField(RGBType)})

    Schema = GraphQLSchema(Root)
    output = print_for_test(Schema)

    assert output == '''
Exemple #11
0
        '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
    assert isinstance(article_field_type, GraphQLObjectType)
Being = GraphQLInterfaceType('Being', {
    'name': GraphQLField(GraphQLString, {
        'surname': GraphQLArgument(GraphQLBoolean),
    })
})

Pet = GraphQLInterfaceType('Pet', {
    'name': GraphQLField(GraphQLString, {
        'surname': GraphQLArgument(GraphQLBoolean),
    }),
})

DogCommand = GraphQLEnumType('DogCommand', {
    'SIT': GraphQLEnumValue(0),
    'HEEL': GraphQLEnumValue(1),
    'DOWN': GraphQLEnumValue(2),
})

Dog = GraphQLObjectType('Dog', {
    'name': GraphQLField(GraphQLString, {
        'surname': GraphQLArgument(GraphQLBoolean),
    }),
    'nickname': GraphQLField(GraphQLString),
    'barkVolume': GraphQLField(GraphQLInt),
    'barks': GraphQLField(GraphQLBoolean),
    'doesKnowCommand': GraphQLField(GraphQLBoolean, {
        'dogCommand': GraphQLArgument(DogCommand)
    }),
    'isHousetrained': GraphQLField(
        GraphQLBoolean,
    GraphQLList,
    GraphQLNonNull,
    GraphQLSchema,
    GraphQLString,
)
import starwars_fixtures

episodeEnum = GraphQLEnumType(
    'Episode',
    description='One of the films in the Star Wars Trilogy',
    values={
        'NEWHOPE': GraphQLEnumValue(
            4,
            description='Released in 1977.',
        ),
        'EMPIRE': GraphQLEnumValue(
            5,
            description='Released in 1980.',
        ),
        'JEDI': GraphQLEnumValue(
            6,
            description='Released in 1983.',
        )
    })

characterInterface = GraphQLInterfaceType(
    'Character',
    description='A character in the Star Wars Trilogy',
    fields=lambda: {
        'id':
        GraphQLField(GraphQLNonNull(GraphQLString),
Exemple #14
0
from collections import OrderedDict
from pytest import raises
from graphql.core.type import (GraphQLEnumType, GraphQLEnumValue,
                               GraphQLObjectType, GraphQLField,
                               GraphQLArgument, GraphQLInt, GraphQLString,
                               GraphQLSchema)
from graphql.core import graphql

ColorType = GraphQLEnumType(name='Color',
                            values=OrderedDict([('RED', GraphQLEnumValue(0)),
                                                ('GREEN', GraphQLEnumValue(1)),
                                                ('BLUE', GraphQLEnumValue(2))
                                                ]))


def get_first(args, *keys):
    for key in keys:
        if key in args:
            return args[key]

    return None


QueryType = GraphQLObjectType(
    name='Query',
    fields={
        'colorEnum':
        GraphQLField(type=ColorType,
                     args={
                         'fromEnum': GraphQLArgument(ColorType),
                         'fromInt': GraphQLArgument(GraphQLInt),
    })

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
    assert isinstance(article_field_type, GraphQLObjectType)
Exemple #16
0
def enum_to_graphql_enum(enumeration):
    return GraphQLEnumType(name=enumeration.__name__,
                           values=OrderedDict([(it.name,
                                                GraphQLEnumValue(it.value))
                                               for it in enumeration]),
                           description=enumeration.__doc__)