예제 #1
0
def test_query_operation_without_name():
    assert print_ast(parse("query { id, name }")) == dedent("""
    {
      id
      name
    }
    """)
예제 #2
0
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")))
    )
예제 #3
0
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
        """)
예제 #4
0
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")))
    )
예제 #5
0
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
            }
            """))
예제 #6
0
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
            }
            """))
예제 #8
0
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]
예제 #9
0
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
            }
            """))
예제 #11
0
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
예제 #13
0
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
        }
        """)
예제 #14
0
def test_mutation():
    assert (dedent("""
            type Mutation {
                foo: Int
            }
            """) == print_schema(
        Schema(mutation_type=ObjectType("Mutation", [Field("foo", Int)]))))
예제 #15
0
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
            }
            """
        )
    )
예제 #16
0
def test_mutation_operation_without_name():
    assert print_ast(parse("mutation { id, name }")) == dedent("""
    mutation {
      id
      name
    }
    """)
예제 #17
0
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
        }
        """)
예제 #18
0
def test_single_field_schema(type_, opts, expected):
    assert (dedent("""
            type Query {
                singleField: %s
            }
            """ % expected) == print_schema(_single_field_schema(
        type_, **opts),
                                            indent="    "))
예제 #19
0
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
        }
        """)
예제 #20
0
def test_subscription():
    assert (dedent("""
            type Subscription {
                foo: Int
            }
            """) == print_schema(
        Schema(
            subscription_type=ObjectType("Subscription", [Field("foo", Int)])))
            )
예제 #21
0
def test_block_string_single_line_with_leading_space():
    assert (print_ast(
        parse('''
        { field(arg: """    space-led value""") }
    ''')) == dedent('''
    {
      field(arg: """    space-led value""")
    }
    '''))
예제 #22
0
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
        }
        """)
예제 #23
0
def test_GraphQLResult_json():
    assert GraphQLResult(data={
        "foo": 42
    }).json(indent=4) == dedent("""
        {
            "data": {
                "foo": 42
            }
        }""")
예제 #24
0
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
}
''')
예제 #25
0
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
    }
    """))
예제 #26
0
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
        }
        """)
예제 #27
0
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
        }
        ''')
예제 #28
0
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
        }
        """)
예제 #29
0
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
        }
        ''')
예제 #30
0
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
        }
        """)