def builds_a_schema_aware_of_deprecation(): schema = GraphQLSchema( GraphQLObjectType('Simple', { 'shinyString': GraphQLField(GraphQLString, description='This is a shiny string field'), 'deprecatedString': GraphQLField(GraphQLString, description='This is a deprecated string field', deprecation_reason='Use shinyString'), 'color': GraphQLField( GraphQLEnumType( 'Color', { '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') })) }, description='This is a simple type')) check_schema(schema)
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_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'}], }}
def test_enum(): class MyEnum(Enum): """Description""" foo = 1 bar = 2 @property def description(self): return "Description {}={}".format(self.name, self.value) @property def deprecation_reason(self): if self == MyEnum.foo: return "Is deprecated" type_map = create_type_map([MyEnum]) assert "MyEnum" in type_map graphql_enum = type_map["MyEnum"] assert isinstance(graphql_enum, GraphQLEnumType) assert graphql_enum.name == "MyEnum" assert graphql_enum.description == "Description" assert graphql_enum.values == { "foo": GraphQLEnumValue(value=1, description="Description foo=1", deprecation_reason="Is deprecated"), "bar": GraphQLEnumValue(value=2, description="Description bar=2"), }
def define_generic_order_types(self): self._gql_ordertypes['directionEnum'] = GraphQLEnumType( 'directionEnum', values=OrderedDict( ASC=GraphQLEnumValue(), DESC=GraphQLEnumValue() ) ) self._gql_ordertypes['nullsOrderingEnum'] = GraphQLEnumType( 'nullsOrderingEnum', values=OrderedDict( SMALLEST=GraphQLEnumValue(), BIGGEST=GraphQLEnumValue(), ) ) self._gql_ordertypes['Ordering'] = GraphQLInputObjectType( 'Ordering', fields=OrderedDict( dir=GraphQLInputObjectField( GraphQLNonNull(self._gql_ordertypes['directionEnum']), ), nulls=GraphQLInputObjectField( self._gql_ordertypes['nullsOrderingEnum'], default_value='SMALLEST', ), ) )
def prints_enum(): RGBType = GraphQLEnumType( name="RGB", values={ "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 == dedent( """ schema { query: Root } enum RGB { RED GREEN BLUE } type Root { rgb: RGB } """ )
def test_enum(): class MyEnum(Enum): '''Description''' foo = 1 bar = 2 @property def description(self): return 'Description {}={}'.format(self.name, self.value) @property def deprecation_reason(self): if self == MyEnum.foo: return 'Is deprecated' typemap = TypeMap([MyEnum]) assert 'MyEnum' in typemap graphql_enum = typemap['MyEnum'] assert isinstance(graphql_enum, GraphQLEnumType) assert graphql_enum.name == 'MyEnum' assert graphql_enum.description == 'Description' values = graphql_enum.values assert values == [ GraphQLEnumValue(name='foo', value=1, description='Description foo=1', deprecation_reason='Is deprecated'), GraphQLEnumValue(name='bar', value=2, description='Description bar=2'), ]
def prints_enum(): rgb_type = GraphQLEnumType( name="RGB", values={ "RED": GraphQLEnumValue(0), "GREEN": GraphQLEnumValue(1), "BLUE": GraphQLEnumValue(2), }, ) query = GraphQLObjectType(name="Query", fields={"rgb": GraphQLField(rgb_type)}) schema = GraphQLSchema(query) output = print_for_test(schema) assert output == dedent(""" type Query { rgb: RGB } enum RGB { RED GREEN BLUE } """)
def define_enums(self): self._gql_enums['directionEnum'] = GraphQLEnumType( 'directionEnum', values=OrderedDict( ASC=GraphQLEnumValue(), DESC=GraphQLEnumValue() ) ) self._gql_enums['nullsOrderingEnum'] = GraphQLEnumType( 'nullsOrderingEnum', values=OrderedDict( SMALLEST=GraphQLEnumValue(), BIGGEST=GraphQLEnumValue(), ) ) scalar_types = list( self.edb_schema.get_objects(modules=self.modules, type=s_scalars.ScalarType)) for st in scalar_types: if st.is_enum(self.edb_schema): name = self.get_gql_name(st.get_name(self.edb_schema)) self._gql_enums[name] = GraphQLEnumType( name, values=OrderedDict( (key, GraphQLEnumValue()) for key in st.get_enum_values(self.edb_schema) ) )
def define_enums(self): self._gql_enums['directionEnum'] = GraphQLEnumType( 'directionEnum', values=OrderedDict(ASC=GraphQLEnumValue(), DESC=GraphQLEnumValue()), description='Enum value used to specify ordering direction.', ) self._gql_enums['nullsOrderingEnum'] = GraphQLEnumType( 'nullsOrderingEnum', values=OrderedDict( SMALLEST=GraphQLEnumValue(), BIGGEST=GraphQLEnumValue(), ), description='Enum value used to specify how nulls are ordered.', ) scalar_types = list( self.edb_schema.get_objects(included_modules=self.modules, type=s_scalars.ScalarType)) for st in scalar_types: if st.is_enum(self.edb_schema): t_name = st.get_name(self.edb_schema) gql_name = self.get_gql_name(t_name) enum_type = GraphQLEnumType( gql_name, values=OrderedDict( (key, GraphQLEnumValue()) for key in st.get_enum_values(self.edb_schema)), description=self._get_description(st), ) self._gql_enums[gql_name] = enum_type self._gql_inobjtypes[f'Insert{t_name}'] = enum_type
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)
def prints_enum(): RGBType = GraphQLEnumType(name='RGB', values={ '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 == dedent(""" schema { query: Root } enum RGB { RED GREEN BLUE } type Root { rgb: RGB } """)
def builds_a_schema_with_an_enum(): food_enum = GraphQLEnumType( "Food", { "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."), }, description="Varieties of food stuffs", ) schema = GraphQLSchema( GraphQLObjectType( "EnumFields", { "food": GraphQLField( food_enum, args={ "kind": GraphQLArgument( food_enum, description="what kind of food?" ) }, description="Repeats the arg you give it", ) }, ) ) introspection = introspection_from_schema(schema) client_schema = build_client_schema(introspection) second_introspection = introspection_from_schema(client_schema) hack_to_remove_standard_types(second_introspection) hack_to_remove_standard_types(introspection) assert second_introspection == introspection client_food_enum = client_schema.get_type("Food") # It's also an Enum type on the client. assert is_enum_type(client_food_enum) values = client_food_enum.values descriptions = {name: value.description for name, value in values.items()} assert descriptions == { "VEGETABLES": "Foods that are vegetables.", "FRUITS": "Foods that are fruits.", "OILS": "Foods that are oils.", "DAIRY": "Foods that are dairy.", "MEAT": "Foods that are meat.", } values = values.values() assert all(value.value is None for value in values) assert all(value.is_deprecated is False for value in values) assert all(value.deprecation_reason is None for value in values) assert all(value.ast_node is None for value in values)
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)
def create_type(): return GraphQLEnumType( name='MedicareUtilizationLevel', values={ 'NONE': GraphQLEnumValue(), 'LOW': GraphQLEnumValue(), 'FULL': GraphQLEnumValue(), }, )
def accepts_a_well_defined_enum_type_with_internal_value_definition(): enum_type = GraphQLEnumType("SomeEnum", {"FOO": 10, "BAR": 20}) assert enum_type.values["FOO"].value == 10 assert enum_type.values["BAR"].value == 20 enum_type = GraphQLEnumType( "SomeEnum", {"FOO": GraphQLEnumValue(10), "BAR": GraphQLEnumValue(20)} ) assert enum_type.values["FOO"].value == 10 assert enum_type.values["BAR"].value == 20
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.values] == ['c', 'b', 'a', 'd']
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.values] == ['a', 'b', 'c', 'd']
def accepts_a_well_defined_enum_type_with_internal_value_definition(): enum_type = GraphQLEnumType('SomeEnum', {'FOO': 10, 'BAR': 20}) assert enum_type.values['FOO'].value == 10 assert enum_type.values['BAR'].value == 20 enum_type = GraphQLEnumType('SomeEnum', { 'FOO': GraphQLEnumValue(10), 'BAR': GraphQLEnumValue(20) }) assert enum_type.values['FOO'].value == 10 assert enum_type.values['BAR'].value == 20
def builds_a_schema_with_an_enum(): food_enum = GraphQLEnumType('Food', { '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.') }, description='Varieties of food stuffs') schema = GraphQLSchema( GraphQLObjectType( 'EnumFields', { 'food': GraphQLField(food_enum, args={ 'kind': GraphQLArgument( food_enum, description='what kind of food?') }, description='Repeats the arg you give it') })) check_schema(schema) introspection = introspection_from_schema(schema) client_schema = build_client_schema(introspection) client_food_enum = client_schema.get_type('Food') # It's also an Enum type on the client. assert isinstance(client_food_enum, GraphQLEnumType) values = client_food_enum.values descriptions = { name: value.description for name, value in values.items() } assert descriptions == { 'VEGETABLES': 'Foods that are vegetables.', 'FRUITS': 'Foods that are fruits.', 'OILS': 'Foods that are oils.', 'DAIRY': 'Foods that are dairy.', 'MEAT': 'Foods that are meat.' } values = values.values() assert all(value.value is None for value in values) assert all(value.is_deprecated is False for value in values) assert all(value.deprecation_reason is None for value in values) assert all(value.ast_node is None for value in values)
def respects_the_include_deprecated_parameter_for_enum_values(): TestEnum = GraphQLEnumType( 'TestEnum', { '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 } } } """ assert graphql_sync(schema, request) == ({ '__type': { 'name': 'TestEnum', 'trueValues': [{ 'name': 'NONDEPRECATED' }, { 'name': 'DEPRECATED' }, { 'name': 'ALSONONDEPRECATED' }], 'falseValues': [{ 'name': 'NONDEPRECATED' }, { 'name': 'ALSONONDEPRECATED' }], 'omittedValues': [{ 'name': 'NONDEPRECATED' }, { 'name': 'ALSONONDEPRECATED' }] } }, 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.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 create_type(): return GraphQLEnumType( name='ProviderStatus', values={ 'AS_SUBMITTED': GraphQLEnumValue(), 'SETTLED': GraphQLEnumValue(), 'AMENDED': GraphQLEnumValue(), 'SETTLED_WITH_AUDIT': GraphQLEnumValue(), 'REOPENED': GraphQLEnumValue(), }, )
def defines_an_enum_type_with_deprecated_value(): EnumTypeWithDeprecatedValue = GraphQLEnumType( name="EnumWithDeprecatedValue", values={"foo": GraphQLEnumValue(deprecation_reason="Just because")}, ) deprecated_value = EnumTypeWithDeprecatedValue.values["foo"] assert deprecated_value == GraphQLEnumValue(deprecation_reason="Just because") assert deprecated_value.is_deprecated is True assert deprecated_value.deprecation_reason == "Just because" assert deprecated_value.value is None assert deprecated_value.ast_node is None
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.values] == ["c", "b", "a", "d"]
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.values] == ["a", "b", "c", "d"]
def defines_an_enum_type_with_deprecated_value(): EnumTypeWithDeprecatedValue = GraphQLEnumType( name='EnumWithDeprecatedValue', values={ 'foo': GraphQLEnumValue(deprecation_reason='Just because') }) deprecated_value = EnumTypeWithDeprecatedValue.values['foo'] assert deprecated_value == GraphQLEnumValue( deprecation_reason='Just because') assert deprecated_value.is_deprecated is True assert deprecated_value.deprecation_reason == 'Just because' assert deprecated_value.value is None assert deprecated_value.ast_node is None
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" }], } }
def describe_find_deprecated_usages(): enum_type = GraphQLEnumType( "EnumType", { "ONE": GraphQLEnumValue(), "TWO": GraphQLEnumValue(deprecation_reason="Some enum reason."), }, ) schema = GraphQLSchema( GraphQLObjectType( "Query", { "normalField": GraphQLField(GraphQLString, args={"enumArg": GraphQLArgument(enum_type)}), "deprecatedField": GraphQLField(GraphQLString, deprecation_reason="Some field reason."), }, )) def should_report_empty_set_for_no_deprecated_usages(): errors = find_deprecated_usages(schema, parse("{ normalField(enumArg: ONE) }")) assert errors == [] def should_report_usage_of_deprecated_fields(): errors = find_deprecated_usages( schema, parse("{ normalField, deprecatedField }")) error_messages = [err.message for err in errors] assert error_messages == [ "The field Query.deprecatedField is deprecated. Some field reason." ] def should_report_usage_of_deprecated_enums(): errors = find_deprecated_usages(schema, parse("{ normalField(enumArg: TWO) }")) error_messages = [err.message for err in errors] assert error_messages == [ "The enum value EnumType.TWO is deprecated. Some enum reason." ]
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, }, ], } }