Example #1
0
    async def cook(
        self,
        sdl: Union[str, List[str]],
        error_coercer: Callable[[Exception], dict] = None,
        custom_default_resolver: Optional[Callable] = None,
        modules: Optional[Union[str, List[str]]] = None,
        schema_name: str = "default",
    ):
        """
        Cook the tartiflette, basicly prepare the engine by binding it to given modules using the schema_name as a key.
        You wont be able to execute a request if the engine wasn't cooked.

        Arguments:
            sdl {Union[str, List[str]]} -- The SDL to work with.

        Keyword Arguments:
            schema_name {str} -- The name of the SDL (default: {"default"})
            error_coercer {Callable[[Exception, dict], dict]} -- An optional callable in charge of transforming a couple Exception/error into an error dict (default: {default_error_coercer})
            custom_default_resolver {Optional[Callable]} -- An optional callable that will replace the tartiflette default_resolver (Will be called like a resolver for each UNDECORATED field) (default: {None})
            modules {Optional[Union[str, List[str]]]} -- An optional list of string containing the name of the modules you want the engine to import, usually this modules contains your Resolvers, Directives, Scalar or Subscription code (default: {None})
        """

        if not modules:
            modules = []

        if isinstance(modules, str):
            modules = [modules]

        self._error_coercer = error_coercer_factory(error_coercer
                                                    or default_error_coercer)
        self._modules, modules_sdl = await _import_modules(
            modules, schema_name)
        SchemaRegistry.register_sdl(schema_name, sdl, modules_sdl)
        self._schema = SchemaBakery.bake(schema_name, custom_default_resolver)
Example #2
0
    async def cook(
        self,
        sdl: Union[str, List[str]] = None,
        error_coercer: Callable[[Exception], dict] = None,
        custom_default_resolver: Optional[Callable] = None,
        modules: Optional[Union[str, List[str]]] = None,
        schema_name: str = None,
    ):
        """
        Cook the tartiflette, i.e. prepare the engine by binding it to given modules using the schema_name as a key.
        You won't be able to execute a request if the engine hasn't been cooked.
        Has no effect if the engine has already been cooked.

        Keyword Arguments:
            sdl {Union[str, List[str]]} -- The SDL to work with.
            schema_name {str} -- The name of the SDL (default: {"default"})
            error_coercer {Callable[[Exception, dict], dict]} -- An optional callable in charge of transforming a couple Exception/error into an error dict (default: {default_error_coercer})
            custom_default_resolver {Optional[Callable]} -- An optional callable that will replace the tartiflette default_resolver (Will be called like a resolver for each UNDECORATED field) (default: {None})
            modules {Optional[Union[str, List[str]]]} -- An optional list of string containing the name of the modules you want the engine to import, usually this modules contains your Resolvers, Directives, Scalar or Subscription code (default: {None})
        """
        if self._cooked:
            return

        if modules is None:
            modules = self._modules or []

        if isinstance(modules, str):
            modules = [modules]

        sdl = sdl or self._sdl
        if not sdl:
            raise Exception("Please provide a SDL")

        schema_name = schema_name or self._schema_name or "default"

        custom_default_resolver = (custom_default_resolver
                                   or self._custom_default_resolver)
        if custom_default_resolver and not iscoroutinefunction(
                custom_default_resolver):
            raise Exception(f"Given custom_default_resolver "
                            f"{custom_default_resolver} "
                            f"is not a coroutine function")

        self._error_coercer = error_coercer_factory(error_coercer
                                                    or self._error_coercer
                                                    or default_error_coercer)

        self._modules, modules_sdl = await _import_modules(
            modules, schema_name)

        SchemaRegistry.register_sdl(schema_name, sdl, modules_sdl)
        self._schema = SchemaBakery.bake(schema_name, custom_default_resolver)

        self._cooked = True
Example #3
0
    def __init__(
        self,
        sdl: Union[str, List[str]],
        schema_name: str = "default",
        error_coercer: Callable[[Exception], dict] = default_error_coercer,
        custom_default_resolver: Optional[Callable] = None,
        exclude_builtins_scalars: Optional[List[str]] = None,
        modules: Optional[Union[str, List[str]]] = None,
    ) -> None:
        """Create an engine by analyzing the SDL and connecting it with the imported Resolver, Mutation,
        Subscription, Directive and Scalar linking them through the schema_name.

        Then using `await an_engine.execute(query)` will resolve your GQL requests.

        Arguments:
            sdl {Union[str, List[str]]} -- The SDL to work with.

        Keyword Arguments:
            schema_name {str} -- The name of the SDL (default: {"default"})
            error_coercer {Callable[[Exception], dict]} -- An optional callable in charge of transforming an Exception into an error dict (default: {default_error_coercer})
            custom_default_resolver {Optional[Callable]} -- An optional callable that will replace the tartiflette default_resolver (Will be called like a resolver for each UNDECORATED field) (default: {None})
            exclude_builtins_scalars {Optional[List[str]]} -- An optional list of string containing the names of the builtin scalar you don't want to be automatically included, usually it's Date, DateTime or Time scalars (default: {None})
            modules {Optional[Union[str, List[str]]]} -- An optional list of string containing the name of the modules you want the engine to import, usually this modules contains your Resolvers, Directives, Scalar or Subscription code (default: {None})
        """

        if isinstance(modules, str):
            modules = [modules]

        self._modules = _import_modules(modules)

        self._error_coercer = error_coercer
        self._parser = TartifletteRequestParser()
        SchemaRegistry.register_sdl(schema_name, sdl, exclude_builtins_scalars)
        self._schema = SchemaBakery.bake(
            schema_name, custom_default_resolver, exclude_builtins_scalars
        )
Example #4
0
    async def cook(
        self,
        sdl: Union[str, List[str]] = None,
        error_coercer: Callable[[Exception, Dict[str, Any]], Dict[str,
                                                                  Any]] = None,
        custom_default_resolver: Optional[Callable] = None,
        custom_default_type_resolver: Optional[Callable] = None,
        modules: Optional[Union[str, List[str], List[Dict[str, Any]]]] = None,
        schema_name: str = None,
    ) -> None:
        """
        Cook the tartiflette, basically prepare the engine by binding it to
        given modules using the schema_name as a key. You wont be able to
        execute a request if the engine wasn't cooked.
        :param sdl: path or list of path to the files / directories containing
        the SDL
        :param error_coercer: callable in charge of transforming a couple
        Exception/error into an error dictionary
        :param custom_default_resolver: callable that will replace the builtin
        default_resolver (called as resolver for each UNDECORATED field)
        :param custom_default_type_resolver: callable that will replace the
        tartiflette `default_type_resolver` (will be called on abstract types
        to deduct the type of a result)
        :param modules: list of string containing the name of the modules you
        want the engine to import, usually this modules contains your
        Resolvers, Directives, Scalar or Subscription code
        :param schema_name: name of the SDL
        :type sdl: Union[str, List[str]]
        :type error_coercer: Callable[[Exception, Dict[str, Any]], Dict[str, Any]]
        :type custom_default_resolver: Optional[Callable]
        :type custom_default_type_resolver: Optional[Callable]
        :type modules: Optional[Union[str, List[str], List[Dict[str, Any]]]]
        :type schema_name: str
        """
        if self._cooked:
            return

        if modules is None:
            modules = self._modules or []

        if isinstance(modules, str):
            modules = [modules]

        sdl = sdl or self._sdl
        if not sdl:
            raise Exception("Please provide a SDL")

        schema_name = schema_name or self._schema_name or "default"

        custom_error_coercer = error_coercer or self._error_coercer
        if custom_error_coercer and not is_valid_coroutine(
                custom_error_coercer):
            raise NonCoroutine(
                "Given < error_coercer > is not a coroutine callable.")

        custom_default_resolver = (custom_default_resolver
                                   or self._custom_default_resolver)
        if custom_default_resolver and not is_valid_coroutine(
                custom_default_resolver):
            raise NonCoroutine(
                "Given < custom_default_resolver > is not a coroutine callable."
            )

        custom_default_type_resolver = (custom_default_type_resolver
                                        or self._custom_default_type_resolver)
        if custom_default_type_resolver and not callable(
                custom_default_type_resolver):
            raise NonCallable(
                "Given < custom_default_type_resolver > is not a coroutine callable."
            )

        self._error_coercer = error_coercer_factory(custom_error_coercer
                                                    or default_error_coercer)

        self._modules, modules_sdl = await _import_modules(
            modules, schema_name)

        SchemaRegistry.register_sdl(schema_name, sdl, modules_sdl)
        self._schema = await SchemaBakery.bake(schema_name,
                                               custom_default_resolver,
                                               custom_default_type_resolver)
        self._build_response = partial(build_response,
                                       error_coercer=self._error_coercer)

        self._cooked = True
Example #5
0
    async def cook(
        self,
        sdl: Union[str, List[str]] = None,
        error_coercer: Callable[[Exception, Dict[str, Any]], Dict[str,
                                                                  Any]] = None,
        custom_default_resolver: Optional[Callable] = None,
        custom_default_type_resolver: Optional[Callable] = None,
        modules: Optional[Union[str, List[str], List[Dict[str, Any]]]] = None,
        query_cache_decorator: Optional[Callable] = UNDEFINED_VALUE,
        json_loader: Optional[Callable[[str], Dict[str, Any]]] = None,
        custom_default_arguments_coercer: Optional[Callable] = None,
        coerce_list_concurrently: Optional[bool] = None,
        schema_name: Optional[str] = None,
    ) -> None:
        """
        Cook the tartiflette, basically prepare the engine by binding it to
        given modules using the schema_name as a key. You wont be able to
        execute a request if the engine wasn't cooked.
        :param sdl: path or list of path to the files / directories containing
        the SDL
        :param error_coercer: callable in charge of transforming a couple
        Exception/error into an error dictionary
        :param custom_default_resolver: callable that will replace the builtin
        default_resolver (called as resolver for each UNDECORATED field)
        :param custom_default_type_resolver: callable that will replace the
        tartiflette `default_type_resolver` (will be called on abstract types
        to deduct the type of a result)
        :param modules: list of string containing the name of the modules you
        want the engine to import, usually this modules contains your
        Resolvers, Directives, Scalar or Subscription code
        :param query_cache_decorator: callable that will replace the
        tartiflette default lru_cache decorator to cache query parsing
        :param json_loader: A callable that will replace default python
        json module.loads for ast_json loading
        :param custom_default_arguments_coercer: callable that will replace the
        tartiflette `default_arguments_coercer`
        :param coerce_list_concurrently: whether or not list will be coerced
        concurrently
        :param schema_name: name of the SDL
        :type sdl: Union[str, List[str]]
        :type error_coercer: Callable[[Exception, Dict[str, Any]], Dict[str, Any]]
        :type custom_default_resolver: Optional[Callable]
        :type custom_default_type_resolver: Optional[Callable]
        :type modules: Optional[Union[str, List[str], List[Dict[str, Any]]]]
        :type query_cache_decorator: Optional[Callable]
        :type json_loader: Optional[Callable[[str], Dict[str, Any]]]
        :type custom_default_arguments_coercer: Optional[Callable]
        :type coerce_list_concurrently: Optional[bool]
        :type schema_name: Optional[str]
        """
        # pylint: disable=too-many-arguments,too-many-locals
        if self._cooked:
            return

        if modules is None:
            modules = self._modules or []

        if isinstance(modules, str):
            modules = [modules]

        sdl = sdl or self._sdl
        if not sdl:
            raise Exception("Please provide a SDL")

        schema_name = schema_name or self._schema_name or "default"

        custom_error_coercer = error_coercer or self._error_coercer
        if custom_error_coercer and not is_valid_coroutine(
                custom_error_coercer):
            raise NonCoroutine(
                "Given < error_coercer > is not a coroutine callable.")

        custom_default_resolver = (custom_default_resolver
                                   or self._custom_default_resolver)
        if custom_default_resolver and not is_valid_coroutine(
                custom_default_resolver):
            raise NonCoroutine(
                "Given < custom_default_resolver > is not a coroutine callable."
            )

        custom_default_type_resolver = (custom_default_type_resolver
                                        or self._custom_default_type_resolver)
        if custom_default_type_resolver and not callable(
                custom_default_type_resolver):
            raise NonCallable(
                "Given < custom_default_type_resolver > is not a coroutine callable."
            )

        custom_default_arguments_coercer = (
            custom_default_arguments_coercer
            or self._custom_default_arguments_coercer)
        if custom_default_arguments_coercer and not is_valid_coroutine(
                custom_default_arguments_coercer):
            raise NonCoroutine(
                "Given < custom_default_arguments_coercer > is not a "
                "coroutine callable.")

        self._error_coercer = error_coercer_factory(custom_error_coercer
                                                    or default_error_coercer)

        self._modules, modules_sdl = await _import_modules(
            modules, schema_name)

        SchemaRegistry.register_sdl(schema_name, sdl, modules_sdl)
        self._schema = await SchemaBakery.bake(
            schema_name,
            custom_default_resolver,
            custom_default_type_resolver,
            custom_default_arguments_coercer,
            (coerce_list_concurrently if coerce_list_concurrently is not None
             else self._coerce_list_concurrently),
        )
        self._build_response = partial(build_response,
                                       error_coercer=self._error_coercer)

        (
            self._query_executor,
            self._subscription_executor,
        ) = self._schema.bake_execute(self._perform_query,
                                      self._perform_subscription)

        if query_cache_decorator is UNDEFINED_VALUE:
            query_cache_decorator = self._query_cache_decorator

        self._cached_parse_and_validate_query = (
            query_cache_decorator(parse_and_validate_query)
            if callable(query_cache_decorator) else parse_and_validate_query)

        self._schema.json_loader = json_loader or self._json_loader
        self._cooked = True