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 query string should respect relationship names in response #160

Open
geoidesic opened this issue Jun 25, 2021 · 0 comments
Open

Comments

@geoidesic
Copy link
Collaborator

Currently given two tables Enquiries and People.
An association on Enquiries:

$this->belongsTo('Friends', ['className' => 'People', 'foreignKey' => 'friend_id']);

An association on People:

$this->hasMany('FriendEnquiries', ['className' => 'Enquiries', 'foreignKey' => 'friend_id']);

You would include this relationship in a query string like this:

http://localhost:8080/api/enquiries/1?include=friend

The response however currently would include the friend with the key people rather than friends. So the front-end then has no way of tying this data back to the friend association.

I believe this is incorrect and that it should include this data using the given query string key as the object key in the included node of the response.

So to be clear: this is the response from the above query with an inline comment showing the suggested change:

{
    "data": {
        "type": "enquiries",
        "id": "1",
        "attributes": {
            "name": "RSVP"
        },
        "relationships": {
            "friend": {
                "links": {
                    "self": "\/api\/enquiries\/1\/relationships\/friend",
                    "related": "\/api\/enquiries\/1\/friend"
                },
                "data": {
                    "type": "people",    
                    "id": "3"
                }
            }
        },
        "links": {
            "self": "\/api\/enquiries\/f4882be5-53c8-4927-a2af-75e34488a103"
        }
    },
    "included": [
        {
            "type": "people",         //<---- this should be "friend"
            "id": "3",
            "attributes": {
                "name": "George"
            },
            "relationships": {
                "enquiries": {
                    "links": {
                        "self": "\/api\/people\/relationships"
                    }
                }
            },
            "links": {
                "self": "\/api\/people\/3"
            }
        },
    }
}

Am I wrong?

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

1 participant