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

Feature Request: Include Relationship attributes in Entity json output #406

Open
ravensorb opened this issue Oct 27, 2015 · 10 comments
Open

Comments

@ravensorb
Copy link
Contributor

I would be great if the "public" view of a relationship could be included in the standard entity json results using something like an "attributes" or "_rel" property

Example

{
    "name": "Grateful Dead",
    "id": "08ec4faa77254b90ac6aeeffde8cf500",
    "songs": [
        {
            "attributes": { "id": "<relationship id>", "dateCreated": "<date created>", "myProperty": "<value>" },  // This is the new line that is the "public view"
            "name": "Here Comes Sunshine",
            "id": "3db677f0c955442e8ac00e39d1a6b6fc",
        }
    ]
}

or

{
    "name": "Grateful Dead",
    "id": "08ec4faa77254b90ac6aeeffde8cf500",
    "songs": [
        {
            "_rel": { "id": "<relationship id>", "dateCreated": "<date created>", "myProperty": "<value>" },  // This is the new line that is the "public view"
            "name": "Here Comes Sunshine",
            "id": "3db677f0c955442e8ac00e39d1a6b6fc",
        }
    ]
}   
@amorgner
Copy link
Member

amorgner commented Nov 5, 2015

I like this, and there's already something similar being worked on. Stay tuned. :-)

@ravensorb
Copy link
Contributor Author

excellent -- one request, it would be nice to be able to configure what relationship attributes were included in the output 😄

@amorgner
Copy link
Member

amorgner commented Nov 5, 2015

Yes, and there's already existing functionality for nodes (header fields) we can also use for the rel attributes.

@ravensorb
Copy link
Contributor Author

would that require that we use the GET functionality to specify the attributes? Or is it more of a way of indicating which attributes are used via the schema editor (prefer the latter)

@cmorgner
Copy link
Member

cmorgner commented Dec 4, 2015

I think it was actually possible to do what you want to achieve for quite some time. You can use the built-in functions incoming() and outgoing() to get the relationships of an entity. That means you can create function properties on a node to expose the relationships you're interested in:

out: outgoing(this)
in:   incoming(this, 'KNOWS')

You can even specify the relationship type (see the in property).

One of the more recent additions to Structr was a _path property that exposes the last path segment of the path over which the entity was reached. This will only work for nested JSON structures and not for toplevel entities:

path: this._path

Will contain the most recent relationship on the path to the entity you're currently viewing.

Does that help?

@ravensorb
Copy link
Contributor Author

I am not sure I understand. How can I use the information about to create a new property on my entity that returns the attribute information for relationships via the REST api?

@amorgner
Copy link
Member

In the REST API there's a special view named _structr_graph that contains information about the start and end node:

$ curl -HX-User:admin -HX-Password:admin http://localhost:8082/structr/rest/Bar
{
   "query_time": "0.000706039",
   "result_count": 1,
   "result": [
      {
         "foo": {
            "bars": [
               {
                  "foo": {
                     "bars": [],
                     "type": "Foo",
                     "id": "45a51d3bab374142852d1f5e7f8cdf8b"
                  },
                  "type": "Bar",
                  "id": "244408a68b594123bb19e41de13565b3"
               }
            ],
            "type": "Foo",
            "id": "45a51d3bab374142852d1f5e7f8cdf8b"
         },
         "type": "Bar",
         "id": "244408a68b594123bb19e41de13565b3"
      }
   ],
   "serialization_time": "0.000078226"
}
$ curl -HX-User:admin -HX-Password:admin http://localhost:8082/structr/rest/Foo/out
{
   "query_time": "0.001432972",
   "result_count": 1,
   "result": [
      {
         "id": "cb40a2e4375c45ef8def36e9e0f4145a",
         "type": "FooHASBar",
         "relType": "HAS",
         "sourceId": "45a51d3bab374142852d1f5e7f8cdf8b",
         "targetId": "244408a68b594123bb19e41de13565b3"
      }
   ],
   "serialization_time": "0.000040121"
}
$ curl -HX-User:admin -HX-Password:admin http://localhost:8082/structr/rest/Foo/out/_structr_graph
{
   "query_time": "0.008784007",
   "result_count": 1,
   "result": [
      {
         "id": "cb40a2e4375c45ef8def36e9e0f4145a",
         "type": "FooHASBar",
         "relType": "HAS",
         "sourceNode": {
            "id": "45a51d3bab374142852d1f5e7f8cdf8b",
            "name": "A foo",
            "type": "Foo"
         },
         "targetNode": {
            "id": "244408a68b594123bb19e41de13565b3",
            "name": "A bar",
            "type": "Bar"
         }
      }
   ],
   "serialization_time": "0.000071178"
}

Please note that the view will probably be renamed to _graph.

We're working on a similar view to include relationship information in node queries.

@ravensorb
Copy link
Contributor Author

Sounds good 😄 Question, any chance this attribute that contains the related attributes that is returned on the node query could be used to write updates to back via POST?

My thought it that this would help reduce the number of HTTP calls back to the user - so I could update a node and a few of the relationship attributes in a single call, that would be great.

@ravensorb
Copy link
Contributor Author

Any thought on this? Right now, for every entity retrieved I need to make a corresponding HTTP GET request to retrieve the relationship attributes. On mobile and low bandwidth connections (data does still cost money in a LOT of places) this is incredibly expensive :)

@ravensorb
Copy link
Contributor Author

@amorgner I was thinking about this and I don't think the _structr_graph (or _graph) output is going to be very usable. It doesn't line up to any existing entity structure which makes it very difficult to work with in an application. We end up needing to make the query as yet another GET query (structr is getting to be MASSIVELY chatty with all of the GET requests that need to be made), then once we get the results back we need to iterator through them to find the property we need, then extract the value (and possibly due this multiple times), then we would normally put this into an intermediate DTO so that we can work with it within the application.

Any thought on going back to the original idea? Provide a way to expose related properties as a "view" in the original GET request (maybe settings header or query param to include/exclude)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants