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

How can I reference an enum type created by SqlAlchemy mapping ? #96

Closed
mariogarcia opened this issue Nov 27, 2017 · 6 comments
Closed

Comments

@mariogarcia
Copy link

mariogarcia commented Nov 27, 2017

Hi:

I would like to reuse an Enum type I've mapped in one class. Lets say I had the class Patient:

class Patient(Base):
    __tablename__ = 'patients'
    id = Column(CHAR(32), default=generate_uuid, nullable=False, primary_key=True)
    name = Column(Text)
    sex = Column(Enum('MALE', 'FEMALE', name='Sex'), nullable=False)
from graphene import relay
from graphene_sqlalchemy import SQLAlchemyObjectType

from app.models.patient import Patient as PatientModel


class Patient(SQLAlchemyObjectType):
    class Meta:
        model = PatientModel
        interfaces = (relay.Node,)

That is perfectly mapped to a Sex graphql type. But if I would like to create a new Patient, How can I reference the Sex enum type generated by graphene-sqlalchemy e.g in an input type ?

class CreatePatient(relay.ClientIDMutation):
    class Input:
        name = graphene.String()
        sex = ???

BTW I'm using latest master branch version.

@sboisson
Copy link

I had the same problem and I used the following workaround:

  • Use a standalone enum.Enum as the reference enum type (no inline definition)
  • Wrap into a SQLAlchemy enum: sqlalchemy.Enum(SexType, name='SexType’)
  • Reference the SQLAlchemy in the model
  • Create graphene mappings (graphene.Enum.from_enum doesn’t work with SQLAlchemy):
SexType = graphene.Enum('SexType', model.SexType.__members__.items())

(...)
@convert_sqlalchemy_type.register(sqlalchemy.types.Enum)
def convert_enum_to_enum(type, column, registry=None):
    if type.name == 'SexType':
        return graphene.Field(
            SexType,
            description=getattr(column, 'doc', None),
            required=not(getattr(column, 'nullable', True)),
        )
    (...)

@mariogarcia
Copy link
Author

Thanks @sboisson sounds good to me 👍

@sboisson
Copy link

You're welcome! You might want to watch issue #78 for built-in support

@annshress
Copy link

Hi there! Do we have a better way to achieve this, now? apart from doing if...else if.

@erikwrede
Copy link
Member

Since #78 and #98 are merged, I expect this problem to be fixed.
@annshress, I don't know what you are referring to with if...else if. If your problem persists, please open a new issue, including an example of your Models. 🙂

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related topics referencing this issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants