question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a way to serve custom properties on model classes

See original GitHub issue

In SQLAlchemy, you often want to provide additional custom properties on the model classes, like this:

class User(Base):
    __tablename__ = 'users'
    first_name = Column(String)
    last_name = Column(String)

    @property
    def full_name(self):
        return self.first_name + ' ' + self.last_name

Unfortunately, such properties are currently not made accessible in GraphQL by graphene_sqlalchemy. While there is a way to exclude existing column properties using the Meta attributes exclude_fields and only_fields, there is no way to include custom properties such as full_name from the above model.

One possible solution is to use hybrid properties which are now supported by graphene_sqlalchemy since 2.0. However, “ordinary” properties should also be supported. Often you dont really want or need hybrid properties - it is not always necessary or feasible to provide the property on the class level. Also, the conversion of hybrid properties currently has the limitation that it always generates String fields.

So I believe we need some mechanism for proxying arbitrary properties from SQLAlchemy to Graphene objects. I’m not yet sure whether all custom public properties (i.e. those not starting with an underscore) except those who are explicitly excluded should be proxied by default (like it is done with hybrid properties), or whether there should be a Meta attribute with a list of properties that must be explicitly set.

We probably also need to add a way to specify which type the (normal or hybrid) properties shall be converted to. This could also be defined in a Meta attribute, or maybe we could check for Python type hints if nothing is defined, like this:

    def full_name(self) -> str:
        return self.first_name + ' '  + self.last_name

    def current_age(self) -> int:
        return current_year() - self.year_of_birth

I am willing to contribute with code, but first I want to get some feedback regarding this idea from others.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:11
  • Comments:18 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
flewellyncommented, May 28, 2018

Is there any update on this capability? What can be done in the meantime?

10reactions
flewellyncommented, Nov 27, 2018

Just bumping this again to inquire.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MVC - Using Custom Class for Model Property - Stack Overflow
So I have a Model in my web app that has a property that I would like use a custom class as the...
Read more >
Adding a custom property class - BMC Documentation
To add a property class · Select Configuration > Property Dictionary View. · Select Custom Property Classes or navigate to an existing custom...
Read more >
Custom properties for devices - Configuration Manager
Set properties via UI · In the Configuration Manager console, go to the Assets and Compliance workspace, and select the Devices node. ·...
Read more >
Model | LoopBack Documentation
Property Decorator. ID Properties; Array Property Decorator; Custom Validation Rules and Error Messages; ENUM Property. How LoopBack Models Map to Database ...
Read more >
About Custom Classes, Behavior Models, and Property Pages
Adding Folders in the Metamodel Hierarchy ... To add a new folder, right-click Classes, Behavior Models, or Property Pages, or the folder where...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found