o
    tBh8                     @   s  U d dl Z d dlmZmZmZmZmZmZmZm	Z	m
Z
 d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z# d dl$m%Z% d dl&m'Z' e (e)Z*ddddddd	Z+ee,ee,d
f f e-d< defddZ.dd Z/G dd de Z0		 		d!dedededeee,ef  dee de1de2dee, defddZ3	d"dededeee,ef  dee, deee,ef  f
dd Z4dS )#    N)	AnyDictIterableListMappingOptionalTupleUnioncast)IDLEREMOVEDocumentNode	FieldNodeFragmentDefinitionNodeFragmentSpreadNodeGraphQLErrorGraphQLInterfaceTypeGraphQLListGraphQLNonNullGraphQLObjectTypeGraphQLSchemaGraphQLTypeInlineFragmentNodeNameNodeNodeOperationDefinitionNodeSelectionSetNodeTypeInfoTypeInfoVisitorVisitoris_leaf_type	print_astvisit)VisitorActionEnum)inspect)definitions)selection_set
selections)documentoperation_definitionr&   fieldinline_fragmentfragment_definition.RESULT_DOCUMENT_KEYStype_c                 C   s   t | tr| jS | S )z2Removes the GraphQLNonNull wrappings around types.)
isinstancer   of_type)r/    r2   q/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/gql/utilities/parse_result.py_ignore_non_null/   s   
r4   c                 C   s:   | j D ]}t|tr|jj|kr|  S qtd| d)z%Returns a fragment from the document.z
Fragment "z" not found in document!)r%   r0   r   namevaluer   )r)   fragment_name
definitionr2   r2   r3   _get_fragment7   s   

r9   c                       s  e Zd Z			d.dedededeeef de	d	e
d
edee f fddZedd Zedededeeef fddZdedededef fddZedededeeef fddZedededeeef fddZedd Zdd Zdededed ee d!ee dedeeeef f fd"d#Zdededed ee d!ee deeef fd$d%Zdedededef fd&d'Z edededeeef fd(d)Z!de"dedeeef fd*d+Z#ede$dedeeef fd,d-Z%  Z&S )/ParseResultVisitorFr   Nschemar)   noderesult	type_infovisit_fragmentinside_list_leveloperation_namec	           	         sD   || _ || _|| _|| _|| _|| _|| _|| _g | _t	 
  dS )a  Recursive Implementation of a Visitor class to parse results
        correspondind to a schema and a document.

        Using a TypeInfo class to get the node types during traversal.

        If we reach a list in the results, then we parse each
        item of the list recursively, traversing the same nodes
        of the query again.

        During traversal, we keep the current position in the result
        in the result_stack field.

        Alongside the field type, we calculate the "result type"
        which is computed from the field type and the current
        recursive level we are for this field
        (:code:`inside_list_level` argument).
        N)r;   r)   r<   r=   r>   r?   r@   rA   result_stacksuper__init__)	selfr;   r)   r<   r=   r>   r?   r@   rA   	__class__r2   r3   rD   B   s   zParseResultVisitor.__init__c                 C   s&   z| j d W S  ty   | j Y S w )N)rB   
IndexErrorr=   )rE   r2   r2   r3   current_resultk   s
   
z!ParseResultVisitor.current_result_argsreturnc                 G   &   t ttttf  | j}dd |D S )Nc                 S   $   i | ]}|  D ]\}}||qqS r2   items).0r=   kvr2   r2   r3   
<dictcomp>u      $ z5ParseResultVisitor.leave_document.<locals>.<dictcomp>)r
   r   r   strr   r%   )r<   rK   resultsr2   r2   r3   leave_documentr   s   z!ParseResultVisitor.leave_documentc                 G   sR   | j d ur't|jdstS tt|j|_|jj| j kr'td|jj  tS t	S )Nr6   zSKIPPING operation )
rA   hasattrr5   r   r
   r   r6   logdebugr   rE   r<   rK   r2   r2   r3   enter_operation_definitionw   s   
z-ParseResultVisitor.enter_operation_definitionc                 G   rM   )Nc                 S   rN   r2   rO   rQ   srR   rS   r2   r2   r3   rT      rU   zAParseResultVisitor.leave_operation_definition.<locals>.<dictcomp>r
   r   r   rV   r   r&   r<   rK   r(   r2   r2   r3   leave_operation_definition   s   z-ParseResultVisitor.leave_operation_definitionc                 G   s   t tttf | j}|S N)r
   r   rV   r   r(   )r<   rK   partial_resultsr2   r2   r3   leave_selection_set   s   z&ParseResultVisitor.leave_selection_setc                 C   s   |  ddkS )Nr(      )count)pathr2   r2   r3   in_first_field   s   z!ParseResultVisitor.in_first_fieldc                 C   sV   | j  }| j}t|}| |r)|dkr)t|tsJ t|j}|d8 }|dks|S )Nr   rf   )r>   get_typer@   r4   ri   r0   r   r1   )rE   rh   
field_type
list_levelresult_typer2   r2   r3   get_current_result_type   s   


z*ParseResultVisitor.get_current_result_typekeyparentrh   	ancestorsc              	   C   s  |j r|j jn|jj}ttjr*td|  td| td| j | jd u r1t	S t
| jtrMz| j| }W n tyS   td| d t	 Y S w td|  | j }| |}	t
|tsEt
|trEt
|tsEt|	sEt|	j}
ttjrtd tdt|  td	t|	  td
t|
 d g }| j }t
|ttfsJ |d }t
|tsJ t|gd}|D ]f}||i}ttjrtd|  tdt|  td| td|d | |r
| jd }nd}t| j| j||||d}td| d t t!t"tt#f  |}|$|d |  q||i}td| d |S | j%$| t&S t'd| d| j)NzEnter field z  path=z  current_result=z  Key z not found in result --> REMOVEz  result_value=z  List detected:z    field_type=z    result_type=z    inner_type=
rH   r'   z      recursive new_result=z      recursive ast=z      recursive path=z      recursive initial_type=rf   )initial_typer@   z      recursive result=r   z$    recursive visits final result = z&Invalid result for container of field : )(aliasr6   r5   rZ   isEnabledForloggingDEBUGr[   rJ   r   r0   r   KeyErrorr>   rj   rn   r   rV   r    r4   r1   r$   get_parent_typer   r   r   r!   ri   r@   parse_result_recursiver;   r)   r
   r   r   r   appendrB   r   r   )rE   r<   ro   rp   rh   rq   r5   result_valuerk   rm   
inner_typevisitsrs   selection_set_nodenew_nodeitem
new_resultr@   inner_visitr2   r2   r3   enter_field   s   	





zParseResultVisitor.enter_fieldc                 C   s  t t|jr	|jjn|jj}td|  | jd u rd }nO|jd u rZ| j	
 }| |}	ttjrMtd| dt|  td| dt|	  t|	sSJ |	| j}nt ttttf  |j}
dd |
D }| j  td| d|  ||iS )NzLeave field z  field type of z is z  result type of c                 S   rN   r2   rO   )rQ   prrR   rS   r2   r2   r3   rT   >  rU   z2ParseResultVisitor.leave_field.<locals>.<dictcomp>z: returning )r
   rV   ru   r6   r5   rZ   r[   rJ   r&   r>   rj   rn   rv   rw   rx   r$   r    parse_valuer   r   r   rB   pop)rE   r<   ro   rp   rh   rq   r5   return_valuerk   rm   rd   r2   r2   r3   leave_field  s"   	




zParseResultVisitor.leave_fieldc                 G   sB   t tjrt d|jj d t d| j | jrtS t	S )NzEnter fragment definition .zvisit_fragment=)
rZ   rv   rw   rx   r[   r5   r6   r?   r   r   r\   r2   r2   r3   enter_fragment_definitionI  s   z,ParseResultVisitor.enter_fragment_definitionc                 G   rM   )Nc                 S   rN   r2   rO   r^   r2   r2   r3   rT   \  rU   z@ParseResultVisitor.leave_fragment_definition.<locals>.<dictcomp>r`   ra   r2   r2   r3   leave_fragment_definitionV  s   z,ParseResultVisitor.leave_fragment_definitionc                 G   sd   |j j}td|  t| j|}t| j| j|| jdd}td| d|  t	t
ttf |S )NzStart recursive fragment visit T)r?   z#Result of recursive fragment visit rt   )r5   r6   rZ   r[   r9   r)   r{   r;   rJ   r
   r   rV   r   )rE   r<   rK   r7   fragment_nodefragment_resultr2   r2   r3   leave_fragment_spread^  s   z(ParseResultVisitor.leave_fragment_spreadc                 G   rM   )Nc                 S   rN   r2   rO   r^   r2   r2   r3   rT   z  rU   z<ParseResultVisitor.leave_inline_fragment.<locals>.<dictcomp>r`   ra   r2   r2   r3   leave_inline_fragmentv  s   z(ParseResultVisitor.leave_inline_fragment)Fr   N)'__name__
__module____qualname__r   r   r   r   rV   r   r   boolintr   rD   propertyrJ   staticmethodrX   r   r	   r#   r]   rb   r   re   ri   rn   r   r   r   r   r   r   r   r   r   r   r   __classcell__r2   r2   rF   r3   r:   A   s    
	)
 


 

t

-




(r:   Fr;   r)   r<   r=   rs   r@   r?   rA   rL   c           
      C   sD   |d u rd S t | |d}t|t|t| |||||||dtd}	|	S )N)rs   )r>   r@   r?   rA   )visitor_keys)r   r"   r   r:   r.   )
r;   r)   r<   r=   rs   r@   r?   rA   r>   visitedr2   r2   r3   r{   }  s*   r{   c                 C   s   t | ||||dS )a3  Unserialize a result received from a GraphQL backend.

    :param schema: the GraphQL schema
    :param document: the document representing the query sent to the backend
    :param result: the serialized result received from the backend
    :param operation_name: the optional operation name

    :returns: a parsed result with scalars and enums parsed depending on
              their definition in the schema.

    Given a schema, a query and a serialized result,
    provide a new result with parsed values.

    If the result contains only built-in GraphQL scalars (String, Int, Float, ...)
    then the parsed result should be unchanged.

    If the result contains custom scalars or enums, then those values
    will be parsed with the parse_value method of the custom scalar or enum
    definition in the schema.)rA   )r{   )r;   r)   r=   rA   r2   r2   r3   parse_result  s   
r   )Nr   FNrc   )5rw   typingr   r   r   r   r   r   r   r	   r
   graphqlr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   graphql.language.visitorr#   graphql.pyutilsr$   	getLoggerr   rZ   r.   rV   __annotations__r4   r9   r:   r   r   r{   r   r2   r2   r2   r3   <module>   sj   
 ,h


  C	
)