-
Notifications
You must be signed in to change notification settings - Fork 227
Add batching
params
#260
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
Merged
Merged
Add batching
params
#260
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -3,21 +3,28 @@ | |||||||
from singledispatch import singledispatch | ||||||||
from sqlalchemy import types | ||||||||
from sqlalchemy.dialects import postgresql | ||||||||
from sqlalchemy.orm import interfaces | ||||||||
from sqlalchemy.orm import interfaces, strategies | ||||||||
|
||||||||
from graphene import (ID, Boolean, Dynamic, Enum, Field, Float, Int, List, | ||||||||
String) | ||||||||
from graphene.types.json import JSONString | ||||||||
|
||||||||
from .batching import get_batch_resolver | ||||||||
from .enums import enum_for_sa_enum | ||||||||
from .fields import (BatchSQLAlchemyConnectionField, | ||||||||
default_connection_field_factory) | ||||||||
from .registry import get_global_registry | ||||||||
from .resolvers import get_attr_resolver, get_custom_resolver | ||||||||
|
||||||||
try: | ||||||||
from sqlalchemy_utils import ChoiceType, JSONType, ScalarListType, TSVectorType | ||||||||
except ImportError: | ||||||||
ChoiceType = JSONType = ScalarListType = TSVectorType = object | ||||||||
|
||||||||
|
||||||||
is_selectin_available = getattr(strategies, 'SelectInLoader', None) | ||||||||
|
||||||||
|
||||||||
def get_column_doc(column): | ||||||||
return getattr(column, "doc", None) | ||||||||
|
||||||||
|
@@ -26,29 +33,46 @@ def is_column_nullable(column): | |||||||
return bool(getattr(column, "nullable", True)) | ||||||||
|
||||||||
|
||||||||
def convert_sqlalchemy_relationship(relationship_prop, registry, connection_field_factory, resolver, **field_kwargs): | ||||||||
direction = relationship_prop.direction | ||||||||
model = relationship_prop.mapper.entity | ||||||||
|
||||||||
def convert_sqlalchemy_relationship(relationship_prop, obj_type, connection_field_factory, batching, | ||||||||
attr_name, orm_field_name, **field_kwargs): | ||||||||
""" | ||||||||
:param sqlalchemy.RelationshipProperty relationship_prop: | ||||||||
:param Registry registry: | ||||||||
:type function|None connection_field_factory: | ||||||||
:type bool batching: | ||||||||
:param SQLAlchemyObjectType obj_type: | ||||||||
:param str orm_field_name: | ||||||||
:rtype: Dynamic | ||||||||
""" | ||||||||
def dynamic_type(): | ||||||||
_type = registry.get_type_for_model(model) | ||||||||
direction = relationship_prop.direction | ||||||||
model = relationship_prop.mapper.entity | ||||||||
type_ = obj_type._meta.registry.get_type_for_model(model) | ||||||||
|
||||||||
if not _type: | ||||||||
batching_ = batching if is_selectin_available else False | ||||||||
connection_field_factory_ = connection_field_factory | ||||||||
|
||||||||
if not type_: | ||||||||
return None | ||||||||
|
||||||||
if direction == interfaces.MANYTOONE or not relationship_prop.uselist: | ||||||||
return Field( | ||||||||
_type, | ||||||||
resolver=resolver, | ||||||||
**field_kwargs | ||||||||
) | ||||||||
elif direction in (interfaces.ONETOMANY, interfaces.MANYTOMANY): | ||||||||
if _type._meta.connection: | ||||||||
# TODO Add a way to override connection_field_factory | ||||||||
return connection_field_factory(relationship_prop, registry, **field_kwargs) | ||||||||
return Field( | ||||||||
List(_type), | ||||||||
**field_kwargs | ||||||||
) | ||||||||
resolver = get_custom_resolver(obj_type, orm_field_name) | ||||||||
if resolver is None: | ||||||||
jnak marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
resolver = get_batch_resolver(relationship_prop) if batching_ else \ | ||||||||
jnak marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
get_attr_resolver(obj_type, relationship_prop.key) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
return Field(type_, resolver=resolver, **field_kwargs) | ||||||||
|
||||||||
if direction in (interfaces.ONETOMANY, interfaces.MANYTOMANY): | ||||||||
if not type_._meta.connection: | ||||||||
return Field(List(type_), **field_kwargs) | ||||||||
|
||||||||
if connection_field_factory_ is None: | ||||||||
connection_field_factory_ = BatchSQLAlchemyConnectionField.from_relationship if batching_ else \ | ||||||||
default_connection_field_factory | ||||||||
jnak marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
# TODO Allow override of connection_field_factory and resolver via ORMField | ||||||||
return connection_field_factory_(relationship_prop, obj_type._meta.registry, **field_kwargs) | ||||||||
|
||||||||
return Dynamic(dynamic_type) | ||||||||
|
||||||||
|
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from graphene.utils.get_unbound_function import get_unbound_function | ||
|
||
|
||
def get_custom_resolver(obj_type, orm_field_name): | ||
""" | ||
Since `graphene` will call `resolve_<field_name>` on a field only if it | ||
does not have a `resolver`, we need to re-implement that logic here so | ||
users are able to override the default resolvers that we provide. | ||
""" | ||
resolver = getattr(obj_type, 'resolve_{}'.format(orm_field_name), None) | ||
if resolver: | ||
return get_unbound_function(resolver) | ||
|
||
return None | ||
|
||
|
||
def get_attr_resolver(obj_type, model_attr): | ||
""" | ||
In order to support field renaming via `ORMField.model_attr`, | ||
we need to define resolver functions for each field. | ||
|
||
:param SQLAlchemyObjectType obj_type: | ||
:param str model_attr: the name of the SQLAlchemy attribute | ||
:rtype: Callable | ||
""" | ||
return lambda root, _info: getattr(root, model_attr, None) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.