def correctly_extend_enum_type(): enum_sdl = dedent(""" enum SomeEnum { FIRST } extend enum SomeEnum { SECOND } extend enum SomeEnum { THIRD } """) schema = build_schema(enum_sdl) some_enum = assert_enum_type(schema.get_type("SomeEnum")) assert print_type(some_enum) + "\n" == dedent(""" enum SomeEnum { FIRST SECOND THIRD } """) assert print_all_ast_nodes(some_enum) == enum_sdl
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) assert second_introspection == introspection # It's also an Enum type on the client. client_food_enum = assert_enum_type(client_schema.get_type("Food")) values_dict = client_food_enum.values descriptions = {name: value.description for name, value in values_dict.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_dict.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.extensions is None for value in values) assert all(value.ast_node is None for value in values)
def extend_enums_with_deprecated_values(): extended_schema = extend_test_schema(""" extend enum SomeEnum { DEPRECATED @deprecated(reason: "do not use") } """) enum_type = assert_enum_type(extended_schema.get_type("SomeEnum")) deprecated_enum_def = enum_type.values["DEPRECATED"] assert deprecated_enum_def.is_deprecated is True assert deprecated_enum_def.deprecation_reason == "do not use"
def extend_enums_with_deprecated_values(): schema = build_schema("enum SomeEnum") extend_ast = parse(""" extend enum SomeEnum { DEPRECATED_VALUE @deprecated(reason: "do not use") } """) extended_schema = extend_schema(schema, extend_ast) some_enum = assert_enum_type(extended_schema.get_type("SomeEnum")) deprecated_value = some_enum.values["DEPRECATED_VALUE"] assert deprecated_value.deprecation_reason == "do not use"
def correctly_extend_enum_type(): schema = build_schema( """ enum SomeEnum { FIRST } extend enum SomeEnum { SECOND } extend enum SomeEnum { THIRD } """ ) some_enum = assert_enum_type(schema.get_type("SomeEnum")) assert print_type(some_enum) == dedent( """ enum SomeEnum { FIRST SECOND THIRD } """ ) expect_ast_node( some_enum, dedent( """ enum SomeEnum { FIRST } """ ), ) expect_extension_ast_nodes( some_enum, dedent( """ extend enum SomeEnum { SECOND } extend enum SomeEnum { THIRD } """ ), )
def builds_types_with_deprecated_fields_and_values(): schema = GraphQLSchema() extend_ast = parse(""" type SomeObject { deprecatedField: String @deprecated(reason: "not used anymore") } enum SomeEnum { DEPRECATED_VALUE @deprecated(reason: "do not use") } """) extended_schema = extend_schema(schema, extend_ast) some_type = assert_object_type(extended_schema.get_type("SomeObject")) deprecated_field = some_type.fields["deprecatedField"] assert deprecated_field.deprecation_reason == "not used anymore" some_enum = assert_enum_type(extended_schema.get_type("SomeEnum")) deprecated_enum = some_enum.values["DEPRECATED_VALUE"] assert deprecated_enum.deprecation_reason == "do not use"
def supports_deprecated_directive(): sdl = dedent( """ enum MyEnum { VALUE OLD_VALUE @deprecated OTHER_VALUE @deprecated(reason: "Terrible reasons") } type Query { field1: String @deprecated field2: Int @deprecated(reason: "Because I said so") enum: MyEnum } """ ) assert cycle_sdl(sdl) == sdl schema = build_schema(sdl) my_enum = assert_enum_type(schema.get_type("MyEnum")) value = my_enum.values["VALUE"] assert value.is_deprecated is False old_value = my_enum.values["OLD_VALUE"] assert old_value.is_deprecated is True assert old_value.deprecation_reason == "No longer supported" other_value = my_enum.values["OTHER_VALUE"] assert other_value.is_deprecated is True assert other_value.deprecation_reason == "Terrible reasons" root_fields = assert_object_type(schema.get_type("Query")).fields field1 = root_fields["field1"] assert field1.is_deprecated is True assert field1.deprecation_reason == "No longer supported" field2 = root_fields["field2"] assert field2.is_deprecated is True assert field2.deprecation_reason == "Because I said so"
def builds_types_with_deprecated_fields_and_values(): extended_schema = extend_test_schema(""" type TypeWithDeprecatedField { newDeprecatedField: String @deprecated(reason: "not used anymore") } enum EnumWithDeprecatedValue { DEPRECATED @deprecated(reason: "do not use") } """) deprecated_field_def = assert_object_type( extended_schema.get_type( "TypeWithDeprecatedField")).fields["newDeprecatedField"] assert deprecated_field_def.is_deprecated is True assert deprecated_field_def.deprecation_reason == "not used anymore" deprecated_enum_def = assert_enum_type( extended_schema.get_type( "EnumWithDeprecatedValue")).values["DEPRECATED"] assert deprecated_enum_def.is_deprecated is True assert deprecated_enum_def.deprecation_reason == "do not use"
def returns_true_for_enum_type(): assert is_enum_type(EnumType) is True assert_enum_type(EnumType)
def correctly_assigns_ast_nodes_to_new_and_extended_types(): extended_schema = extend_test_schema(""" extend type Query { newField(testArg: TestInput): TestEnum } extend scalar SomeScalar @foo extend enum SomeEnum { NEW_VALUE } extend union SomeUnion = Bar extend input SomeInput { newField: String } extend interface SomeInterface { newField: String } enum TestEnum { TEST_VALUE } input TestInput { testInputField: TestEnum } """) ast = parse(""" extend type Query { oneMoreNewField: TestUnion } extend scalar SomeScalar @test extend enum SomeEnum { ONE_MORE_NEW_VALUE } extend union SomeUnion = TestType extend input SomeInput { oneMoreNewField: String } extend interface SomeInterface { oneMoreNewField: String } union TestUnion = TestType interface TestInterface { interfaceField: String } type TestType implements TestInterface { interfaceField: String } directive @test(arg: Int) repeatable on FIELD | SCALAR """) extended_twice_schema = extend_schema(extended_schema, ast) query = assert_object_type(extended_twice_schema.get_type("Query")) some_enum = assert_enum_type( extended_twice_schema.get_type("SomeEnum")) some_union = assert_union_type( extended_twice_schema.get_type("SomeUnion")) some_scalar = assert_scalar_type( extended_twice_schema.get_type("SomeScalar")) some_input = assert_input_object_type( extended_twice_schema.get_type("SomeInput")) some_interface = assert_interface_type( extended_twice_schema.get_type("SomeInterface")) test_input = assert_input_object_type( extended_twice_schema.get_type("TestInput")) test_enum = assert_enum_type( extended_twice_schema.get_type("TestEnum")) test_union = assert_union_type( extended_twice_schema.get_type("TestUnion")) test_type = assert_object_type( extended_twice_schema.get_type("TestType")) test_interface = assert_interface_type( extended_twice_schema.get_type("TestInterface")) test_directive = assert_directive( extended_twice_schema.get_directive("test")) assert test_type.extension_ast_nodes is None assert test_enum.extension_ast_nodes is None assert test_union.extension_ast_nodes is None assert test_input.extension_ast_nodes is None assert test_interface.extension_ast_nodes is None assert query.extension_ast_nodes assert len(query.extension_ast_nodes) == 2 assert some_scalar.extension_ast_nodes assert len(some_scalar.extension_ast_nodes) == 2 assert some_enum.extension_ast_nodes assert len(some_enum.extension_ast_nodes) == 2 assert some_union.extension_ast_nodes assert len(some_union.extension_ast_nodes) == 2 assert some_input.extension_ast_nodes assert len(some_input.extension_ast_nodes) == 2 assert some_interface.extension_ast_nodes assert len(some_interface.extension_ast_nodes) == 2 definitions: List[Optional[TypeSystemDefinitionNode]] = [ test_input.ast_node, test_enum.ast_node, test_union.ast_node, test_interface.ast_node, test_type.ast_node, test_directive.ast_node, ] extensions: List[Optional[FrozenList[TypeSystemDefinitionNode]]] = [ query.extension_ast_nodes, some_scalar.extension_ast_nodes, some_enum.extension_ast_nodes, some_union.extension_ast_nodes, some_input.extension_ast_nodes, some_interface.extension_ast_nodes, ] for extension_ast_nodes in extensions: if extension_ast_nodes: definitions.extend(extension_ast_nodes) restored_extension_ast = DocumentNode(definitions=definitions) assert print_schema(extend_schema( test_schema, restored_extension_ast)) == print_schema(extended_twice_schema) new_field = query.fields["newField"] assert print_ast_node( new_field) == "newField(testArg: TestInput): TestEnum" assert print_ast_node( new_field.args["testArg"]) == "testArg: TestInput" assert (print_ast_node( query.fields["oneMoreNewField"]) == "oneMoreNewField: TestUnion") new_value = some_enum.values["NEW_VALUE"] assert some_enum assert print_ast_node(new_value) == "NEW_VALUE" one_more_new_value = some_enum.values["ONE_MORE_NEW_VALUE"] assert one_more_new_value assert print_ast_node(one_more_new_value) == "ONE_MORE_NEW_VALUE" assert print_ast_node( some_input.fields["newField"]) == "newField: String" assert (print_ast_node( some_input.fields["oneMoreNewField"]) == "oneMoreNewField: String") assert print_ast_node( some_interface.fields["newField"]) == "newField: String" assert (print_ast_node(some_interface.fields["oneMoreNewField"]) == "oneMoreNewField: String") assert (print_ast_node( test_input.fields["testInputField"]) == "testInputField: TestEnum") test_value = test_enum.values["TEST_VALUE"] assert test_value assert print_ast_node(test_value) == "TEST_VALUE" assert (print_ast_node(test_interface.fields["interfaceField"]) == "interfaceField: String") assert (print_ast_node( test_type.fields["interfaceField"]) == "interfaceField: String") assert print_ast_node(test_directive.args["arg"]) == "arg: Int"
def correctly_assign_ast_nodes(): sdl = dedent(""" schema { query: Query } type Query { testField(testArg: TestInput): TestUnion } input TestInput { testInputField: TestEnum } enum TestEnum { TEST_VALUE } union TestUnion = TestType interface TestInterface { interfaceField: String } type TestType implements TestInterface { interfaceField: String } scalar TestScalar directive @test(arg: TestScalar) on FIELD """) schema = build_schema(sdl) query = assert_object_type(schema.get_type("Query")) test_input = assert_input_object_type(schema.get_type("TestInput")) test_enum = assert_enum_type(schema.get_type("TestEnum")) test_union = assert_union_type(schema.get_type("TestUnion")) test_interface = assert_interface_type( schema.get_type("TestInterface")) test_type = assert_object_type(schema.get_type("TestType")) test_scalar = assert_scalar_type(schema.get_type("TestScalar")) test_directive = assert_directive(schema.get_directive("test")) restored_schema_ast = DocumentNode(definitions=[ schema.ast_node, query.ast_node, test_input.ast_node, test_enum.ast_node, test_union.ast_node, test_interface.ast_node, test_type.ast_node, test_scalar.ast_node, test_directive.ast_node, ]) assert print_ast(restored_schema_ast) == sdl test_field = query.fields["testField"] assert print_ast(test_field.ast_node) == ( "testField(testArg: TestInput): TestUnion") assert print_ast( test_field.args["testArg"].ast_node) == "testArg: TestInput" assert print_ast(test_input.fields["testInputField"].ast_node) == ( "testInputField: TestEnum") test_enum_value = test_enum.values["TEST_VALUE"] assert test_enum_value assert print_ast(test_enum_value.ast_node) == "TEST_VALUE" assert print_ast(test_interface.fields["interfaceField"].ast_node) == ( "interfaceField: String") assert print_ast( test_directive.args["arg"].ast_node) == "arg: TestScalar"
def supports_deprecated_directive(): sdl = dedent( """ enum MyEnum { VALUE OLD_VALUE @deprecated OTHER_VALUE @deprecated(reason: "Terrible reasons") } input MyInput { oldInput: String @deprecated otherInput: String @deprecated(reason: "Use newInput") newInput: String } type Query { field1: String @deprecated field2: Int @deprecated(reason: "Because I said so") enum: MyEnum field3(oldArg: String @deprecated, arg: String): String field4(oldArg: String @deprecated(reason: "Why not?"), arg: String): String field5(arg: MyInput): String } """ # noqa: E501 ) assert cycle_sdl(sdl) == sdl schema = build_schema(sdl) my_enum = assert_enum_type(schema.get_type("MyEnum")) value = my_enum.values["VALUE"] assert value.deprecation_reason is None old_value = my_enum.values["OLD_VALUE"] assert old_value.deprecation_reason == "No longer supported" other_value = my_enum.values["OTHER_VALUE"] assert other_value.deprecation_reason == "Terrible reasons" root_fields = assert_object_type(schema.get_type("Query")).fields field1 = root_fields["field1"] assert field1.deprecation_reason == "No longer supported" field2 = root_fields["field2"] assert field2.deprecation_reason == "Because I said so" input_fields = assert_input_object_type(schema.get_type("MyInput")).fields new_input = input_fields["newInput"] assert new_input.deprecation_reason is None old_input = input_fields["oldInput"] assert old_input.deprecation_reason == "No longer supported" other_input = input_fields["otherInput"] assert other_input.deprecation_reason == "Use newInput" field3_old_arg = root_fields["field3"].args["oldArg"] assert field3_old_arg.deprecation_reason == "No longer supported" field4_old_arg = root_fields["field4"].args["oldArg"] assert field4_old_arg.deprecation_reason == "Why not?"
def correctly_assigns_ast_nodes_to_new_and_extended_types(): schema = build_schema(""" type Query scalar SomeScalar enum SomeEnum union SomeUnion input SomeInput type SomeObject interface SomeInterface directive @foo on SCALAR """) first_extension_ast = parse(""" extend type Query { newField(testArg: TestInput): TestEnum } extend scalar SomeScalar @foo extend enum SomeEnum { NEW_VALUE } extend union SomeUnion = SomeObject extend input SomeInput { newField: String } extend interface SomeInterface { newField: String } enum TestEnum { TEST_VALUE } input TestInput { testInputField: TestEnum } """) extended_schema = extend_schema(schema, first_extension_ast) second_extension_ast = parse(""" extend type Query { oneMoreNewField: TestUnion } extend scalar SomeScalar @test extend enum SomeEnum { ONE_MORE_NEW_VALUE } extend union SomeUnion = TestType extend input SomeInput { oneMoreNewField: String } extend interface SomeInterface { oneMoreNewField: String } union TestUnion = TestType interface TestInterface { interfaceField: String } type TestType implements TestInterface { interfaceField: String } directive @test(arg: Int) repeatable on FIELD | SCALAR """) extended_twice_schema = extend_schema(extended_schema, second_extension_ast) extend_in_one_go_schema = extend_schema( schema, concat_ast([first_extension_ast, second_extension_ast])) assert print_schema(extend_in_one_go_schema) == print_schema( extended_twice_schema) query = assert_object_type(extended_twice_schema.get_type("Query")) some_enum = assert_enum_type( extended_twice_schema.get_type("SomeEnum")) some_union = assert_union_type( extended_twice_schema.get_type("SomeUnion")) some_scalar = assert_scalar_type( extended_twice_schema.get_type("SomeScalar")) some_input = assert_input_object_type( extended_twice_schema.get_type("SomeInput")) some_interface = assert_interface_type( extended_twice_schema.get_type("SomeInterface")) test_input = assert_input_object_type( extended_twice_schema.get_type("TestInput")) test_enum = assert_enum_type( extended_twice_schema.get_type("TestEnum")) test_union = assert_union_type( extended_twice_schema.get_type("TestUnion")) test_type = assert_object_type( extended_twice_schema.get_type("TestType")) test_interface = assert_interface_type( extended_twice_schema.get_type("TestInterface")) test_directive = assert_directive( extended_twice_schema.get_directive("test")) assert test_type.extension_ast_nodes is None assert test_enum.extension_ast_nodes is None assert test_union.extension_ast_nodes is None assert test_input.extension_ast_nodes is None assert test_interface.extension_ast_nodes is None assert query.extension_ast_nodes assert len(query.extension_ast_nodes) == 2 assert some_scalar.extension_ast_nodes assert len(some_scalar.extension_ast_nodes) == 2 assert some_enum.extension_ast_nodes assert len(some_enum.extension_ast_nodes) == 2 assert some_union.extension_ast_nodes assert len(some_union.extension_ast_nodes) == 2 assert some_input.extension_ast_nodes assert len(some_input.extension_ast_nodes) == 2 assert some_interface.extension_ast_nodes assert len(some_interface.extension_ast_nodes) == 2 assert { test_input.ast_node, test_enum.ast_node, test_union.ast_node, test_interface.ast_node, test_type.ast_node, test_directive.ast_node, *query.extension_ast_nodes, *some_scalar.extension_ast_nodes, *some_enum.extension_ast_nodes, *some_union.extension_ast_nodes, *some_input.extension_ast_nodes, *some_interface.extension_ast_nodes, } == { *first_extension_ast.definitions, *second_extension_ast.definitions } new_field = query.fields["newField"] assert print_ast_node( new_field) == "newField(testArg: TestInput): TestEnum" assert print_ast_node( new_field.args["testArg"]) == "testArg: TestInput" assert (print_ast_node( query.fields["oneMoreNewField"]) == "oneMoreNewField: TestUnion") new_value = some_enum.values["NEW_VALUE"] assert print_ast_node(new_value) == "NEW_VALUE" one_more_new_value = some_enum.values["ONE_MORE_NEW_VALUE"] assert print_ast_node(one_more_new_value) == "ONE_MORE_NEW_VALUE" assert print_ast_node( some_input.fields["newField"]) == "newField: String" assert (print_ast_node( some_input.fields["oneMoreNewField"]) == "oneMoreNewField: String") assert print_ast_node( some_interface.fields["newField"]) == "newField: String" assert (print_ast_node(some_interface.fields["oneMoreNewField"]) == "oneMoreNewField: String") assert (print_ast_node( test_input.fields["testInputField"]) == "testInputField: TestEnum") test_value = test_enum.values["TEST_VALUE"] assert print_ast_node(test_value) == "TEST_VALUE" assert (print_ast_node(test_interface.fields["interfaceField"]) == "interfaceField: String") assert (print_ast_node( test_type.fields["interfaceField"]) == "interfaceField: String") assert print_ast_node(test_directive.args["arg"]) == "arg: Int"
def returns_false_for_wrapped_enum_type(): assert is_enum_type(GraphQLList(EnumType)) is False with raises(TypeError): assert_enum_type(GraphQLList(EnumType))
def builds_a_schema_with_an_enum(): food_enum = GraphQLEnumType( "Food", { "VEGETABLES": GraphQLEnumValue( 1, description="Foods that are vegetables." ), "FRUITS": GraphQLEnumValue(2), "OILS": GraphQLEnumValue(3, deprecation_reason="Too fatty."), }, 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) assert second_introspection == introspection # It's also an Enum type on the client. client_food_enum = assert_enum_type(client_schema.get_type("Food")) # Client types do not get server-only values, so they are set to None # rather than using the integers defined in the "server" schema. values = { name: value.to_kwargs() for name, value in client_food_enum.values.items() } assert values == { "VEGETABLES": { "value": None, "description": "Foods that are vegetables.", "deprecation_reason": None, "extensions": None, "ast_node": None, }, "FRUITS": { "value": None, "description": None, "deprecation_reason": None, "extensions": None, "ast_node": None, }, "OILS": { "value": None, "description": None, "deprecation_reason": "Too fatty.", "extensions": None, "ast_node": None, }, }
def returns_false_for_non_enum_type(): assert is_enum_type(ScalarType) is False with raises(TypeError): assert_enum_type(ScalarType)
def correctly_assign_ast_nodes(): sdl = dedent( """ schema { query: Query } type Query { testField(testArg: TestInput): TestUnion } input TestInput { testInputField: TestEnum } enum TestEnum { TEST_VALUE } union TestUnion = TestType interface TestInterface { interfaceField: String } type TestType implements TestInterface { interfaceField: String } scalar TestScalar directive @test(arg: TestScalar) on FIELD """ ) ast = parse(sdl, no_location=True) schema = build_ast_schema(ast) query = assert_object_type(schema.get_type("Query")) test_input = assert_input_object_type(schema.get_type("TestInput")) test_enum = assert_enum_type(schema.get_type("TestEnum")) test_union = assert_union_type(schema.get_type("TestUnion")) test_interface = assert_interface_type(schema.get_type("TestInterface")) test_type = assert_object_type(schema.get_type("TestType")) test_scalar = assert_scalar_type(schema.get_type("TestScalar")) test_directive = assert_directive(schema.get_directive("test")) assert ( schema.ast_node, query.ast_node, test_input.ast_node, test_enum.ast_node, test_union.ast_node, test_interface.ast_node, test_type.ast_node, test_scalar.ast_node, test_directive.ast_node, ) == ast.definitions test_field = query.fields["testField"] expect_ast_node(test_field, "testField(testArg: TestInput): TestUnion") expect_ast_node(test_field.args["testArg"], "testArg: TestInput") expect_ast_node(test_input.fields["testInputField"], "testInputField: TestEnum") test_enum_value = test_enum.values["TEST_VALUE"] expect_ast_node(test_enum_value, "TEST_VALUE") expect_ast_node( test_interface.fields["interfaceField"], "interfaceField: String" ) expect_ast_node(test_directive.args["arg"], "arg: TestScalar")