Skip to content

Trying to inject id field on every selection_set #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nrbnlulu opened this issue Feb 21, 2023 · 1 comment
Closed

Trying to inject id field on every selection_set #193

nrbnlulu opened this issue Feb 21, 2023 · 1 comment

Comments

@nrbnlulu
Copy link
Contributor

nrbnlulu commented Feb 21, 2023

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?

@nrbnlulu
Copy link
Contributor Author

Solution was to add blank tuples in arguments and directives

 ID_SELECTION_NODE = gql_lang.FieldNode(
        name=gql_lang.NameNode(value="id"), arguments=(), directives=()
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant