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

Upgrading to graphene 2.0 #85

Closed
kavink opened this issue Nov 5, 2017 · 1 comment
Closed

Upgrading to graphene 2.0 #85

kavink opened this issue Nov 5, 2017 · 1 comment

Comments

@kavink
Copy link

kavink commented Nov 5, 2017

Based on example from @aminghadersohi on another issue I adapted that solution for my use case. I am using something like this and its working great for now, But am unable to migrate this to Graphene 2.0. After migrating to 2.0 based on upgrade guide, either the relay args and extra args both dont work.

I liked this approach because I can query on any configured field from model, based on how I configure it.

If there is a better approach on how I can query any fields please let me know, I can just migrate to that .

Would it be possible to point me how do I migrate this?

Base = sqlalchemy.ext.declarative.declarative_base()

class ClientRoleStore(Base):
    __tablename__ = "clientrole"
    uuid = Column(CHAR(36), primary_key=True, nullable=False, default=lambda: str(uuid1()))

class ClientRole(SQLAlchemyObjectType):
    class Meta:
        model = ClientRoleStore
        interfaces = (relay.Node,)

all_types = [
    ClientRole,
    # there are other fields but I have kept it simple here
]


query_args = {
    "clientrole": [("name", graphene.String()), ("add", graphene.String())],

}

query_attrs = {'node': relay.Node.Field()}


def resolve(cls, self, args, context, info):
    RELAY_ARGS = ['first', 'last', 'before', 'after']

    query = cls.get_query(context)
    if args:
        for key, value in args.items():
            if key not in RELAY_ARGS:
                query = query.filter(getattr(model, key) == args[key])
    return query.all()


for type_cls in all_types:
    model = type_cls.__dict__['_meta'].model
    name = model.__tablename__

    extra_args = {key[0]: key[1] for key in query_args[name]}

    query_attrs[name + 's'] = SQLAlchemyConnectionField(type_cls, **extra_args)
    query_attrs[name] = relay.Node.Field(type_cls)
    query_attrs['resolve_' + name + 's'] = partial(resolve, type_cls)

Query = type("Query", (graphene.ObjectType,), query_attrs)

@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 25, 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

1 participant