Exemplo n.º 1
0
def update_schema_scalar(schema: GraphQLSchema, name: str,
                         scalar: GraphQLScalarType):
    """Update the scalar in a schema with the scalar provided.

    :param schema: the GraphQL schema
    :param name: the name of the custom scalar type in the schema
    :param scalar: a provided scalar type

    This can be used to update the default Custom Scalar implementation
    when the schema has been provided from a text file or from introspection.
    """

    if not isinstance(scalar, GraphQLScalarType):
        raise TypeError("Scalars should be instances of GraphQLScalarType.")

    schema_scalar = schema.get_type(name)

    if schema_scalar is None:
        raise KeyError(f"Scalar '{name}' not found in schema.")

    if not isinstance(schema_scalar, GraphQLScalarType):
        raise TypeError(f'The type "{name}" is not a GraphQLScalarType,'
                        f" it is a {type(schema_scalar)}")

    # Update the conversion methods
    # Using setattr because mypy has a false positive
    # https://github.com/python/mypy/issues/2427
    setattr(schema_scalar, "serialize", scalar.serialize)
    setattr(schema_scalar, "parse_value", scalar.parse_value)
    setattr(schema_scalar, "parse_literal", scalar.parse_literal)
Exemplo n.º 2
0
    def _make_executable(self, schema: GraphQLSchema):
        for type_name, fields in self.registry.items():
            object_type = schema.get_type(type_name)
            for field_name, resolver_fn in fields.items():
                field_definition = object_type.fields.get(field_name)
                if not field_definition:
                    raise Exception(f'Invalid field {type_name}.{field_name}')

                field_definition.resolve = resolver_fn
Exemplo n.º 3
0
    def _bind_schema(self, schema: GraphQLSchema,
                     solutions: List[Solution]) -> GraphQLSchema:

        solutions_map = {solution.type: solution for solution in solutions}
        custom_types = [type_ for type_ in schema.to_kwargs()['types']
                        if not type_.name.startswith('__')]

        for type_ in custom_types:
            if isinstance(type_, GraphQLObjectType):
                solution = solutions_map[type_.name]
                self.logger.debug(f' Binding solution: {solution} ')
                solution_type = schema.get_type(solution.type)
                assert solution_type
                fields = getattr(solution_type, 'fields', [])
                for name, field in fields.items():
                    field.resolve = solution.resolve(name)

        return schema
Exemplo n.º 4
0
class Schema:
    """Schema Definition.
    A Graphene Schema can execute operations (query, mutation, subscription) against the defined
    types. For advanced purposes, the schema can be used to lookup type definitions and answer
    questions about the types through introspection.
    Args:
        query (Type[ObjectType]): Root query *ObjectType*. Describes entry point for fields to *read*
            data in your Schema.
        mutation (Optional[Type[ObjectType]]): Root mutation *ObjectType*. Describes entry point for
            fields to *create, update or delete* data in your API.
        subscription (Optional[Type[ObjectType]]): Root subscription *ObjectType*. Describes entry point
            for fields to receive continuous updates.
        types (Optional[List[Type[ObjectType]]]): List of any types to include in schema that
            may not be introspected through root types.
        directives (List[GraphQLDirective], optional): List of custom directives to include in the
            GraphQL schema. Defaults to only include directives defined by GraphQL spec (@include
            and @skip) [GraphQLIncludeDirective, GraphQLSkipDirective].
        auto_camelcase (bool): Fieldnames will be transformed in Schema's TypeMap from snake_case
            to camelCase (preferred by GraphQL standard). Default True.
    """
    def __init__(
        self,
        query=None,
        mutation=None,
        subscription=None,
        types=None,
        directives=None,
        auto_camelcase=True,
    ):
        self.query = query
        self.mutation = mutation
        self.subscription = subscription
        type_map = TypeMap(query,
                           mutation,
                           subscription,
                           types,
                           auto_camelcase=auto_camelcase)
        self.graphql_schema = GraphQLSchema(
            type_map.query,
            type_map.mutation,
            type_map.subscription,
            type_map.types,
            directives,
        )

    def __str__(self):
        return print_schema(self.graphql_schema)

    def __getattr__(self, type_name):
        """
        This function let the developer select a type in a given schema
        by accessing its attrs.
        Example: using schema.Query for accessing the "Query" type in the Schema
        """
        _type = self.graphql_schema.get_type(type_name)
        if _type is None:
            raise AttributeError(f'Type "{type_name}" not found in the Schema')
        if isinstance(_type, GrapheneGraphQLType):
            return _type.graphene_type
        return _type

    def lazy(self, _type):
        return lambda: self.get_type(_type)

    def execute(self, *args, **kwargs):
        """Execute a GraphQL query on the schema.
        Use the `graphql_sync` function from `graphql-core` to provide the result
        for a query string. Most of the time this method will be called by one of the Graphene
        :ref:`Integrations` via a web request.
        Args:
            request_string (str or Document): GraphQL request (query, mutation or subscription)
                as string or parsed AST form from `graphql-core`.
            root_value (Any, optional): Value to use as the parent value object when resolving
                root types.
            context_value (Any, optional): Value to be made available to all resolvers via
                `info.context`. Can be used to share authorization, dataloaders or other
                information needed to resolve an operation.
            variable_values (dict, optional): If variables are used in the request string, they can
                be provided in dictionary form mapping the variable name to the variable value.
            operation_name (str, optional): If multiple operations are provided in the
                request_string, an operation name must be provided for the result to be provided.
            middleware (List[SupportsGraphQLMiddleware]): Supply request level middleware as
                defined in `graphql-core`.
            execution_context_class (ExecutionContext, optional): The execution context class
                to use when resolving queries and mutations.
        Returns:
            :obj:`ExecutionResult` containing any data and errors for the operation.
        """
        kwargs = normalize_execute_kwargs(kwargs)
        return graphql_sync(self.graphql_schema, *args, **kwargs)

    async def execute_async(self, *args, **kwargs):
        """Execute a GraphQL query on the schema asynchronously.
        Same as `execute`, but uses `graphql` instead of `graphql_sync`.
        """
        kwargs = normalize_execute_kwargs(kwargs)
        return await graphql(self.graphql_schema, *args, **kwargs)

    async def subscribe(self, query, *args, **kwargs):
        """Execute a GraphQL subscription on the schema asynchronously."""
        # Do parsing
        try:
            document = parse(query)
        except GraphQLError as error:
            return ExecutionResult(data=None, errors=[error])

        # Do validation
        validation_errors = validate(self.graphql_schema, document)
        if validation_errors:
            return ExecutionResult(data=None, errors=validation_errors)

        # Execute the query
        kwargs = normalize_execute_kwargs(kwargs)
        return await subscribe(self.graphql_schema, document, *args, **kwargs)

    def introspect(self):
        introspection = self.execute(introspection_query)
        if introspection.errors:
            raise introspection.errors[0]
        return introspection.data