Skip to content

Trying to inject id field on every selection_set  #193

Closed
@nrbnlulu

Description

@nrbnlulu

Hi, I'm trying to add id field on every selection set (like relay does I think)
Tried the below code based on #149.

class IDInjectionVisitor(visitor.Visitor):
    def has_id_field(self, selection_set: Tuple[gql_lang.SelectionNode]) -> bool:
        for field in selection_set:
            assert isinstance(field, gql_lang.FieldNode)
            if field.name.value == 'id':
                return True
        return False

    ID_FIELD_NODE = gql_lang.FieldNode(name=gql_lang.NameNode(value='id'))
    
    def enter(self, node, key, parent, path, ancestors):
        if is_operation_def_node(parent):
            return None
        if selection_set := is_selection_set(node):
            if not self.has_id_field(selection_set.selections):
                selection_set.selections = (*selection_set.selections, self.ID_FIELD_NODE)

This causes this error:

../../qtgql/codegen/introspection.py:229: in parse_queries
    if errors := graphql.validate(self.schema_definition, operations):
../../.venv/lib/python3.10/site-packages/graphql/validation/validate.py:86: in validate
    visit(document_ast, TypeInfoVisitor(type_info, ParallelVisitor(visitors)))
../../.venv/lib/python3.10/site-packages/graphql/language/visitor.py:261: in visit
    result = visit_fn(node, key, parent, path, ancestors)
../../.venv/lib/python3.10/site-packages/graphql/utilities/type_info.py:310: in enter
    result = fn(node, *args)
../../.venv/lib/python3.10/site-packages/graphql/language/visitor.py:343: in enter
    result = fn(node, *args)
../../.venv/lib/python3.10/site-packages/graphql/validation/rules/unique_argument_names.py:22: in enter_field
    self.check_arg_uniqueness(node.arguments)
../../.venv/lib/python3.10/site-packages/graphql/validation/rules/unique_argument_names.py:28: in check_arg_uniqueness
    seen_args = group_by(argument_nodes, attrgetter("name.value"))
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

items = None, key_fn = operator.attrgetter('name.value')

    def group_by(items: Collection[T], key_fn: Callable[[T], K]) -> Dict[K, List[T]]:
        """Group an unsorted collection of items by a key derived via a function."""
        result: Dict[K, List[T]] = defaultdict(list)
>       for item in items:
E       TypeError: 'NoneType' object is not iterable

Note that the I am injecting has no location would that be a problme?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions