Skip to content

Use with SqlAlchemy classical mapping #286

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

Closed
eduardomezencio opened this issue Sep 9, 2020 · 2 comments
Closed

Use with SqlAlchemy classical mapping #286

eduardomezencio opened this issue Sep 9, 2020 · 2 comments

Comments

@eduardomezencio
Copy link

I'm trying to use graphene-sqlalchemy in a project where classical mapping is used (https://docs.sqlalchemy.org/en/13/orm/mapping_styles.html#classical-mappings). I can see in some reports here (#130 (comment)) that setting Base.query is a needed step to get things working, and there is no declararive base in classical mapping. Is there a way to make this work without having to change the entire code to use declarative mapping?

When I try to make a query I get An error occurred while resolving field Equipment.id graphql.error.located_error.GraphQLLocatedError: 'Equipment' object has no attribute '__mapper__'

@eduardomezencio
Copy link
Author

Well, after some trial and error (and some basic reading of exception messages, I must admit) I was able to get it working as follows:
When mapping your tables, with the usual method described in sqlalchemy manual:

from sqlalchemy.orm import mapper
mapper(YourClass, your_table_metadata)

you can keep the returned Mapper object in the __mapper__ field of your class

YourClass.__mapper__ = mapper(YourClass, your_table_metadata)

and then it seems to work correctly. But it also looks like when creating your GraphQLView, you need to use the get_context argument as follows:

GraphQLView.as_view(
    "graphql", schema=schema, graphiql=True,
    get_context=lambda: {'session': Session})

where Session is the session registry returned by scoped_session.

@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