Пример #1
0
def test_wraps_with_directives():
    from tartiflette.types.helpers import wraps_with_directives

    cllbs_a = Mock()
    cllbs_a.on_argument_execution = Mock()

    cllbs_b = Mock()
    cllbs_b.on_argument_execution = Mock()

    directives = [
        {
            "callables": {
                "on_argument_execution": cllbs_a.on_argument_execution
            },
            "args": {"a": "b"},
        },
        {
            "callables": {
                "on_argument_execution": cllbs_b.on_argument_execution
            },
            "args": {"c": "d"},
        },
    ]

    r = wraps_with_directives(directives, "on_argument_execution", "A")
    assert r is not None
    assert r.func is cllbs_a.on_argument_execution
    a, b = r.args
    assert a == {"a": "b"}
    assert b.func is cllbs_b.on_argument_execution
    a, b = b.args
    assert a == {"c": "d"}
    assert b == "A"

    assert wraps_with_directives([], "on_argument_execution", "A") == "A"
Пример #2
0
    def bake(self, schema: "GraphQLSchema") -> None:
        super().bake(schema)
        directives_definition = get_directive_instances(
            self._directives, self._schema)
        self._directives_implementations = {
            CoercerWay.OUTPUT:
            wraps_with_directives(
                directives_definition=directives_definition,
                directive_hook="on_pre_output_coercion",
            ),
            CoercerWay.INPUT:
            wraps_with_directives(
                directives_definition=directives_definition,
                directive_hook="on_post_input_coercion",
            ),
        }

        self._introspection_directives = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_introspection",
        )

        for value in self.values:
            value.bake(schema)
            self._value_map[value.name] = value
Пример #3
0
    def bake(self, schema: "GraphQLSchema") -> None:
        self._schema = schema
        self._directives_implementations = get_directive_instances(
            self._directives, self._schema)

        self._introspection_directives = wraps_with_directives(
            directives_definition=self._directives_implementations,
            directive_hook="on_introspection",
        )

        if isinstance(self.gql_type, GraphQLType):
            self._type = self.gql_type
        else:
            self._type["name"] = self.gql_type
            self._type["kind"] = self._schema.find_type(self.gql_type).kind

        self.coercer = partial(
            wraps_with_directives(
                directives_definition=self.directives,
                directive_hook="on_argument_execution",
                func=partial(
                    argument_coercer,
                    input_coercer=get_coercer(self,
                                              schema=schema,
                                              way=CoercerWay.INPUT),
                ),
            ),
            self,
        )
Пример #4
0
    async def __call__(
        self,
        parent_result: Optional[Any],
        args: Dict[str, Any],
        ctx: Optional[Dict[str, Any]],
        info: "Info",
        execution_directives: Optional[List[Dict[str, Any]]],
    ) -> (Any, Any):
        try:
            resolver = wraps_with_directives(
                directives_definition=execution_directives,
                directive_hook="on_field_execution",
                func=self._directivated_func,
            )

            result = await resolver(
                parent_result,
                await coerce_arguments(
                    self._schema_field.arguments, args, ctx, info
                ),
                ctx,
                info,
            )

            if info.execution_ctx.is_introspection:
                result = await self._introspection(result, ctx, info)

            return (
                result,
                await self._coercer(result, self._schema_field, ctx, info),
            )
        except SkipExecution as e:
            raise e
        except Exception as e:  # pylint: disable=broad-except
            return e, None
Пример #5
0
    def bake(self, schema):
        super().bake(schema)

        self._introspection_directives = wraps_with_directives(
            directives_definition=get_directive_instances(
                self._directives, self._schema),
            directive_hook="on_introspection",
        )
Пример #6
0
    def bake(self, schema: "GraphQLSchema") -> None:
        super().bake(schema)
        directives_definition = get_directive_instances(
            self._directives, self._schema
        )
        self._directives_implementations = {
            CoercerWay.INPUT: wraps_with_directives(
                directives_definition=directives_definition,
                directive_hook="on_post_input_coercion",
            )
        }

        self._introspection_directives = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_introspection",
        )

        for arg in self._fields.values():
            arg.bake(self._schema)
Пример #7
0
    def bake(self, schema):
        super().bake(schema)
        directives_definition = get_directive_instances(
            self._directives, self._schema
        )
        self._directives_implementations = {
            CoercerWay.OUTPUT: wraps_with_directives(
                directives_definition=directives_definition,
                directive_hook="on_pre_output_coercion",
            ),
            CoercerWay.INPUT: wraps_with_directives(
                directives_definition=directives_definition,
                directive_hook="on_post_input_coercion",
            ),
        }

        self._introspection_directives = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_introspection",
        )
Пример #8
0
    def bake(self, schema: "GraphQLSchema") -> None:
        super().bake(schema)
        self._interfaces = []
        for interface_name in self.interfaces_names:
            interface = self._schema.find_type(interface_name)
            self._interfaces.append(interface)
            interface.possibleTypes.append(self)

        directives_definition = get_directive_instances(
            self._directives, self._schema)

        self._directives_implementations = {
            CoercerWay.OUTPUT:
            wraps_with_directives(
                directives_definition=directives_definition,
                directive_hook="on_pre_output_coercion",
            )
        }

        self._introspection_directives = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_introspection",
        )
Пример #9
0
    def bake(self, custom_default_resolver: Optional[Callable]) -> None:
        self.update_coercer()
        if (
            self._raw_func is default_resolver
            and custom_default_resolver is not None
        ):
            self.update_func(custom_default_resolver)

        if self._schema_field.subscribe and self._raw_func is default_resolver:
            self._raw_func = default_subscription_resolver(self._raw_func)

        self._directivated_func = wraps_with_directives(
            directives_definition=self._schema_field.directives,
            directive_hook="on_field_execution",
            func=self._raw_func,
        )
Пример #10
0
    def bake(
        self,
        schema: "GraphQLSchema",
        parent_type: Any,
        custom_default_resolver: Optional[Callable],
    ) -> None:
        self._schema = schema
        self._reduced_type_name = reduce_type(self.gql_type)
        self._reduced_type = self._schema.find_type(self._reduced_type_name)
        self._directives_implementations = get_directive_instances(
            self._directives, self._schema)
        self._introspection_directives = wraps_with_directives(
            directives_definition=self._directives_implementations,
            directive_hook="on_introspection",
        )
        self.parent_type = parent_type

        self._is_leaf = self._compute_is_leaf()

        for arg in self.arguments.values():
            arg.bake(self._schema)

        self.resolver.bake(custom_default_resolver)