Skip to content

Add subresource docs - #199

Merged
soyuka merged 1 commit into
api-platform:masterfrom
soyuka:subresource-docs
Sep 8, 2017
Merged

soyuka merged 1 commit into
api-platform:masterfrom
soyuka:subresource-docs

Conversation

@soyuka

@soyuka soyuka commented Apr 28, 2017

Copy link
Copy Markdown
Member

No description provided.

Comment thread core/operations.md Outdated
}
```

If the relation you put the subresource on is a `ToMany`, you will retrieve a collection.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you put the subresource on a relation that is toMany, you will retrieve...

@soyuka
soyuka force-pushed the subresource-docs branch 5 times, most recently from adb9fb2 to 2930b49 Compare April 28, 2017 14:37

@dunglas dunglas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also write item and resource (without uppercased first letter) for consistency.

Comment thread core/operations.md Outdated
/**
* Answer.
*
* @ORM\Table(name="answer")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This annotation is useless and should be removed.

Comment thread core/operations.md Outdated
class Answer
{
/**
* @ORM\Column(name="id", type="integer")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name attribute is useless and should be removed.

Comment thread core/operations.md Outdated
private $id;

/**
* @ORM\Column(name="content", type="string", nullable=false)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All attributes are useless and should be removed.

Comment thread core/operations.md Outdated
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Table(name="question")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @Table annotation is useless and should be removed.

Comment thread core/operations.md Outdated
class Question
{
/**
* @ORM\Column(name="id", type="integer")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name attribute is useless and should be removed.

Comment thread core/operations.md Outdated

/**
* @ORM\OneToOne(targetEntity="Answer", inversedBy="question")
* @ORM\JoinColumn(name="answer_id", referencedColumnName="id", unique=true)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name attribute is useless and should be removed.

Comment thread core/operations.md Outdated
/**
* @ORM\OneToOne(targetEntity="Answer", inversedBy="question")
* @ORM\JoinColumn(name="answer_id", referencedColumnName="id", unique=true)
* @ApiProperty(subresource=true)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put this annotation first to increase its visibility.

Comment thread core/operations.md Outdated
/**
* @ORM\OneToOne(targetEntity="Question", mappedBy="answer")
*/
private $question;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing getters/setters (or the property must be public). Same for Question.

Comment thread core/operations.md Outdated
}
```

Note that all we had to do is to set up `@ApiProperty(subresource=true)` on the `Question::answer` relation. Because the `answer` is a `OneToOne` relation, we know that this subresource is an Item. Therefore the response will look like this:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have to do?
To set up the @ApiProperty(subresource=true) annotation?
Question::$answer property.

Comment thread core/operations.md

You may want custom groups on subresources. Because a subresource is nothing more than a collection operation, you can set `normalization_context` or `denormalization_context` on that operation. To do so, you need to override `collectionOperations`. Base on the above operation, because we retrieve an answer, we need to alter it's configuration:

<configurations>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this that this tag is supported.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we' re using it above though.

@soyuka

soyuka commented May 15, 2017

Copy link
Copy Markdown
Member Author

Thanks for the review @dunglas, still we may need to rethink how subresources are declared (see our recent discussion on the subject with @teohhanhui).

teohhanhui [12:21 PM]
@soyuka I think for the subresource feature, it'd be really nice to have this:
GET /users/2.jsonld

 "@id": "/users/2",
 "posts": "/users/2/posts"
}

This way we can entirely avoid the join.
[12:23]
we do need better annotation / config syntax.. 😄 (edited)
soyuka [2:02 PM]
hmm, would be possible with the current annotation, if you have a subresource on the given property, normalize as a subresource IRI
[2:02]
though, if I still want the joined collection we need something to avoid conflicts
teohhanhui [6:13 PM]
with the current annotation, it's not configurable..
[6:14]
that's why we need to split that up.. haha
[6:15]
subresource=true -> subresourceOperation=true, maybe (edited)
[6:17]
but probably we need some rethinking about this 🙂
[6:21]
PropertyMetadata::hasSubresource is totally misleading to me.. simply because it should not be treated as first-class metadata about a property.. it should belong in the attributes, since it serves as a hint on whether to expose such a subresource
[6:23]
oh, and the semantics is also wrong.. haha.. we're trying to say, "expose a subresource endpoint for this property", not "this property has a subresource" (????)
soyuka [6:44 PM]
I think that having it as a first-class metadata is more user-friendly. expose a subresource endpoint for this property yeah this is correct, let me know if you have better naming ideas I'm all ears 🙂.

@bendavies

Copy link
Copy Markdown
Contributor

this PR needs updating for api-platform/core#1206

@soyuka
soyuka force-pushed the subresource-docs branch from 2930b49 to a6c027f Compare July 6, 2017 11:31
@soyuka

soyuka commented Jul 6, 2017

Copy link
Copy Markdown
Member Author

Bumped with @ApiSubresource and comments fixing.

Comment thread core/operations.md Outdated
}
```

If you put the subresource on a relation that is `ToMany`, you will retrieve a collection.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`ToMany` -> to-many

Comment thread core/operations.md Outdated
}
```

Note that all we have to do is to set up `@ApiSubresource` annotation on the `Question::answer` property. Because the `answer` is a `OneToOne` relation, we know that this subresource is an Item. Therefore the response will look like this:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OneToOne -> to-one

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Item -> item

Comment thread core/operations.md Outdated

If you put the subresource on a relation that is `ToMany`, you will retrieve a collection.

Last but not least, subresources can be nested so that `/foos/1/bars/1/foobars` will get `foobars` belonging to `bar=1` itself belonging to `foo=1`. To do this, just add as many subresources annotations as you need.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subresources can be nested, such that /questions/42/answer/comments will get the collection of comments for the answer to question 42.

Comment thread core/operations.md Outdated

Since ApiPlatform 2.1, you can declare subresources. A "subresource" is a resource exposed under a path with parameters dependent on another resource. It can result in a collection or an item.

For example, let's create two entities (Question, Answer) and set up a subresource so that `/question/42/answer` gives us the answer to the question `42`:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

questions/42/answer

the question `42` -> question 42

Comment thread core/operations.md Outdated
```json
{
"@context": "/contexts/Answer",
"@id": "/answers/1",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/answers/42

Comment thread core/operations.md Outdated
"@id": "/answers/1",
"@type": "Answer",
"id": 42,
"content": "42",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Life, the Universe, and Everything 😛

@soyuka
soyuka force-pushed the subresource-docs branch from a6c027f to faca475 Compare August 1, 2017 08:38
@soyuka

soyuka commented Aug 1, 2017

Copy link
Copy Markdown
Member Author

Bump

Comment thread core/operations.md Outdated

## Subresources

Since ApiPlatform 2.1, you can declare subresources. A subresource is a collection or an item that belongs to another resource. The starting point of a resource must be a relation on an existing resource.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The starting point of a subresource ... ?

@soyuka
soyuka force-pushed the subresource-docs branch 3 times, most recently from 6e06d5a to 9f5edf7 Compare September 5, 2017 14:53
@soyuka
soyuka merged commit 8fa3904 into api-platform:master Sep 8, 2017
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

Successfully merging this pull request may close these issues.

5 participants