
    ?h)                         d Z ddlmZmZmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZ ddlmZ d	d
lmZ  G d d      Zy)z
GraphQL query module.
    )ListAnyDictOptional)ConnectionError)
Connection   )AggregateBuilder)
GetBuilder
PROPERTIES)MultiGetBuilder   )_decode_json_response_dictc                   z    e Zd ZdZdefdZ	 ddedee   de	fdZ
d	ee	   defd
ZdedefdZdedeeef   fdZy)QueryzL
    Query class used to make `get` and/or `aggregate` GraphQL queries.
    
connectionc                     || _         y)z
        Initialize a Classification class instance.

        Parameters
        ----------
        connection : weaviate.connect.Connection
            Connection object to an active and running Weaviate instance.
        N)_connection)selfr   s     P/home/chris/cleankitchens-env/lib/python3.12/site-packages/weaviate/gql/query.py__init__zQuery.__init__   s     &    N
class_name
propertiesreturnc                 0    t        ||| j                        S )a  
        Instantiate a GetBuilder for GraphQL `get` requests.

        Parameters
        ----------
        class_name : str
            Class name of the objects to interact with.
        properties : list of str and ReferenceProperty, str or None
            Properties of the objects to get, by default None

        Returns
        -------
        GetBuilder
            A GetBuilder to make GraphQL `get` requests from weaviate.
        )r   r   )r   r   r   s      r   getz	Query.get    s    ( *j$2B2BCCr   get_builderc                 .    t        || j                        S )a  
        Instantiate a MultiGetBuilder for GraphQL `multi_get` requests.
        Bundles multiple get requests into one.

        Parameters
        ----------
        get_builder : list of GetBuilder
            List of GetBuilder objects for a single request each.

        Returns
        -------
        MultiGetBuilder
            A MultiGetBuilder to make GraphQL `get` multiple requests from weaviate.
        )r   r   )r   r   s     r   	multi_getzQuery.multi_get6   s    & {D,<,<==r   c                 .    t        || j                        S )aY  
        Instantiate an AggregateBuilder for GraphQL `aggregate` requests.

        Parameters
        ----------
        class_name : str
            Class name of the objects to be aggregated.

        Returns
        -------
        AggregateBuilder
            An AggregateBuilder to make GraphQL `aggregate` requests from weaviate.
        )r
   r   )r   r   s     r   	aggregatezQuery.aggregateK   s      
D,<,<==r   	gql_queryc                     t        |t              st        d      d|i}	 | j                  j	                  d|      }t        |d      }|J |S # t
        $ r}t        d      |d}~ww xY w)u  
        Allows to send simple graph QL string queries.
        Be cautious of injection risks when generating query strings.

        Parameters
        ----------
        gql_query : str
            GraphQL query as a string.

        Returns
        -------
        dict
            Data response of the query.

        Examples
        --------
        >>> query = """
        ... {
        ...     Get {
        ...         Article(limit: 2) {
        ...         title
        ...         hasAuthors {
        ...             ... on Author {
        ...                 name
        ...                 }
        ...             }
        ...         }
        ...     }
        ... }
        ... """
        >>> client.query.raw(query)
        {
        "data": {
            "Get": {
            "Article": [
                {
                "hasAuthors": [
                    {
                    "name": "Jonathan Wilson"
                    }
                ],
                "title": "Sergio Agüero has been far more than a great goalscorer for
                            Manchester City"
                },
                {
                "hasAuthors": [
                    {
                    "name": "Emma Elwick-Bates"
                    }
                ],
                "title": "At Swarovski, Giovanna Engelbert Is Crafting Jewels As Exuberantly
                            Joyful As She Is"
                }
            ]
            }
        },
        "errors": null
        }

        Raises
        ------
        TypeError
            If 'gql_query' is not of type str.
        requests.ConnectionError
            If the network connection to weaviate fails.
        weaviate.UnexpectedStatusCodeException
            If weaviate reports a none OK status.
        z Query is expected to be a stringqueryz/graphql)pathweaviate_objectzQuery not executed.NzGQL query failed)
isinstancestr	TypeErrorr   postRequestsConnectionErrorr   )r   r#   
json_queryresponseconn_errress         r   rawz	Query.raw\   s    L )S)>??y)
	O'',,*j,YH )3EF
 ' 	O)*?@hN	Os   A 	A*A%%A*)N)__name__
__module____qualname____doc__r   r   r)   r   r   r   r   r   r   r    r
   r"   r   r   r1    r   r   r   r      s    
&: 
& ,0DD Z(D 
	D,>*%> 
>*>C >,< >"RS RT#s(^ Rr   r   N)r5   typingr   r   r   r   requests.exceptionsr   r,   weaviate.connectr   r"   r
   r   r   r   r    r   utilr   r   r6   r   r   <module>r;      s0    - , J ' ' ' & -_ _r   