예제 #1
0
    def _validate_arguments(self, schema_definition, query_node, errors, path,
                            schema):
        if not schema_definition:
            # Handled by another Validator. (5.3.1)
            return errors

        for arg in query_node.arguments:
            if not arg.name.value in schema_definition.arguments:
                continue  # Handled by another Validator (5.4.1)

            c_argument_schema_type = schema_definition.arguments[
                arg.name.value].graphql_type
            r_argument_schema_type = reduce_type(c_argument_schema_type)
            if isinstance(r_argument_schema_type, str):
                r_argument_schema_type = schema.find_type(
                    r_argument_schema_type)

            errors = self._validate(
                r_argument_schema_type,
                c_argument_schema_type,
                arg,
                path,
                errors,
                schema,
            )
        return errors
예제 #2
0
    def _validate_input_fields(
        self,
        arg,
        schema_type_name,
        schema_input_fields,
        object_node,
        errors,
        path,
        schema,
    ):  # pylint: disable=too-many-locals
        for schema_input_field in schema_input_fields:
            if (schema_input_field
                    not in [x.name.value for x in object_node.fields]
                    and isinstance(
                        schema_input_fields[schema_input_field].gql_type,
                        GraphQLNonNull,
                    ) and schema_input_fields[schema_input_field].default_value
                    is None):
                errors.append(
                    graphql_error_from_nodes(
                        message=
                        f"Missing non nullable Input Field < {schema_input_field} > for Input Object < {schema_type_name} >.",
                        nodes=object_node,
                        path=path,
                        extensions=self._extensions,
                    ))

        for query_field_node in object_node.fields:
            if query_field_node.name.value not in schema_input_fields:
                errors.append(
                    graphql_error_from_nodes(
                        message=
                        f"Unknown Input Field < {query_field_node.name.value} > in < {schema_type_name} > Input Object",
                        nodes=query_field_node,
                        path=path,
                        extensions=self._extensions,
                    ))
                continue

            c_argument_schema_type = schema_input_fields[
                query_field_node.name.value].graphql_type
            r_argument_schema_type = reduce_type(c_argument_schema_type)
            if isinstance(r_argument_schema_type, str):
                r_argument_schema_type = schema.find_type(
                    r_argument_schema_type)

            errors = self._validate(
                r_argument_schema_type,
                c_argument_schema_type,
                arg,
                path,
                errors,
                schema,
                value_node=query_field_node.value,
                input_field=query_field_node,
            )
        return errors
예제 #3
0
 def _validate_type_is_an_input_types(self, obj: "GraphQLType",
                                      message_prefix: str) -> List[str]:
     """
     Validates that the object is a defined input types.
     :param obj: object to check
     :param message_prefix: prefix to append to the error message
     :type obj: GraphQLType
     :type message_prefix: str
     :return: a list of errors
     :rtype: List[str]
     """
     rtype = reduce_type(obj.gql_type)
     if not rtype in self._input_types:
         return [
             f"{message_prefix} is of type "
             f"< {rtype} > which is not a Scalar, "
             "an Enum or an InputObject"
         ]
     return []
예제 #4
0
 def _validate_schema_named_types(self) -> List[str]:
     """
     Validates that all type with fields refers to known GraphQL types.
     :return: a list of errors
     :rtype: List[str]
     """
     errors = []
     for type_name, gql_type in self.type_definitions.items():
         try:
             for field in gql_type.implemented_fields.values():
                 reduced_type = reduce_type(field.gql_type)
                 if str(reduced_type) not in self.type_definitions:
                     errors.append(
                         f"Field < {type_name}.{field.name} > is Invalid: "
                         f"the given Type < {reduced_type} > does not exist!"
                     )
         except AttributeError:
             pass
     return errors
예제 #5
0
def get_schema_field_type_name(parent_type_name: str, field_name: str,
                               schema: "GraphQLSchema") -> Union[None, str]:
    """
    Find the reduced type (completly unwrapped type) name of a field using
    it's parent type name and the field name.

    :param parent_type_name: The Parent type name of the field to search for.
    :type parent_type_name: str
    :param field_name: the Field Name to search for.
    :type field_name: str
    :param schema: The schema instance to look through.
    :type schema: GraphQLSchema

    :return: A string or None if field is not found.
    :rtype: Union[None, str]
    """

    try:
        return reduce_type(
            find_field(parent_type_name, field_name, schema).gql_type)
    except (AttributeError, KeyError):
        return None
예제 #6
0
def find_field_reduced_type(
        parent_type_name: str, field_name: str,
        schema: "GraphQLSchema") -> Union[None, "GraphQLType"]:
    """
    Find the reduced type (completly unwrapped type) Object of a field using
    it's parent type name and the field name.

    :param parent_type_name: The Parent type name of the field to search for.
    :type parent_type_name: str
    :param field_name: the Field Name to search for.
    :type field_name: str
    :param schema: The schema instance to look through.
    :type schema: GraphQLSchema

    :return: A GraphQLType or None if field is not found.
    :rtype: Union[None, "GraphQLType"]
    """

    schema_field = find_field(parent_type_name, field_name, schema)

    if not schema_field:
        return None

    return schema.type_definitions[reduce_type(schema_field.gql_type)]