Example #1
0
def test_identifies_deprecated_enum_values():
    TestEnum = GraphQLEnumType(
        'TestEnum', {
            'NONDEPRECATED': 0,
            'DEPRECATED': GraphQLEnumValue(
                1, deprecation_reason='Removed in 1.0'),
            'ALSONONDEPRECATED': 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 sort_lists(result.data) == sort_lists({
        '__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_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)
    ]
Example #3
0
def test_respects_the_includedeprecated_parameter_for_enum_values():
    TestEnum = GraphQLEnumType(
        'TestEnum', {
            'NONDEPRECATED': 0,
            'DEPRECATED': GraphQLEnumValue(
                1, deprecation_reason='Removed in 1.0'),
            'ALSONONDEPRECATED': 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 sort_lists(result.data) == sort_lists({
        '__type': {
            'name':
            'TestEnum',
            'trueValues': [{
                'name': 'NONDEPRECATED'
            }, {
                'name': 'DEPRECATED'
            }, {
                'name': 'ALSONONDEPRECATED'
            }],
            'falseValues': [{
                'name': 'NONDEPRECATED'
            }, {
                'name': 'ALSONONDEPRECATED'
            }],
            'omittedValues': [{
                'name': 'NONDEPRECATED'
            }, {
                'name': 'ALSONONDEPRECATED'
            }],
        }
    })
Example #4
0
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 == '''
Example #5
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),
Example #8
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)
Example #10
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__)