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_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_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_it_renames_relevant_schema_elements(schema: Schema) -> None: new_schema = transform_schema(schema, CamelCaseSchemaTransform()) assert new_schema.to_string() == dedent( """ directive @foo(argOne: Int!, argTwo: InputObject) on FIELD input InputObject { fieldOne: Int! fieldTwo: String } type Query { snakeCaseField: Int fieldWithArguments(argOne: Int!, argTwo: InputObject): String } """ )
def test_arguments_and_input_fields_are_handled_correctly( schema: Schema, ) -> None: def resolver(_root, _ctx, _info, *, arg_one, arg_two=None): if arg_two is None: arg_two = {"field_one": 0, "field_two": ""} return arg_one + arg_two["field_one"] schema.register_resolver("Query", "field_with_arguments", resolver) new_schema = transform_schema(schema, CamelCaseSchemaTransform()) result = graphql_blocking( new_schema, "{ fieldWithArguments(argOne: 42, argTwo: { fieldOne: 42 }) }", root={}, ) assert result and result.response()["data"] == {"fieldWithArguments": "84"}
def test_hides_input_type_field(): schema = build_schema( """ input Foo { name: String id: ID! } input Bar { name: String id: ID! } type Query { field(foo: Foo, bar: Bar): String } """ ) class HideInputField(VisibilitySchemaTransform): def is_input_field_visible(self, typename, fieldname): return not (typename == "Foo" and fieldname == "name") assert ( dedent( """ input Bar { name: String id: ID! } input Foo { id: ID! } type Query { field(foo: Foo, bar: Bar): String } """ ) == _sdl(transform_schema(schema, HideInputField())) )
def test_hides_directive(schema): class HideDirective(VisibilitySchemaTransform): def is_directive_visible(self, name): return name != "fooDirective" assert ( dedent( """ directive @barDirective(arg: SomeEnum, other_arg: Int) on FIELD type Bar implements IFace { name: String } type Foo implements IFace { name: String } interface IFace { name: String } type Query { a(arg: SomeEnum, other_arg: Int): String foo: Foo iface: IFace union: Thing uid: UUID } enum SomeEnum { FOO BAR } union Thing = Foo | Bar scalar UUID """ ) == _sdl(transform_schema(schema, HideDirective())) )
def test_hides_custom_scalar(schema): assert ( dedent( """ directive @barDirective(arg: SomeEnum, 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(arg: SomeEnum, other_arg: Int): String foo: Foo iface: IFace union: Thing } enum SomeEnum { FOO BAR } union Thing = Foo | Bar """ ) == _sdl(transform_schema(schema, _hide_type_transform("UUID"))) )
from py_gql.schema.transforms import CamelCaseSchemaTransform, transform_schema from py_gql.tracers import ApolloTracer from .schema import SCHEMA RUNTIME = ThreadPoolRuntime(max_workers=20) with open(os.path.join(os.path.dirname(__file__), "graphiql.html")) as f: GRAPHIQL_HTML = f.read() app = flask.Flask(__name__) # Let the initial order be respected. app.config["JSON_SORT_KEYS"] = False CAMEL_CASED_SCHEMA = transform_schema(SCHEMA, CamelCaseSchemaTransform()) SCHEMA_SDL = CAMEL_CASED_SCHEMA.to_string() @app.route("/sdl") def sdl_route(): return flask.Response(SCHEMA_SDL, mimetype="text") @app.route("/graphql", methods=("POST", )) def graphql_route(): data = flask.request.json tracer = ApolloTracer()
def test_does_not_hide_specified_scalar(schema): assert _sdl(schema) == _sdl( transform_schema(schema, _hide_type_transform("String")) )
def test_custom_resolver_still_works(schema: Schema) -> None: schema.register_resolver("Query", "snake_case_field", lambda *_: 42) new_schema = transform_schema(schema, CamelCaseSchemaTransform()) result = graphql_blocking(new_schema, "{ snakeCaseField }", root={}) assert result and result.response()["data"] == {"snakeCaseField": 42}
def test_default_resolver_still_works(schema: Schema) -> None: new_schema = transform_schema(schema, CamelCaseSchemaTransform()) result = graphql_blocking( new_schema, "{ snakeCaseField }", root={"snake_case_field": 42} ) assert result and result.response()["data"] == {"snakeCaseField": 42}