-
Notifications
You must be signed in to change notification settings - Fork 65
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
Adding Links to resources and included #54
Comments
For your case it would look like ...
'somelink' => new Link('link')
... |
Have I answered you question? |
Thanks for your reply. I get
|
Oh yeah. Sorry for incorrect example. Tests have it's own special 'development' version of schema. I'll update 'normal' example in a few minutes. |
Thanks! |
which produces {
"data": {
"type": "sites",
"id": "1",
"attributes": {
"name": "JSON API Samples"
},
"relationships": {
"posts": {
"data": [
{
"type": "posts",
"id": "321"
}
],
"links": {
"some-sublink": "http://example.com/sites/1/resource-sublink",
"external-link": "www.example.com"
}
}
},
"links": {
"self": "http:\/\/example.com\/sites\/1"
}
},
"included": [
...
]
} |
Is good now? |
Is it on purpose that links can only be added to the links of a
But maybe the link objects are not supposed to be used like that? Are they restricted to having only a self link and a related link? |
links could be added to the resource as well |
@kasperhelweg Does it work for you? |
Yes, it works. Thanks. One final question: how would I have the links for a resource appear in
that will show self links. But can I add other links to those objects? |
Do you mean to show/hide some relationship links depending on whether object is a main resource or included resource? |
If for example I have a resource that that looks like this
and I include that with another resoruce
|
But I can only see that I can set
which will include the |
I haven't thought about such a scenario 😄 Naive approach $encoder = Encoder::instance([
Author::class => AuthorSchema::class,
Comment::class => CommentSchemaWithoutLinks::class,
]);
echo $encoder->encode($author) . PHP_EOL;
// and with links for comments when main resource
$encoder = Encoder::instance([
Author::class => AuthorSchema::class,
Comment::class => CommentSchemaWithLinks::class,
]);
echo $encoder->encode($comment) . PHP_EOL; Better approachSchema could act dynamically when created with So for your question it might be something like $addLinksInComments = true;
$schemaClosure = function ($factory, $container) use (&$addLinksInComments) {
$schema = new CommentSchema($factory, $container, $addLinksInComments);
return $schema;
};
$encoder = Encoder::instance([
Author::class => AuthorSchema::class,
Comment::class => $schemaClosure,
]);
echo $encoder->encode($author) . PHP_EOL;
UPD Actually this feature a is used in tests very actively. In the code example I gave earlier you can see it in action. |
This feature might look as non-trivial. Are you OK with it or need some help? |
Well, I'm not sure exactly what the example does 😃 My problem is that I can't figure out how to define links in a schema in general. For example, when extending |
I can add links to a top-level object by doing |
You can't (maybe yet, I don't think it's difficult to add). Do you want Schema to have top level links that will be added to document on encoding? |
Yes, something like that... If it makes sense to have that 😄 |
I will create a branch and we can work on your questions. As far as I understand you've got 2 issues
|
But if the main resource-representation contains a number of links, I guess it would make sense that those links could also be included if that resource is included with another resource? Just like all the |
That would be cool! Yes, those are my issues 😄 |
@kasperhelweg I've found even easier way to do that. Check out this sample which addresses your second issue. It corresponds with this code in Schema. |
@kasperhelweg With default top level links taken from schema there is a problem. What if encoded data is array? Should links be added and if yes which ones, first resource's or from all resources? |
I'm afraid the only reasonable way to add these links is to pass them to Encoder on encoding. What are your thoughts on that? |
Cool. Just to be clear that we are on the same page 😄 , if I do
would be a primary resource representation. I can easily do this by
I would expect to get the exact same representation in the
The problem is that I can't easily do What I imagine is that there is simply a I would gladly help working on this at some point. |
Alternatively, maybe I could pass the links to the encoder but something like
where
not sure if that makes sense at all... |
I think you mess a bit with links. In your first example link will be added to document level but not to comment (very easy to miss). |
it will look like this {
"links" : {
"link-as-param-to-encode" : "/could/be/anything"
},
"data" : {
"type" : "people",
"id" : "9",
"attributes" : {
"first_name" : "Dan",
"last_name" : "Gebhardt"
},
"links" : {
"self" : "http://example.com/people/9"
}
}
} See? You have links at document level and in resource. |
Oh, you'r right. |
OK, wrap it up. We have solved issue with dynamic Schema, haven't we? Anything left unanswered? |
Nope, that's all good! Thanks for the effort!! |
Super. Don't forget to support the project with ⭐ 😉 |
I will! |
I have a somewhat similar situation: {
"data": [
{
"type": "domain",
"id": "example.org",
"attributes": {
"name": "example.org",
"ipV4Address": "1.1.1.1"
},
},
{
"type": "domain",
"id": "as.com",
"attributes": {
"name": "as.com",
"ipV4Address": "1.1.1.2"
}
}
]
} and I'd like to include a links object for each resource of type domain. $encoder->withLinks won't work since it will added at the top level, and relationships seems a bit overkill. Below what I would like to achieve: {
"data": [
{
"type": "domain",
"id": "example.org",
"attributes": {
"name": "example.org",
"ipV4Address": "1.1.1.1"
},
"links": {
"configuration": "/domains/example.org/configuration"
}
},
{
"type": "domain",
"id": "as.com",
"attributes": {
"name": "as.com",
"ipV4Address": "1.1.1.2"
},
"links": {
"configuration": "/domains/as.com/configuration"
}
}
]
} NOTE: I just want to know if something like this is easily doable, if not I think it will have to go for a relationships entry for each resource. |
Now that I think about it I don't really want to use relationships since I will be forced to resolve DNS records for the given domain and that's just to costly. I would really like to provide a link for an endpoint providing that information, should the client ever need to retrieve it. |
@yoanisgil it seems the lib restricts links to 'self' only on resource level. I've looked to the spec and there is no such restriction (likely it was in earlier revisions). It could be an enhancement/bug. If you still want to add links despite your DNS issues you can use relationships. I don't see any real difference to have those links on resource level or relationship level. Anyways thanks for spotting this issue. I'll create an enhancement for it. |
Not a problem. The issue with relationships is that I don't really want to return DNS records because I will be forced to resolve them which is a rather expensive operation. That's why I just want to provide a link to an endpoint providing such information. |
How can one add links to a resource object such as
where "somelink" is the link to be added? Thanks!
The text was updated successfully, but these errors were encountered: