def test_parse_creates_ast(): source = Source("""{ node(id: 4) { id, name } } """) result = parse(source) assert result == \ ast.Document( loc=Loc(start=0, end=41, source=source), definitions=[ast.OperationDefinition( loc=Loc(start=0, end=40, source=source), operation='query', name=None, variable_definitions=None, directives=[], selection_set=ast.SelectionSet( loc=Loc(start=0, end=40, source=source), selections=[ast.Field( loc=Loc(start=4, end=38, source=source), alias=None, name=ast.Name( loc=Loc(start=4, end=8, source=source), value='node'), arguments=[ast.Argument( name=ast.Name(loc=Loc(start=9, end=11, source=source), value='id'), value=ast.IntValue( loc=Loc(start=13, end=14, source=source), value='4'), loc=Loc(start=9, end=14, source=source))], directives=[], selection_set=ast.SelectionSet( loc=Loc(start=16, end=38, source=source), selections=[ast.Field( loc=Loc(start=22, end=24, source=source), alias=None, name=ast.Name( loc=Loc(start=22, end=24, source=source), value='id'), arguments=[], directives=[], selection_set=None), ast.Field( loc=Loc(start=30, end=34, source=source), alias=None, name=ast.Name( loc=Loc(start=30, end=34, source=source), value='name'), arguments=[], directives=[], selection_set=None)]))]))])
def test_get_selections_to_dict(): selections = [ ast.Field(name='foo'), ast.Field(name='nested', selection_set=ast.SelectionSet([ ast.Field(name='bar'), ast.Field(name='more_nested', selection_set=ast.SelectionSet(selections=[ ast.Field(name='baz'), ])) ])) ] assert {'foo': None, 'nested': {'bar': None, 'more_nested': {'baz': None}}} == selections_to_dict(selections)
def _insert_selection_set_parent_nodes_from_deep_field_name( self, selection, deep_field_name): split_field_name = deep_field_name.split(LOOKUP_SEP, 1) name = to_camel_case(split_field_name[0]) if len(split_field_name) == 1: return ast.Field( name=ast.Name(value=name), selection_set=ast.SelectionSet( selections=selection.selection_set.selections)) return ast.Field( name=ast.Name(value=name), selection_set=ast.SelectionSet(selections=[ self._insert_selection_set_parent_nodes_from_deep_field_name( selection, split_field_name[1]) ]))
def _get_field_selection_set( self, field: GraphQLField, include_node: bool = True) -> Optional[graphql_ast.SelectionSet]: return_type = self.get_return_type(field.type) if isinstance(return_type, (GraphQLScalarType, GraphQLEnumType)): return None elif isinstance(return_type, (GraphQLObjectType, GrapheneInterfaceType)): all_selections = [] sub_fields = return_type.fields.items() if self.is_node_type(return_type): if include_node is False: sub_fields = [('id', return_type.fields['id'])] else: # disable full rendering of nested nodes to avoid recursion include_node = False for name, sub_field in sub_fields: selection = graphql_ast.Field( name=graphql_ast.Name(value=name), selection_set=self._get_field_selection_set( sub_field, include_node=include_node)) all_selections.append(selection) return graphql_ast.SelectionSet(selections=all_selections) raise NotImplementedError
def test_parses_multi_byte_characters(): result = parse(u''' # This comment has a \u0A0A multi-byte character. { field(arg: "Has a \u0A0A multi-byte character.") } ''', no_location=True, no_source=True) assert result == ast.Document(definitions=[ ast.OperationDefinition( operation='query', name=None, variable_definitions=None, directives=[], selection_set=ast.SelectionSet(selections=[ ast.Field( alias=None, name=ast.Name(value=u'field'), arguments=[ ast.Argument( name=ast. Name(value=u'arg'), value=ast .StringValue( value=u'Has a \u0a0a multi-byte character.')) ], directives=[], selection_set=None) ])) ])
def test_parses_multi_byte_characters(): # type: () -> None result = parse( u""" # This comment has a \u0A0A multi-byte character. { field(arg: "Has a \u0A0A multi-byte character.") } """, no_location=True, no_source=True, ) assert result == ast.Document(definitions=[ ast.OperationDefinition( operation="query", name=None, variable_definitions=None, directives=[], selection_set=ast.SelectionSet(selections=[ ast.Field( alias=None, name=ast.Name(value=u"field"), arguments=[ ast.Argument( name=ast.Name(value=u"arg"), value=ast.StringValue( value=u"Has a \u0a0a multi-byte character."), ) ], directives=[], selection_set=None, ) ]), ) ])
def view_func(): variable_values = self.get_variable_values() field_selection_set = self._get_field_selection_set( field, include_node=True) if hasattr(field.type, 'graphene_type') and issubclass( field.type.graphene_type, Node): _type_name, _id = Node.from_global_id(variable_values['id']) node_type = schema.get_type(_type_name) inline_selection = graphql_ast.InlineFragment( type_condition=graphql_ast.NamedType(name=graphql_ast.Name( value=_type_name)), selection_set=self._get_field_selection_set( GraphQLField(node_type), include_node=True)) field_selection_set.selections.append(inline_selection) document_ast = graphql_ast.Document([ graphql_ast.OperationDefinition( operation=operation, variable_definitions=variable_definitions, selection_set=graphql_ast.SelectionSet(selections=[ graphql_ast.Field(name=graphql_ast.Name( value=field_name), arguments=arguments, selection_set=field_selection_set) ])) ]) execution_results = schema.execute(document_ast, variable_values=variable_values) # TODO custom encoder that positions data[field_name] at data result, status_code = encode_execution_results( [execution_results], is_batch=False, format_error=default_format_error, encode=json_encode) return Response(result, status=status_code, content_type='application/json')
def test_parses_null_value_in_list(): result = parse('{ fieldWithObjectInput(input: {b: ["A", null, "C"], c: "C"}) }') assert result == ast.Document( definitions=[ ast.OperationDefinition( operation='query', name=None, variable_definitions=None, directives=[], selection_set=ast.SelectionSet( selections=[ ast.Field( alias=None, name=ast.Name(value=u'fieldWithObjectInput'), directives=[], selection_set=None, arguments=[ ast.Argument( name=ast.Name(value=u'input'), value=ast.ObjectValue( fields=[ ast.ObjectField( name=ast.Name(value=u'b'), value=ast.ListValue( values=[ ast.StringValue(value=u'A'), ast.NullValue(), ast.StringValue(value=u'C'), ], ), ), ast.ObjectField( name=ast.Name(value=u'c'), value=ast.StringValue(value=u'C'), ), ] ), ), ], ), ], ), ), ], )
def __init__(self, name, field): self.field = field self.ast_field = ast.Field(name=ast.Name(value=name), arguments=[]) self.selection_set = None
def test_parse_creates_ast(): # type: () -> None source = Source( """{ node(id: 4) { id, name } } """ ) result = parse(source) assert result == ast.Document( loc=Loc(start=0, end=41, source=source), definitions=[ ast.OperationDefinition( loc=Loc(start=0, end=40, source=source), operation="query", name=None, variable_definitions=None, directives=[], selection_set=ast.SelectionSet( loc=Loc(start=0, end=40, source=source), selections=[ ast.Field( loc=Loc(start=4, end=38, source=source), alias=None, name=ast.Name( loc=Loc(start=4, end=8, source=source), value="node" ), arguments=[ ast.Argument( name=ast.Name( loc=Loc(start=9, end=11, source=source), value="id", ), value=ast.IntValue( loc=Loc(start=13, end=14, source=source), value="4", ), loc=Loc(start=9, end=14, source=source), ) ], directives=[], selection_set=ast.SelectionSet( loc=Loc(start=16, end=38, source=source), selections=[ ast.Field( loc=Loc(start=22, end=24, source=source), alias=None, name=ast.Name( loc=Loc(start=22, end=24, source=source), value="id", ), arguments=[], directives=[], selection_set=None, ), ast.Field( loc=Loc(start=30, end=34, source=source), alias=None, name=ast.Name( loc=Loc(start=30, end=34, source=source), value="name", ), arguments=[], directives=[], selection_set=None, ), ], ), ) ], ), ) ], )