示例#1
0
    def bake(self, schema: "GraphQLSchema") -> None:
        """
        Bakes the GraphQLObjectType and computes all the necessary stuff for
        execution.
        :param schema: the GraphQLSchema instance linked to the engine
        :type schema: GraphQLSchema
        """
        if self.interfaces_names:
            for interface_name in self.interfaces_names:
                interface = schema.find_type(interface_name)
                self.interfaces.append(interface)
                interface.add_possible_type(self)

        # Directives
        directives_definition = compute_directive_nodes(
            schema, self.directives
        )
        self.introspection_directives = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_introspection",
        )

        self.pre_output_coercion_directives = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_pre_output_coercion",
            with_default=True,
        )

        # Coercers
        self.output_coercer = partial(
            output_directives_coercer,
            coercer=partial(object_coercer, object_type=self),
            directives=self.pre_output_coercion_directives,
        )
示例#2
0
    def bake(self, schema: "GraphQLSchema") -> None:
        """
        Bakes the GraphQLInterfaceType and computes all the necessary stuff for
        execution.
        :param schema: the GraphQLSchema instance linked to the engine
        :type schema: GraphQLSchema
        """
        # Directives
        directives_definition = compute_directive_nodes(
            schema, self.directives)
        self.introspection_directives = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_introspection",
        )

        # Coercers
        self.output_coercer = partial(
            output_directives_coercer,
            coercer=partial(abstract_coercer, abstract_type=self),
            directives=wraps_with_directives(
                directives_definition=directives_definition,
                directive_hook="on_pre_output_coercion",
                with_default=True,
            ),
        )
示例#3
0
    def bake(self, schema: "GraphQLSchema") -> None:
        """
        Bakes the GraphQLUnionType and computes all the necessary stuff for
        execution.
        :param schema: the GraphQLSchema instance linked to the engine
        :type schema: GraphQLSchema
        """
        for type_name in self.types:
            schema_type = schema.find_type(type_name)
            self._possible_types.append(schema_type)
            self._possible_types_set.add(type_name)

        # Directives
        directives_definition = compute_directive_nodes(
            schema, self.directives)
        self.introspection_directives = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_introspection",
        )

        # Coercers
        self.output_coercer = partial(
            output_directives_coercer,
            coercer=partial(abstract_coercer, abstract_type=self),
            directives=wraps_with_directives(
                directives_definition=directives_definition,
                directive_hook="on_pre_output_coercion",
                with_default=True,
            ),
        )
    def bake(self, schema: "GraphQLSchema") -> None:
        """
        Bakes the GraphQLInputObject and computes all the necessary stuff for execution.
        :param schema: the GraphQLSchema schema instance linked to the engine
        :type schema: GraphQLSchema
        """
        # Directives
        directives_definition = compute_directive_nodes(
            schema, self.directives)
        self.introspection_directives = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_introspection",
        )
        post_input_coercion_directives = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_post_input_coercion",
        )

        # Coercers
        self.input_coercer = partial(
            input_directives_coercer,
            coercer=partial(input_input_object_coercer,
                            input_object_type=self),
            directives=post_input_coercion_directives,
        )
        self.literal_coercer = partial(
            literal_directives_coercer,
            coercer=partial(literal_input_object_coercer,
                            input_object_type=self),
            directives=post_input_coercion_directives,
        )
示例#5
0
    def bake(self, schema: "GraphQLSchema") -> None:
        """
        Bakes the GraphQLArgument and computes all the necessary stuff for
        execution.
        :param schema: the GraphQLSchema instance linked to the engine
        :type schema: GraphQLSchema
        """
        self.graphql_type = get_graphql_type(schema, self.gql_type)

        if isinstance(self.gql_type, GraphQLType):
            self.type = self.gql_type
        else:
            self.type["name"] = self.gql_type
            self.type["kind"] = self.graphql_type.kind

        self.defaultValue = (str(self.default_value)
                             if self.default_value is not None else None)

        # Directives
        directives_definition = compute_directive_nodes(
            schema, self.directives)
        self.introspection_directives = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_introspection",
        )

        # Coercers
        self.literal_coercer = get_literal_coercer(self.graphql_type)
        self.coercer = partial(
            argument_coercer,
            directives=wraps_with_directives(
                directives_definition=directives_definition,
                directive_hook="on_argument_execution",
            ),
        )
示例#6
0
    def bake(
        self,
        schema: "GraphQLSchema",
        custom_default_resolver: Optional[Callable],
    ) -> None:
        """
        Bakes the GraphQLField and computes all the necessary stuff for
        execution.
        :param schema: the GraphQLSchema instance linked to the engine
        :param custom_default_resolver: callable that will replace the builtin
        default_resolver
        :type schema: GraphQLSchema
        :type custom_default_resolver: Optional[Callable]
        """
        self.graphql_type = get_graphql_type(schema, self.gql_type)

        # Directives
        directives_definition = compute_directive_nodes(
            schema, self.directives
        )
        self.on_post_bake = partial(
            wraps_with_directives(
                directives_definition=directives_definition,
                directive_hook="on_post_bake",
                with_default=True,
            ),
            self,
        )
        self.introspection_directives = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_introspection",
        )

        # Resolvers
        self.resolver = partial(
            resolve_field,
            field_definition=self,
            resolver=wraps_with_directives(
                directives_definition=directives_definition,
                directive_hook="on_field_execution",
                func=(
                    self.raw_resolver
                    or custom_default_resolver
                    or default_field_resolver
                ),
                is_resolver=True,
                with_default=True,
            ),
            output_coercer=get_output_coercer(self.graphql_type),
        )

        for argument in self.arguments.values():
            argument.bake(schema)
            self.args.append(argument)
示例#7
0
    def bake_execute(self, func_query, func_subscription):
        directives = compute_directive_nodes(self, self._schema_directives)
        func_query = wraps_with_directives(
            directives, "on_schema_execution", func_query, is_resolver=True
        )

        func_subscription = wraps_with_directives(
            directives,
            "on_schema_subscription",
            func_subscription,
            is_async_generator=True,
        )

        return func_query, func_subscription
示例#8
0
async def should_include_node(
    execution_context: "ExecutionContext",
    node: Union["FragmentSpreadNode", "FieldNode", "InlineFragmentNode"],
) -> bool:
    """
    Determines if a field should be included based on the @include and @skip
    directives, where @skip has higher precedence than @include.
    :param execution_context: instance of the query execution context
    :param node: the selection node to collect or skip
    :type execution_context: ExecutionContext
    :type node: Union[FragmentSpreadNode, FieldNode, InlineFragmentNode]
    :return: whether or not the node should be collected or skipped
    :rtype: bool
    """
    if not node.directives:
        return True

    hook_name = ("on_field_collection" if isinstance(
        node, FieldNode) else ("on_fragment_spread_collection" if isinstance(
            node, FragmentSpreadNode) else "on_inline_fragment_collection"))

    try:
        await wraps_with_directives(
            directives_definition=compute_directive_nodes(
                execution_context.schema,
                node.directives,
                execution_context.variable_values,
            ),
            directive_hook=hook_name,
            with_default=True,
        )(
            node,
            execution_context.context,
            context_coercer=execution_context.context,
        )
    except SkipCollection:
        return False
    except Exception:  # pylint: disable=broad-except
        # TODO: we should store unexpected exception in order to treat them as
        # field result on execution to handle them the same way as resolved
        # value and having the bubble up error and so on.
        return False
    return True
示例#9
0
    def bake(self, schema: "GraphQLSchema") -> None:
        """
        Bakes the GraphQLEnumValue and computes all the necessary stuff for
        execution.
        :param schema: the GraphQLSchema instance linked to the engine
        :type schema: GraphQLSchema
        """
        # Directives
        directives_definition = compute_directive_nodes(
            schema, self.directives
        )
        self.on_post_bake = partial(
            wraps_with_directives(
                directives_definition=directives_definition,
                directive_hook="on_post_bake",
                with_default=True,
            ),
            self,
        )
        self.introspection_directives = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_introspection",
        )
        post_input_coercion_directives = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_post_input_coercion",
            with_default=True,
        )

        # Coercers
        self.input_coercer = post_input_coercion_directives
        self.literal_coercer = post_input_coercion_directives
        self.output_coercer = wraps_with_directives(
            directives_definition=directives_definition,
            directive_hook="on_pre_output_coercion",
            with_default=True,
        )
示例#10
0
async def resolve_field_value_or_error(
    execution_context: "ExecutionContext",
    field_definition: "GraphQLField",
    field_nodes: List["FieldNode"],
    resolver: Callable,
    source: Any,
    info: "ResolveInfo",
) -> Union[Exception, Any]:
    """
    Coerce the field's arguments and then try to resolve the field.
    :param execution_context: instance of the query execution context
    :param field_definition: GraphQLField instance of the resolved field
    :param field_nodes: AST nodes related to the resolved field
    :param resolver: callable to use to resolve the field
    :param source: default root value or field parent value
    :param info: information related to the execution and the resolved field
    :type execution_context: ExecutionContext
    :type field_definition: GraphQLField
    :type field_nodes: List[FieldNode]
    :type resolver: Callable
    :type source: Any
    :type info: ResolveInfo
    :return: the resolved field value
    :rtype: Union[Exception, Any]
    """
    # pylint: disable=too-many-locals
    try:
        computed_directives = []
        for field_node in field_nodes:
            computed_directives.extend(
                compute_directive_nodes(
                    execution_context.schema,
                    field_node.directives,
                    execution_context.variable_values,
                )
            )

        resolver = wraps_with_directives(
            directives_definition=computed_directives,
            directive_hook="on_field_execution",
            func=resolver,
            is_resolver=True,
            with_default=True,
        )

        result = await resolver(
            source,
            await coerce_arguments(
                field_definition.arguments,
                field_nodes[0],
                execution_context.variable_values,
                execution_context.context,
            ),
            execution_context.context,
            info,
            context_coercer=execution_context.context,
        )
        if info.is_introspection:
            return await introspection_directives_executor(
                result,
                execution_context.context,
                info,
                context_coercer=execution_context.context,
            )
        return result
    except Exception as e:  # pylint: disable=broad-except
        return e