示例#1
0
    def _validate_type(
        self, varname: str, a_value: Any, a_type: Any, is_nullable: bool
    ) -> None:
        if is_nullable and a_value is None:
            return

        if not is_nullable and a_value is None:
            self._add_exception(
                InvalidType(
                    "Value can't be null or contain a null value",
                    path=self._internal_ctx.field_path[:],
                    locations=[self._internal_ctx.node.location],
                )
            )

        try:
            # pylint: disable=unidiomatic-typecheck
            if not (
                a_type == float and type(a_value) == int
            ) and not isinstance(a_value, a_type):
                self._add_exception(
                    InvalidType(
                        "Given value for < %s > is not type < %s >"
                        % (varname, a_type),
                        path=self._internal_ctx.field_path[:],
                        locations=[self._internal_ctx.node.location],
                    )
                )
        except TypeError:
            # TODO remove this, and handle the case it's an InputValue
            # (look at registered input values and compare fields)
            pass
示例#2
0
    def _validates_vars(self) -> None:
        # validate given var are okay
        name = self._internal_ctx.node.var_name
        if name not in self._vars:
            default_values = self._internal_ctx.node.default_value
            if not default_values and not self._internal_ctx.node.is_nullable:
                self._add_exception(UnknownVariableException(name))
                return None

            self._vars[name] = default_values
            return None

        a_type = self._internal_ctx.node.var_type
        a_value = self._vars[name]

        if self._internal_ctx.node.is_list:
            if not isinstance(a_value, list):
                self._add_exception(
                    InvalidType(
                        "Expecting List for < %s > values" % name,
                        path=self._internal_ctx.field_path[:],
                        locations=[self._internal_ctx.node.location],
                    ))
                return None

            for val in a_value:
                self._validate_type(name, val, a_type)
            return None

        self._validate_type(name, a_value, a_type)
        return None
示例#3
0
 def _validate_type(self, varname: str, a_value: Any, a_type: Any) -> None:
     try:
         if not isinstance(a_value, a_type):
             self._add_exception(
                 InvalidType(
                     "Given value for < %s > is not type < %s >" %
                     (varname, a_type),
                     path=self._internal_ctx.field_path[:],
                     locations=[self._internal_ctx.node.location],
                 ))
     except TypeError:
         # TODO remove this, and handle the case it's an InputValue
         # (look at registered input values and compare fields)
         pass
示例#4
0
    def bake(self, schema: "GraphQLSchema") -> None:
        """
        Sets the type resolver into the schema abstract type definition.
        :param schema: the GraphQLSchema instance linked to the type resolver
        :type schema: GraphQLSchema
        """
        if not self._implementation:
            raise MissingImplementation(
                f"No implementation given for type resolver < {self.name} >")

        try:
            graphql_type = schema.find_type(self.name)
            if not graphql_type.is_abstract_type:
                raise InvalidType(
                    f"Type < {self.name} > is not an abstract type.")

            graphql_type.type_resolver = self._implementation
        except KeyError:
            raise UnknownTypeDefinition(
                f"Unknown Type Definition < {self.name} >.")