예제 #1
0
def bake(schema_name: str, config: Optional[Dict[str, Any]] = None) -> str:
    """
    Adds the introspection SDL.
    :param schema_name: schema name to link with
    :param config: configuration of the introspection
    :type schema_name: str
    :type config: Optional[Dict[str, Any]]
    :return: the SDL related to the introspection
    :rtype: str
    """
    # pylint: disable=unused-argument
    Resolver("__Type.enumValues",
             schema_name=schema_name)(resolve_type_enum_values)
    Resolver("__Type.fields", schema_name=schema_name)(resolve_type_fields)
    with open(os.path.join(os.path.dirname(__file__),
                           "introspection.sdl")) as file:
        return file.read()
async def ttftt_engine():
    Directive(
        name="a_directive", schema_name="test_directive_on_schema_execute"
    )(ADirective)
    Resolver("Query.a", schema_name="test_directive_on_schema_execute")(
        resolve_a
    )
    return await create_engine(
        sdl=_SDL, schema_name="test_directive_on_schema_execute"
    )
예제 #3
0
async def ttftt_engine():
    Directive(name="a_directive",
              schema_name="test_directive_schema_on_subscription")(ADirective)
    Resolver("Query.a",
             schema_name="test_directive_schema_on_subscription")(resolve_a)
    Subscription(
        "Subscription.timer",
        schema_name="test_directive_schema_on_subscription",
    )(subscription_timer)

    return await create_engine(
        sdl=_SDL, schema_name="test_directive_schema_on_subscription")
예제 #4
0
async def ttftt_engine():
    @Resolver("Query.hello", schema_name="test_issue213")
    async def resolve_query_hello(parent, args, ctx, info):
        return args.get("name")

    class QueryByResolver:
        async def __call__(self, parent, args, ctx, info):
            return args.get("name")

    Resolver("Query.bye", schema_name="test_issue213")(QueryByResolver())

    return await create_engine(sdl=_SDL, schema_name="test_issue213")
async def ttftt_engine_extended():
    Directive(
        name="a_directive",
        schema_name="test_directive_on_schema_execute_extended",
    )(ADirective)
    Directive(
        name="b_directive",
        schema_name="test_directive_on_schema_execute_extended",
    )(BDirective)
    Resolver(
        "Query.a", schema_name="test_directive_on_schema_execute_extended"
    )(resolve_a)
    return await create_engine(
        sdl=_SDL + _SDL_EXTEND,
        schema_name="test_directive_on_schema_execute_extended",
    )
예제 #6
0
def pytest_runtest_setup(item):
    marker = _get_ttftt_engine_marker(item)
    if not marker:
        return

    resolvers = marker.kwargs.get("resolvers") or {}
    subscriptions = marker.kwargs.get("subscriptions") or {}
    if not (resolvers or subscriptions):
        return

    schema_name = _get_schema_name_from_marker(marker)

    # Init schema definitions
    SchemaRegistry._schemas.setdefault(schema_name, {})

    # Reset schema resolvers
    if resolvers:
        SchemaRegistry._schemas[schema_name]["resolvers"] = {}

    if subscriptions:
        SchemaRegistry._schemas[schema_name]["subscriptions"] = {}

    # Apply "Resolver" decorators to resolvers functions
    for name, implementation in resolvers.items():
        Resolver(name, schema_name=schema_name)(implementation)

    for name, implementation in subscriptions.items():
        Subscription(name, schema_name=schema_name)(implementation)

    # Bake resolvers
    for resolver in (SchemaRegistry._schemas[schema_name].get("resolvers")
                     or {}).values():
        resolver.bake(_TTFTT_ENGINES[schema_name]._schema)

    # Bake subscriptions
    for subscription in (
            SchemaRegistry._schemas[schema_name].get("subscriptions")
            or {}).values():
        subscription.bake(_TTFTT_ENGINES[schema_name]._schema)

    # Re-bake engine schema
    _TTFTT_ENGINES[schema_name]._schema.bake()
예제 #7
0
async def bake(schema_name, config):
    Directive("Blah", schema_name=schema_name)(BlahDir(config))
    Scalar("NinjaGo", schema_name=schema_name)(NinjaGo)
    Resolver("Lol.ninja", schema_name=schema_name)(resolver_of_lol_ninja)
    return _SDL
예제 #8
0
    clean_registry.register_sdl("a", full_sdl)
    schema = SchemaBakery.bake("a")
    assert schema.has_type(type_name) is expected


@pytest.mark.parametrize(
    "schema_name,where,obj",
    [
        (
            "directives_schema",
            "directives",
            Directive("my_directive", "directives_schema"),
        ),
        ("scalars_schema", "scalars", Scalar("my_scalar", "scalars_schema")),
        (
            "resolvers_schema",
            "resolvers",
            Resolver("my_resolver", "resolvers_schema"),
        ),
    ],
)
def test_schema_registry_register(clean_registry, schema_name, where, obj):
    SchemaRegistry._register(schema_name, where, obj)

    with pytest.raises(ImproperlyConfigured) as excinfo:
        SchemaRegistry._register(schema_name, where, obj)

    assert str(excinfo.value) == (
        "Can't register < %s > to < %s > %s because it's already registered" %
        (obj.name, schema_name, where))
예제 #9
0
def pytest_runtest_setup(item):
    marker = _get_ttftt_engine_marker(item)
    if not marker or not getattr(item, "allow_schema_bake", True):
        return

    resolvers = marker.kwargs.get("resolvers") or {}
    type_resolvers = marker.kwargs.get("type_resolvers") or {}
    subscriptions = marker.kwargs.get("subscriptions") or {}
    directives = marker.kwargs.get("directives") or {}
    if not (resolvers or subscriptions or directives):
        return

    schema_name = _get_schema_name_from_marker(marker)

    # Init schema definitions
    SchemaRegistry._schemas.setdefault(schema_name, {})

    # Reset schema resolvers
    if resolvers:
        SchemaRegistry._schemas[schema_name]["resolvers"] = {}

    # Reset schema type_resolvers
    if type_resolvers:
        SchemaRegistry._schemas[schema_name]["type_resolvers"] = {}

    if subscriptions:
        SchemaRegistry._schemas[schema_name]["subscriptions"] = {}

    if directives:
        SchemaRegistry._schemas[schema_name]["directives"] = {}

    # Apply "Resolver" decorators to resolvers functions
    for name, implementation in resolvers.items():
        Resolver(name, schema_name=schema_name)(implementation)

    # Apply "TypeResolver" decorators to type resolvers functions
    for name, implementation in type_resolvers.items():
        TypeResolver(name, schema_name=schema_name)(implementation)

    # Apply "Subscription" decorators to resolvers functions
    for name, implementation in subscriptions.items():
        Subscription(name, schema_name=schema_name)(implementation)

    # Apply "Directive" decorators to resolvers functions
    for name, implementation in directives.items():
        Directive(name, schema_name=schema_name)(implementation)

    # Bake resolvers
    for resolver in (SchemaRegistry._schemas[schema_name].get("resolvers")
                     or {}).values():
        resolver.bake(_TTFTT_ENGINES[schema_name]._schema)

    # Bake type resolvers
    for type_resolver in (
            SchemaRegistry._schemas[schema_name].get("type_resolvers")
            or {}).values():
        type_resolver.bake(_TTFTT_ENGINES[schema_name]._schema)

    # Bake subscriptions
    for subscription in (
            SchemaRegistry._schemas[schema_name].get("subscriptions")
            or {}).values():
        subscription.bake(_TTFTT_ENGINES[schema_name]._schema)

    # Bake directives
    for directive in (SchemaRegistry._schemas[schema_name].get("directives")
                      or {}).values():
        directive.bake(_TTFTT_ENGINES[schema_name]._schema)

    # Re-bake engine schema
    SchemaRegistry.find_schema_info(
        schema_name=schema_name)["inst"] = _TTFTT_ENGINES[schema_name]._schema

    loop = asyncio.new_event_loop()
    loop.run_until_complete(_TTFTT_ENGINES[schema_name]._schema.bake())