def errors_respect_whitespace(): with raises(GraphQLSyntaxError) as exc_info: lex_one("\n\n ?\n\n\n") assert str(exc_info.value) == dedent(""" Syntax Error: Cannot parse the unexpected character '?'. GraphQL request (3:5) 2:\x20 3: ? ^ 4:\x20 """)
def extends_objects_by_including_new_types(): schema = build_schema(""" type Query { someObject: SomeObject } type SomeObject { oldField: String } """) new_types_sdl = """ enum NewEnum { VALUE } interface NewInterface { baz: String } type NewObject implements NewInterface { baz: String } scalar NewScalar union NewUnion = NewObject """ extend_ast = parse(new_types_sdl + """ extend type SomeObject { newObject: NewObject newInterface: NewInterface newUnion: NewUnion newScalar: NewScalar newEnum: NewEnum newTree: [SomeObject]! } """) extended_schema = extend_schema(schema, extend_ast) assert validate_schema(extended_schema) == [] assert print_schema_changes(schema, extended_schema) == dedent(""" type SomeObject { oldField: String newObject: NewObject newInterface: NewInterface newUnion: NewUnion newScalar: NewScalar newEnum: NewEnum newTree: [SomeObject]! }\n""" + new_types_sdl)
def builds_a_schema_with_a_recursive_type_reference(): sdl = dedent( """ schema { query: Recur } type Recur { recur: Recur } """ ) assert cycle_introspection(sdl) == sdl
def simple_input_enum(): body = dedent( """ enum Hello { WORLD } type Query { str(hello: Hello): String } """ ) output = cycle_output(body) assert output == body
def builds_a_schema_with_empty_deprecation_reasons(): sdl = dedent( """ type Query { someField: String @deprecated(reason: "") } enum SomeEnum { SOME_VALUE @deprecated(reason: "") } """ ) assert cycle_introspection(sdl) == sdl
def simple_type(): body = dedent( """ type Query { str: String int: Int float: Float id: ID bool: Boolean } """ ) output = cycle_output(body) assert output == body
def extends_objects_by_adding_new_fields_with_existing_types(): extended_schema = extend_test_schema(""" extend type Foo { newField(arg1: SomeEnum!): SomeEnum } """) assert print_test_schema_changes(extended_schema) == dedent(""" type Foo implements SomeInterface { name: String some: SomeInterface tree: [Foo]! newField(arg1: SomeEnum!): SomeEnum } """)
def overriding_skip_directive_excludes_built_in_one(): sdl = dedent(""" directive @skip on FIELD type Query { str: String } """) schema = build_schema(sdl) assert len(schema.directives) == 3 assert schema.get_directive("skip") is not GraphQLSkipDirective assert schema.get_directive("skip") is not None assert schema.get_directive("include") is GraphQLIncludeDirective assert schema.get_directive("deprecated") is GraphQLDeprecatedDirective
def updates_line_numbers_in_error_for_file_context(): s = "\n\n ?\n\n" source = Source(s, "foo.js", SourceLocation(11, 12)) with raises(GraphQLSyntaxError) as exc_info: Lexer(source).advance() assert str(exc_info.value) == dedent(""" Syntax Error: Cannot parse the unexpected character '?'. foo.js (13:6) 12:\x20 13: ? ^ 14:\x20 """)
def prints_string_field_with_multiple_args_first_is_default(): output = print_single_field_schema( GraphQLField(type_=GraphQLString, args={ 'argOne': GraphQLArgument(GraphQLInt, default_value=1), 'argTwo': GraphQLArgument(GraphQLString), 'argThree': GraphQLArgument(GraphQLBoolean) })) assert output == dedent(""" type Query { singleField(argOne: Int = 1, argTwo: String, argThree: Boolean): String } """) # noqa
def preserves_leading_spaces_when_printing_a_description(): description = ' This field is "awesome"' output = print_single_field_schema( GraphQLField(GraphQLString, description=description)) assert output == dedent(''' type Query { """ This field is "awesome" """ singleField: String } ''') recreated_root = build_schema(output).type_map['Query'] recreated_field = recreated_root.fields['singleField'] assert recreated_field.description == description
def simple_union(): body = dedent(""" union Hello = World type Query { hello: Hello } type World { str: String } """) output = cycle_output(body) assert output == body
def unreferenced_type_implementing_referenced_union(): body = dedent(""" type Concrete { key: String } type Query { union: Union } union Union = Concrete """) output = cycle_output(body) assert output == body
def adding_directives_maintains_skip_and_include_directives(): body = dedent(""" directive @foo(arg: Int) on FIELD type Query { str: String } """) schema = build_ast_schema(parse(body)) assert len(schema.directives) == 4 assert schema.get_directive('skip') is GraphQLSkipDirective assert schema.get_directive('include') is GraphQLIncludeDirective assert schema.get_directive('deprecated') is GraphQLDeprecatedDirective assert schema.get_directive('foo') is not None
def overriding_skip_directive_excludes_built_in_one(): body = dedent(""" directive @skip on FIELD type Query { str: String } """) schema = build_ast_schema(parse(body)) assert len(schema.directives) == 3 assert schema.get_directive('skip') is not GraphQLSkipDirective assert schema.get_directive('skip') is not None assert schema.get_directive('include') is GraphQLIncludeDirective assert schema.get_directive('deprecated') is GraphQLDeprecatedDirective
def default_root_operation_type_names(): schema = build_schema( dedent( """ type Query { str: String } type Mutation { str: String } type Subscription { str: String } """ ) ) assert schema.query_type.name == "Query" assert schema.mutation_type.name == "Mutation" assert schema.subscription_type.name == "Subscription"
def builds_a_schema_with_complex_field_values(): sdl = dedent( """ type Query { string: String listOfString: [String] nonNullString: String! nonNullListOfString: [String]! nonNullListOfNonNullString: [String!]! } """ ) assert cycle_introspection(sdl) == sdl
def may_extend_directives_with_new_directive(): schema = build_schema(""" type Query { foo: String } """) extension_sdl = dedent(''' """New directive.""" directive @new(enable: Boolean!, tag: String) repeatable on QUERY | FIELD ''') extended_schema = extend_schema(schema, parse(extension_sdl)) assert validate_schema(extended_schema) == [] assert print_schema_changes(schema, extended_schema) == extension_sdl
def builds_a_simple_schema(): sdl = dedent(''' schema { query: Simple } """This is simple type""" type Simple { """This is a string field""" string: String } ''') assert cycle_introspection(sdl) == sdl
def prints_enum(): rgb_type = GraphQLEnumType(name="RGB", values=dict.fromkeys( ("RED", "GREEN", "BLUE"))) schema = GraphQLSchema(types=[rgb_type]) output = print_for_test(schema) assert output == dedent(""" enum RGB { RED GREEN BLUE } """)
def adding_directives_maintains_skip_and_include_directives(): sdl = dedent(""" directive @foo(arg: Int) on FIELD type Query { str: String } """) schema = build_schema(sdl) assert len(schema.directives) == 4 assert schema.get_directive("skip") is GraphQLSkipDirective assert schema.get_directive("include") is GraphQLIncludeDirective assert schema.get_directive("deprecated") is GraphQLDeprecatedDirective assert schema.get_directive("foo") is not None
def prints_string_field_with_int_arg_with_default_null(): output = print_single_field_schema( GraphQLField( type_=GraphQLString, args={"argOne": GraphQLArgument(GraphQLInt, default_value=None)}, ) ) assert output == dedent( """ type Query { singleField(argOne: Int = null): String } """ )
def prints_string_field_with_multiple_args(): output = print_single_field_schema( GraphQLField( type_=GraphQLString, args={ "argOne": GraphQLArgument(GraphQLInt), "argTwo": GraphQLArgument(GraphQLString), }, )) assert output == dedent(""" type Query { singleField(argOne: Int, argTwo: String): String } """)
def input_object(): body = dedent( """ input Input { int: Int } type Query { field(in: Input): String } """ ) output = cycle_output(body) assert output == body
def one_line_prints_a_short_description(): description = "This field is awesome" output = print_single_field_schema( GraphQLField(GraphQLString, description=description)) assert output == dedent(''' type Query { """This field is awesome""" singleField: String } ''') schema = build_schema(output) recreated_root = assert_object_type(schema.type_map["Query"]) recreated_field = recreated_root.fields["singleField"] assert recreated_field.description == description
def extends_interfaces_by_adding_new_fields(): schema = build_schema(""" interface SomeInterface { oldField: String } interface AnotherInterface implements SomeInterface { oldField: String } type SomeObject implements SomeInterface & AnotherInterface { oldField: String } type Query { someInterface: SomeInterface } """) extend_ast = parse(""" extend interface SomeInterface { newField: String } extend interface AnotherInterface { newField: String } extend type SomeObject { newField: String } """) extended_schema = extend_schema(schema, extend_ast) assert validate_schema(extended_schema) == [] assert print_schema_changes(schema, extended_schema) == dedent(""" interface SomeInterface { oldField: String newField: String } interface AnotherInterface implements SomeInterface { oldField: String newField: String } type SomeObject implements SomeInterface & AnotherInterface { oldField: String newField: String } """)
def prints_string_field_with_non_null_int_arg(): output = print_single_field_schema( GraphQLField( type_=GraphQLString, args={"argOne": GraphQLArgument(non_null(GraphQLInt))}, ) ) assert output == dedent( """ type Query { singleField(argOne: Int!): String } """ )
def converts_a_simple_schema(): introspection = introspection_from_schema(schema) assert introspection_to_sdl(introspection) == dedent(''' schema { query: Simple } """This is a simple type""" type Simple { """This is a string field""" string: String } ''')
def extends_objects_by_adding_implemented_interfaces(): extended_schema = extend_test_schema(""" extend type Biz implements SomeInterface { name: String some: SomeInterface } """) assert print_test_schema_changes(extended_schema) == dedent(""" type Biz implements SomeInterface { fizz: String name: String some: SomeInterface } """)
def multiple_value_enum(): sdl = dedent( """ enum Hello { WO RLD } type Query { hello: Hello } """ ) assert cycle_sdl(sdl) == sdl