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

include related fields in response other than custom FETCH #2

Closed
maikotz opened this issue Apr 19, 2020 · 2 comments
Closed

include related fields in response other than custom FETCH #2

maikotz opened this issue Apr 19, 2020 · 2 comments

Comments

@maikotz
Copy link

maikotz commented Apr 19, 2020

Hi,

first of all, thanks for the project!
Is it possible to include related fields in GET requests as well?

I want to use Apollo GraphQL on top but can't implement your custom FETCH method there.

My model is a company that has multiple arrays of id's (users, addresses, invoices, etc). These id's are on other microservices. With FETCH I can easily query them as related ressources.

Or am I missing another way here?

Best regards

models.py

class Company(BaseModel):
    __tablename__ = 'company'
    id = Column(Integer, primary_key=True)
    name = Column(String(150), nullable=False, unique=True)
    addresses = relationship("Address", foreign_keys="Address.company_id", back_populates='company')

class Address(BaseModel):
    __tablename__ = 'addresses'
    id = Column(Integer, primary_key=True)
    address_id = Column(Integer)
    company_id = Column(Integer, ForeignKey('company.id'))
    company = relationship("Company", foreign_keys=company_id)

curl --request FETCH call
{ "fields": [ "name" ], "related": { "addresses": [ "address_id" ] } }

@cs91chris
Copy link
Owner

cs91chris commented Apr 29, 2020

Hi,
I do not well understand what output you expect.
The models have to extend the Model class of flask_autocrud (I don't know what your BaseModel is). Your models should look like this (I have not tested):

from flask_autocrud import Model

db = SQLAlchemy()


class Company(db.Model, Model):
    __tablename__ = 'company'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(150), nullable=False, unique=True)


class Address(db.Model, Model):
    __tablename__ = 'addresses'
    id = db.Column(db.Integer, primary_key=True)
    address_id = db.Column(db.Integer)
    company_id = db.Column(db.Integer, ForeignKey('company.id'))
    company = relationship(Company, backref="addresses")

You can see an example, where entities are artists and albums, here

def test_related(client):

Actually is not possible, with GET method, to fetch multiple related resources and applying filters on it.

Let me know

@cs91chris cs91chris reopened this May 24, 2020
@cs91chris
Copy link
Owner

cs91chris commented May 24, 2020

Hi @maikotz there is new release available. Now even multiple related resource are retrived with GET.
The following is an example that should fit your need:

curl -XGET 'http://127.0.0.1:5000/artist?_related=Album&_fields=Name&_no_links='

{
    "ArtistList": [
        {
            "AlbumList": [
                {
                    "AlbumId": 4
                },
                {
                    "AlbumId": 348
                },
                {
                    "AlbumId": 349
                }
            ],
            "ArtistId": 1,
            "Name": "AC/DC"
        },
        {
            "AlbumList": [
                {
                    "AlbumId": 3
                }
            ],
            "ArtistId": 2,
            "Name": "Accept"
        }
    ]
}

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

No branches or pull requests

2 participants