Skip to content
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

add prepare_item resolver method #30

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Unreleased
- Clearer error when SQLAlchemy session is not passed in GraphQL context.
- List sorts can be paths across relationships, like `user.name` from `Task`.
Filters can already be across relationships. {issue}`27`
- Default mutation resolvers have a separate `prepare_obj` method that
creates/updates/deletes the object in the session but does not commit. This
can be used to avoid extra commits when wrapping the default resolver with
extra behavior. {issue}`25`


## Version 1.0.0
Expand Down
47 changes: 35 additions & 12 deletions src/magql_sqlalchemy/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,18 @@ def get_item(
.where(self.pk_col == kwargs[self.pk_name])
).scalar_one()

def prepare_item(
self, info: graphql.GraphQLResolveInfo, kwargs: dict[str, t.Any]
) -> t.Any:
"""Get and modify the model instance in the SQLAlchemy session, but do
not commit the session. Calling the resolver calls this and then calls
commit, but this can be used directly when wrapping the resolver with
other behavior.

.. versionadded:: 1.1
"""
raise NotImplementedError

def apply_related(self, session: orm.Session, kwargs: dict[str, t.Any]) -> None:
"""For all relationship arguments, replace the id values with their model
instances.
Expand Down Expand Up @@ -471,6 +483,14 @@ def apply_related(self, session: orm.Session, kwargs: dict[str, t.Any]) -> None:
.all()
)

def __call__(
self, parent: t.Any, info: graphql.GraphQLResolveInfo, **kwargs: t.Any
) -> t.Any:
item = self.prepare_item(info, kwargs)
session = _get_sa_session(info)
session.commit()
return item


class CreateResolver(MutationResolver):
"""Create a new row in the database. Used by :attr:`.ModelManager.create_field`. The
Expand All @@ -481,15 +501,14 @@ class CreateResolver(MutationResolver):
:param model: The SQLAlchemy model.
"""

def __call__(
self, parent: t.Any, info: graphql.GraphQLResolveInfo, **kwargs: t.Any
def prepare_item(
self, info: graphql.GraphQLResolveInfo, kwargs: dict[str, t.Any]
) -> t.Any:
session = _get_sa_session(info)
self.apply_related(session, kwargs)
obj = self.model(**kwargs)
session.add(obj)
session.commit()
return obj
item = self.model(**kwargs)
session.add(item)
return item


class UpdateResolver(MutationResolver):
Expand All @@ -502,8 +521,8 @@ class UpdateResolver(MutationResolver):
:param model: The SQLAlchemy model.
"""

def __call__(
self, parent: t.Any, info: graphql.GraphQLResolveInfo, **kwargs: t.Any
def prepare_item(
self, info: graphql.GraphQLResolveInfo, kwargs: dict[str, t.Any]
) -> t.Any:
session = _get_sa_session(info)
self.apply_related(session, kwargs)
Expand All @@ -515,7 +534,6 @@ def __call__(

setattr(item, key, value)

session.commit()
return item


Expand All @@ -527,13 +545,18 @@ class DeleteResolver(MutationResolver):
:param model: The SQLAlchemy model.
"""

def __call__(
self, parent: t.Any, info: graphql.GraphQLResolveInfo, **kwargs: t.Any
def prepare_item(
self, info: graphql.GraphQLResolveInfo, kwargs: dict[str, t.Any]
) -> t.Any:
session = _get_sa_session(info)
item = self.get_item(info, kwargs)
session.delete(item)
session.commit()
return item

def __call__(
self, parent: t.Any, info: graphql.GraphQLResolveInfo, **kwargs: t.Any
) -> t.Any:
super().__call__(parent, info, **kwargs)
return True


Expand Down