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
Filters implementation #33
Conversation
src/hydra/addFilters.js
Outdated
let promise = fetchResource(resource.url).then(response => { | ||
const resourceFilters = []; | ||
|
||
if (response.filters) { |
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.
if (!response.filters) {
return [];
}
// loop on filters
src/hydra/addFilters.js
Outdated
|
||
const resourceFilter = new Filter(property, filter.variable); | ||
|
||
resourceFilters.push(resourceFilter); |
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.
can be resourceFilters.push(new Filter(property, filter.variable))
src/hydra/addFilters.js
Outdated
for (const filter of response.filters) { | ||
let property = filter.property; | ||
|
||
// TODO : To prevent PropertyFilter, maybe should be handle specifically ? |
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.
what do you mean?
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.
The PropertyFilter
provides null
as property
, but it can be applied to all properties. I thought i could iterate on each properties to create all filters, but this behavior could be different on another type of filter providing null
as property
, so i guess just skipping the filter might be what we want.
{ | ||
property: "id", | ||
variable: "id" | ||
} |
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.
possible to add a filter without property to test the line you commented above?
@@ -1365,7 +1365,7 @@ test("Invalid docs JSON", async () => { | |||
}); | |||
|
|||
test("Invalid entrypoint JSON", async () => { | |||
let entrypoint = `{foo,}`; | |||
const entrypoint = `{foo,}`; |
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.
you don't need brackets, use a simple quote ('
) instead :)
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 change came from #34. It has been removed.
src/Filter.js
Outdated
variable: string; | ||
|
||
/** | ||
* @param {string} property |
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.
Too many spaces?
src/hydra/fetchResource.js
Outdated
import get from "lodash.get"; | ||
|
||
export default async entrypointUrl => { | ||
return await fetchJsonLd(entrypointUrl, { items_per_page: 0 }).then( |
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.
By default, the name of this attribute in API Platform core is itemsPerPage
, we should use it here.
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.
Done
@jfthuillier: To improve readability of this PR, could you rebase your code? It should remove changes from your first PR. |
5751076
to
99d112b
Compare
I can't manage to squash the last |
Just tested and work fine for me (including api-platform/admin#100) |
src/hydra/addFilters.js
Outdated
|
||
const resourceFilters = []; | ||
|
||
for (const filter of response.filters) { |
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 usually use the functional style: response.filters.forEach()
src/hydra/addFilters.js
Outdated
const resourceFilters = []; | ||
|
||
for (const filter of response.filters) { | ||
let property = filter.property; |
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.
Can be const
, the variable can even be removed
src/hydra/fetchResource.js
Outdated
import fetchJsonLd from "./fetchJsonLd"; | ||
import get from "lodash.get"; | ||
|
||
export default async entrypointUrl => { |
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.
resourceUrl
?
src/Filter.js
Outdated
/** | ||
* @property {string} variable - The variable of this field | ||
*/ | ||
export default class Filter { |
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.
I'm not very confident in this structure.
Here is the OpenAPI structure for filters (they did a great job): https://swagger.io/docs/specification/describing-parameters/
And the Hydra one: http://www.hydra-cg.com/spec/latest/core/#x5-2-templated-links
To support both I suggest to rename this class Parameter
with the following structure:
class Parameter {
variable: string;
type: string; // (an IRI)
required: bool;
description: string;
}
3101b8a
to
4757139
Compare
src/hydra/addParameters.js
Outdated
} | ||
|
||
resourceParameters.push( | ||
new Parameter(parameter.variable, "", parameter.required, "") |
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.
@dunglas : what is the type
value you are expecting ?
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.
Something like an XSD type?
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.
It will be the same that the range
attribute of the Field
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.
Tests fixed & and attribute name changed to range
286a937
to
d77fd6a
Compare
src/hydra/addParameters.js
Outdated
const promises = []; | ||
|
||
for (const resource of api.resources) { | ||
let promise = fetchResource(resource.url).then(response => { |
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.
let
-> const
src/hydra/addParameters.js
Outdated
|
||
for (const resource of api.resources) { | ||
let promise = fetchResource(resource.url).then(response => { | ||
if (!response.parameters) { |
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.
You can use a default value:
const resourceParameters = [];
const { parameters = {} } = response;
parameters.forEach(....);
return resourceParameters;
src/hydra/addParameters.js
Outdated
const resourceParameters = []; | ||
const { parameters = {} } = response; | ||
|
||
parameters.forEach(parameter => { |
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.
You only used parameter.variable
, parameter.property
and parameter.required
. Maybe could you use destructuring?
parameters.forEach(({ property = null, required, variable }) => {
if (null === property) {
return;
}
// ...
resourceParameter.push(
new Parameter(variable, range, required, "")
);
});
src/hydra/addParameters.js
Outdated
let range = null; | ||
|
||
const resourceProperty = resource.fields.find( | ||
field => property === field.name |
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.
({ name }) => property === name
src/hydra/addParameters.js
Outdated
const resourceProperty = resource.fields.find( | ||
field => property === field.name | ||
); | ||
if (resourceProperty) { |
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.
const { range = null } = resource.fields.find(...) || {};
src/hydra/addParameters.js
Outdated
const promises = []; | ||
|
||
for (const resource of api.resources) { | ||
const promise = fetchResource(resource.url).then(response => { |
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.
It seems that only response.parameters
is needed.
In that case:
const promise = fetchResource(resource.url).then(({ parameters = {} }) => {
const resourceParameters = [];
parameters.forEach(({ property = null, required, variable }) => {
// ...
});
return resourceParameters;
});
src/hydra/addParameters.js
Outdated
return; | ||
} | ||
|
||
const { range = null } = resource.fields.find( |
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.
Is it sure to always match a field here? In my previous comment, I suggested to add a default value (|| {}
) to avoid runtime errors.
src/hydra/parseHydraDocumentation.js
Outdated
status: get(response, "status") | ||
}) | ||
) | ||
.then(({ api, response, status }) => { |
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.
.then(({ api, response, status }) => (
addParameters(api).then(api => ({ api, response, status }));
));
) { | ||
throw new Error( | ||
'The entrypoint definition has no "http://www.w3.org/ns/hydra/core#supportedProperty" key or it is not an array.' | ||
return fetchEntrypointAndDocs(entrypointUrl, options) |
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.
A lot of modifications in this file are only CS
changes. Could you remove them from this PR?
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.
Done, is it just to have visibility for your review ?
I will have to reuse yarn fix
before merging, right ?
Thanks @jfthuillier! Great PR. |
In parseHydraDocumentation > addFilters
I made the choice to ignore filters with null property (PropertyFilter provides null).
Are you ok with this ?