Skip to content

Commit

Permalink
chore: move base filter creation
Browse files Browse the repository at this point in the history
  • Loading branch information
erikwrede committed Oct 6, 2023
1 parent 1aef748 commit 18a7c54
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
23 changes: 22 additions & 1 deletion graphene_sqlalchemy/registry.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
from collections import defaultdict
from typing import TYPE_CHECKING, List, Type

Expand All @@ -7,7 +8,7 @@
from sqlalchemy.types import Enum as SQLAlchemyEnumType

if TYPE_CHECKING: # pragma: no_cover
from graphene_sqlalchemy.filters import (
from .filters import (
FieldFilter,
BaseTypeFilter,
RelationshipFilter, )
Expand All @@ -26,6 +27,26 @@ def __init__(self):
self._registry_base_type_filters = {}
self._registry_relationship_filters = {}

self._init_base_filters()

def _init_base_filters(self):
import graphene_sqlalchemy.filters as gsqa_filters

from .filters import (FieldFilter)
field_filter_classes = [
filter_cls[1]
for filter_cls in inspect.getmembers(gsqa_filters, inspect.isclass)
if (
filter_cls[1] is not FieldFilter
and FieldFilter in filter_cls[1].__mro__
and getattr(filter_cls[1]._meta, "graphene_type", False)
)
]
for field_filter_class in field_filter_classes:
self.register_filter_for_scalar_type(
field_filter_class._meta.graphene_type, field_filter_class
)

def register(self, obj_type):
from .types import SQLAlchemyBase

Expand Down
15 changes: 0 additions & 15 deletions graphene_sqlalchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from sqlalchemy.orm import ColumnProperty, CompositeProperty, RelationshipProperty
from sqlalchemy.orm.exc import NoResultFound

import graphene_sqlalchemy.filters as gsa_filters
from .converter import (
convert_sqlalchemy_column,
convert_sqlalchemy_composite,
Expand Down Expand Up @@ -420,21 +419,7 @@ def __init_subclass_with_meta__(
)

if not registry:
# TODO add documentation for users to register their own filters
registry = get_global_registry()
field_filter_classes = [
filter_cls[1]
for filter_cls in inspect.getmembers(gsa_filters, inspect.isclass)
if (
filter_cls[1] is not FieldFilter
and FieldFilter in filter_cls[1].__mro__
and getattr(filter_cls[1]._meta, "graphene_type", False)
)
]
for field_filter_class in field_filter_classes:
get_global_registry().register_filter_for_scalar_type(
field_filter_class._meta.graphene_type, field_filter_class
)

assert isinstance(registry, Registry), (
"The attribute registry in {} needs to be an instance of "
Expand Down

0 comments on commit 18a7c54

Please sign in to comment.