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

Implement Standalone Convert; Enum Handling; User Edge/Connection Classes; Column Alias Handling #25

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

gravy-jones-locker
Copy link
Contributor

@gravy-jones-locker gravy-jones-locker commented Sep 4, 2023

Description

This includes a number of changes, including:

  1. mapper.type now calls mapper.convert - this allows mapper.convert to be called within user code, which makes the relevant functionality accessible where a decorator is not appropriate.
  2. strawberry.enum is called against SQLAlchemy Enum columns.
  3. Rather than relying on dynamically generated, model specific classes, the user can now pass Edge and Connection classes to the mapper constructor. This is necessary where those Edge/Connection classes are used elsewhere in user schemas.
  4. Relationship mapping is updated such that user-defined column names are taken into account rather than just model attribute names.

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

@Ckk3
Copy link
Contributor

Ckk3 commented Sep 5, 2023

up

Copy link
Contributor

@mattalbr mattalbr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the change! I'm still new on the project, so I left a few comments but added Tim and Erik as reviewers for their expertise.

If you could also add a RELEASE.md file, pushing a new commit should trigger all of the new automated testing that I got working this week (feel free to ignore the Lint step which I haven't gotten green yet).

@@ -259,7 +268,7 @@ def _convert_column_to_strawberry_type(
corresponding strawberry type.
"""
if isinstance(column.type, Enum):
type_annotation = column.type.python_type
type_annotation = strawberry.enum(column.type.python_type)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my reading of strawberry.enum it should be okay to wrap a type twice, but it would be great to have a test that does this. E.g. in my company, we already annotate all of our enums with @strawberry.enum, and I'm unsure if that would set python_type to the wrapped or unwrapped enum, and what would happen here.

Just a test that does

@strawbery.enum
class MyEnum(enum.StrEnum):
  FOO = "FOO"

class MyModel(Base):
  my_enum = Column(Enum(MyEnum))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thought for the tests: what does that second wrapping do to any of the initial settings? e.g. If I @strawberry.enum(name="DifferentName") does it still respect the overwritten name?

@@ -151,6 +151,8 @@ def __init__(
extra_sqlalchemy_type_to_strawberry_type_map: Optional[
Mapping[Type[TypeEngine], Type[Any]]
] = None,
edge_type: Type = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added Erik to comment on this, but we should probably at the very least have a Protocol that these types should follow

use_federation=False,
) -> Any:
"""
Do type conversion. Usually accessed using typical .type decorator. But
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if this is to be part of the public API, we should probably beef up the docstring a bit. I still don't really understand when someone would need this, and why someone wouldn't just call Employee = mapper.type(models.Employee)(Employee)

At the least, exercising this in a unit test to have some basic API-level test coverage and demonstrate the usage could be nice, but might be worth waiting until Erik or Tim weighs in.

@botberry
Copy link
Member

botberry commented Oct 3, 2023

Hi, thanks for contributing to Strawberry 🍓!

We noticed that this PR is missing a RELEASE.md file. We use that to automatically do releases here on GitHub and, most importantly, to PyPI!

So as soon as this PR is merged, a release will be made 🚀.

Here's an example of RELEASE.md:

Release type: patch

Description of the changes, ideally with some examples, if adding a new feature.

Release type can be one of patch, minor or major. We use semver, so make sure to pick the appropriate type. If in doubt feel free to ask :)

Copy link
Collaborator

@TimDumol TimDumol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Change PR title and description? Seems to only have the edge/connection type changes
  2. It's been a while since I've touched this code, but don't we need a distinct edge / connection type for each relationship (or at least, node type in a relationship)? e.g., if Book -> Authors and Book -> Reviews, you'd need a different edge / connection type for Author and Review, which this doesn't seem to support.

@erikwrede
Copy link
Member

Hey, let's delay the enum part of this until we've standardized strawberry-graphql/strawberry#3543
This will allow us to just use any enum and have strawberry's core logic handle the rest.

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

Successfully merging this pull request may close these issues.

7 participants