-
Notifications
You must be signed in to change notification settings - Fork 21
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
feat(smart views): smart views can access nested relationship #203
base: main
Are you sure you want to change the base?
Conversation
To add more context about how JSON API works: |
package.json
Outdated
@@ -35,7 +35,7 @@ | |||
"express-jwt": "5.3.1", | |||
"inflected": "2.0.4", | |||
"ip-utils": "git+https://github.com/ForestAdmin/ip-utils.git#5f88562ba53fedcdc0374937fca0fdb71fa4923c", | |||
"jsonapi-serializer": "3.4.1", | |||
"jsonapi-serializer": "3.6.1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fix some issue with the serializer and allow to return null from relationshipLinks function and not include the link in this case
function getAttributesFor(dest, fields) { | ||
function getRelatedLinkForHasMany(modelName, relationshipName) { | ||
return function (record, current, parent) { | ||
if (current && current[relationshipName]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not want the link to be set if the relation is already included in the payload
dest[fieldName] = { | ||
ref: fieldReference, | ||
nullIfMissing: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this or the links will not be included if the relation is not in the records sent to the serializer
if (_.isArray(field.type)) { | ||
dest[fieldName].ignoreRelationshipData = true; | ||
if (_.isArray(field.type) || !include) { | ||
dest[fieldName].ignoreRelationshipData = include; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not include from second level (We never include relations of a relation)
src/serializers/resource.js
Outdated
@@ -141,7 +158,35 @@ function ResourceSerializer(Implementation, model, records, integrator, opts, me | |||
}); | |||
} | |||
|
|||
var serializationOptions = { | |||
function defineRelationshipId(records, fields, goDeeper = true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recursive function to initialize belongsTo with an id if not set (based on foreignKey) so the serializer can return the { data: { id: 2 } }
const fieldName = field.field; | ||
const [referenceType,referenceKey] = field.reference.split('.'); | ||
const referenceSchema = Schemas.schemas[referenceType]; | ||
if (goDeeper && record[fieldName]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only do it on one level. But we could do it recursively like on forest-rails (@arnaudbesnier Your call)
Let's keep this for later. We need stability now and I don't want to introduce unecessary feature. Especially on the liana side. |
3944c3b
to
077553b
Compare
12de3c3
to
33e1a19
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this contribution! Since this repository now uses (experimental) semantic release, you might have to:
- Remove entry to changelog.
- Change the target to master.
- Change the title (you could
fix(smart views): smart views can access nested relationship
).
Let me know if I can help!
b4141a8
to
55fd940
Compare
Code Climate has analyzed commit 9ece720 and detected 0 issues on this pull request. The test coverage on the diff in this pull request is 78.3% (70% is the threshold). This pull request will bring the total coverage in the repository to 60.6% (0.6% change). View more on Code Climate. |
Description
The purpose of this PR is to return correctly all relationships from the liana. This will allow to use simple getters on Smart View.
Requirements
https://github.com/ForestAdmin/forestadmin/pull/1022
Pull Request checklist: