def test_query_operation_without_name(): assert print_ast(parse("query { id, name }")) == dedent(""" { id name } """)
def test_hides_interface(schema): assert ( dedent( """ directive @barDirective(arg: SomeEnum, other_arg: Int) on FIELD directive @fooDirective on FIELD type Bar { name: String } type Foo { name: String } type Query { a(arg: SomeEnum, other_arg: Int): String foo: Foo union: Thing uid: UUID } enum SomeEnum { FOO BAR } union Thing = Foo | Bar scalar UUID """ ) == _sdl(transform_schema(schema, _hide_type_transform("IFace"))) )
def test_unions(): Foo = ObjectType("Foo", [Field("str", String)]) Bar = ObjectType("Bar", [Field("int", Int)]) SingleUnion = UnionType("SingleUnion", types=[Foo]) MultiUnion = UnionType("MultiUnion", types=[Foo, Bar]) Query = ObjectType( "Query", [Field("single", SingleUnion), Field("multi", MultiUnion)]) assert print_schema(Schema(Query), indent=" ") == dedent(""" type Bar { int: Int } type Foo { str: String } union MultiUnion = Foo | Bar type Query { single: SingleUnion multi: MultiUnion } union SingleUnion = Foo """)
def test_hides_enum(schema): assert ( dedent( """ directive @barDirective(other_arg: Int) on FIELD directive @fooDirective on FIELD type Bar implements IFace { name: String } type Foo implements IFace { name: String } interface IFace { name: String } type Query { a(other_arg: Int): String foo: Foo iface: IFace union: Thing uid: UUID } union Thing = Foo | Bar scalar UUID """ ) == _sdl(transform_schema(schema, _hide_type_transform("SomeEnum"))) )
def test_custom_indentation_object(): assert (ASTPrinter(indent=4)(parse(""" { bar { one two ... on Object { three } ...A } } fragment A on Object { four five } """)) == dedent(""" { bar { one two ... on Object { three } ...A } } fragment A on Object { four five } """))
def test_hides_object(schema): assert ( dedent( """ directive @barDirective(arg: SomeEnum, other_arg: Int) on FIELD directive @fooDirective on FIELD type Bar implements IFace { name: String } interface IFace { name: String } type Query { a(arg: SomeEnum, other_arg: Int): String iface: IFace union: Thing uid: UUID } enum SomeEnum { FOO BAR } union Thing = Bar scalar UUID """ ) == _sdl(transform_schema(schema, _hide_type_transform("Foo"))) )
def test_enum_extension(): assert (build_schema(""" type Query { foo: Foo } enum Foo { BLUE GREEN RED } extend enum Foo { YELLOW } """).to_string() == dedent(""" enum Foo { BLUE GREEN RED YELLOW } type Query { foo: Foo } """))
def assert_validation_result(schema, source, expected_msgs=None, expected_locs=None, checkers=None): # Prints are here so we can more easily debug when running pytest with -v expected_msgs = expected_msgs or [] expected_locs = expected_locs or [] print(source) result = validate_ast( schema, parse(dedent(source), allow_type_system=True), validators=[ lambda s, d, v: default_validator( s, d, v, validators=(checkers or SPECIFIED_RULES)) ], ) errors = result.errors msgs = [str(err) for err in errors] locs = [[node.loc for node in err.nodes] for err in errors] print(" [msgs] ", msgs) print(" [locs] ", locs) assert msgs == expected_msgs if expected_locs: assert locs == [_ensure_list(l) for l in expected_locs]
def test_interfaces(): Foo = InterfaceType("Foo", [Field("str", String)]) Baz = InterfaceType("Baz", [Field("int", Int)]) Bar = ObjectType( "Bar", [Field("str", String), Field("int", Int)], interfaces=[Foo, Baz]) Query = ObjectType("Query", [Field("bar", Bar)]) assert print_schema(Schema(Query), indent=" ") == dedent(""" type Bar implements Foo & Baz { str: String int: Int } interface Baz { int: Int } interface Foo { str: String } type Query { bar: Bar } """)
def test_union_type_extension(): assert (build_schema(""" type Query { foo: Foo } type Bar { bar: Int } type Baz { baz: Int } union Foo = Bar extend union Foo = Baz """).to_string() == dedent(""" type Bar { bar: Int } type Baz { baz: Int } union Foo = Bar | Baz type Query { foo: Foo } """))
def test_it_adds_new_type_definitions_and_opeations_to_schema(): assert (extend_schema( BASE_SCHEMA, """ scalar UUID type SomeMutation { bar: UUID } extend schema { mutation: SomeMutation } """, ).to_string() == dedent(""" schema { query: Query mutation: SomeMutation } type Query { foo: Int } type SomeMutation { bar: UUID } scalar UUID """))
def test_injected_object_type_extension(): Foo = ObjectType("Foo", [Field("one", String)]) schema = build_schema( """ type Query { foo: Foo } extend type Foo { two: Int } """, additional_types=[Foo], ) assert schema.to_string() == dedent(""" type Foo { one: String two: Int } type Query { foo: Foo } """) assert schema.types["Foo"] is not Foo
def test_string_field_with_int_arg(): schema = _single_field_schema(String, args=[Argument("argOne", Int)]) assert print_schema(schema, indent=" ") == dedent(""" type Query { singleField(argOne: Int): String } """)
def test_mutation(): assert (dedent(""" type Mutation { foo: Int } """) == print_schema( Schema(mutation_type=ObjectType("Mutation", [Field("foo", Int)]))))
def test_build_schema_ignores_extensions_if_specified(): assert ( build_schema( """ type Query { one: Int } extend type Object { one: Int } extend type Query { two: String } """, ignore_extensions=True, ).to_string() == dedent( """ type Query { one: Int } """ ) )
def test_mutation_operation_without_name(): assert print_ast(parse("mutation { id, name }")) == dedent(""" mutation { id name } """)
def test_SnakeCaseToCamelCaseVisitor(): query = parse(""" { foo_bar { bar_foo ... on Object { baz_foo } } } fragment A on Object { foo_baz } """) visited_query = ast_transforms.SnakeCaseToCamelCaseVisitor().visit( query.deepcopy()) assert visited_query assert print_ast(visited_query, indent=4) == dedent(""" { fooBar { barFoo ... on Object { bazFoo } } } fragment A on Object { fooBaz } """)
def test_single_field_schema(type_, opts, expected): assert (dedent(""" type Query { singleField: %s } """ % expected) == print_schema(_single_field_schema( type_, **opts), indent=" "))
def test_string_field_with_int_arg_with_null_default_value(): schema = _single_field_schema( String, args=[Argument("argOne", Int, default_value=None)]) assert print_schema(schema, indent=" ") == dedent(""" type Query { singleField(argOne: Int = null): String } """)
def test_subscription(): assert (dedent(""" type Subscription { foo: Int } """) == print_schema( Schema( subscription_type=ObjectType("Subscription", [Field("foo", Int)]))) )
def test_block_string_single_line_with_leading_space(): assert (print_ast( parse(''' { field(arg: """ space-led value""") } ''')) == dedent(''' { field(arg: """ space-led value""") } '''))
def test_string_field_with_multiple_args(): schema = _single_field_schema( String, args=[Argument("argOne", Int), Argument("argTwo", String)]) assert print_schema(schema, indent=" ") == dedent(""" type Query { singleField(argOne: Int, argTwo: String): String } """)
def test_GraphQLResult_json(): assert GraphQLResult(data={ "foo": 42 }).json(indent=4) == dedent(""" { "data": { "foo": 42 } }""")
def test_kitchen_sink(fixture_file): ks = fixture_file("kitchen-sink.graphql") assert print_ast(parse(ks)) == dedent(''' query queryName($foo: ComplexType, $site: Site = MOBILE) { whoever123is: node(id: [123, 456]) { id ... on User @defer { field2 { id alias: field1(first: 10, after: $foo) @include(if: $foo) { id ...frag } } } ... @skip(unless: $foo) { id } ... { id } } } mutation likeStory { like(story: 123) @defer { story { id } } } subscription StoryLikeSubscription($input: StoryLikeSubscribeInput) { storyLikeSubscribe(input: $input) { story { likers { count } likeSentence { text } } } } fragment frag on Friend { foo(size: $size, bar: $b, obj: {key: "value", block: """ block string uses \\""" """}) } { unnamed(truthy: true, falsey: false, nullish: null) query } ''')
def test_mutation_operation_without_name_and_artifacts(): assert (print_ast( parse(""" mutation ($foo: TestType) @testDirective { id, name } """)) == dedent(""" mutation ($foo: TestType) @testDirective { id name } """))
def test_string_field_with_string_arg_with_default_value(): schema = _single_field_schema( String, args=[Argument("argOne", String, default_value="tes\t de\fault")], ) assert print_schema(schema, indent=" ") == dedent(""" type Query { singleField(argOne: String = "tes\\t de\\fault"): String } """)
def test_custom_scalar_regex_type(): assert print_schema(_single_field_schema(RegexType("BBQ", r"^BBQ$")), indent=" ") == dedent(''' """String matching pattern /^BBQ$/""" scalar BBQ type Query { singleField: BBQ } ''')
def test_it_adds_new_directives_to_schema(): assert extend_schema( BASE_SCHEMA, "directive @some(a: Boolean!) on FIELD").to_string() == dedent(""" directive @some(a: Boolean!) on FIELD type Query { foo: Int } """)
def test_description_fits_on_one_line(): assert print_schema( _single_field_schema(String, description="This field is awesome"), indent=" ", include_descriptions=True, ) == dedent(''' type Query { """This field is awesome""" singleField: String } ''')
def test_object_field(): schema = _single_field_schema(ObjectType("Foo", [Field("str", String)])) assert print_schema(schema, indent=" ") == dedent(""" type Foo { str: String } type Query { singleField: Foo } """)