Skip to content

Commit

Permalink
ensure params.sort and params.pagination have values before accessing…
Browse files Browse the repository at this point in the history
… object attrs
  • Loading branch information
maxschridde1494 committed Oct 2, 2019
1 parent fb5bf3e commit bffcffa
Showing 1 changed file with 71 additions and 64 deletions.
135 changes: 71 additions & 64 deletions packages/ra-data-graphql-simple/src/buildVariables.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,89 +100,96 @@ const buildGetListVariables = introspectionResults => (
aorFetchType,
params
) => {
const filter = params.filter ? Object.keys(params.filter).reduce((acc, key) => {
if (key === 'ids') {
return { ...acc, ids: params.filter[key] };
}

if (typeof params.filter[key] === 'object') {
const type = introspectionResults.types.find(
t => t.name === `${resource.type.name}Filter`
);
const filterSome = type.inputFields.find(
t => t.name === `${key}_some`
);

if (filterSome) {
const filter = Object.keys(params.filter[key]).reduce(
(acc, k) => ({
...acc,
[`${k}_in`]: params.filter[key][k],
}),
{}
);
return { ...acc, [`${key}_some`]: filter };
let variables = { };
if (params.filter) {
variables.filter = Object.keys(params.filter).reduce((acc, key) => {
if (key === 'ids') {
return { ...acc, ids: params.filter[key] };
}
}

const parts = key.split('.');

if (parts.length > 1) {
if (parts[1] === 'id') {
if (typeof params.filter[key] === 'object') {
const type = introspectionResults.types.find(
t => t.name === `${resource.type.name}Filter`
);
const filterSome = type.inputFields.find(
t => t.name === `${parts[0]}_some`
t => t.name === `${key}_some`
);

if (filterSome) {
return {
...acc,
[`${parts[0]}_some`]: { id: params.filter[key] },
};
const filter = Object.keys(params.filter[key]).reduce(
(acc, k) => ({
...acc,
[`${k}_in`]: params.filter[key][k],
}),
{}
);
return { ...acc, [`${key}_some`]: filter };
}
}

const parts = key.split('.');

if (parts.length > 1) {
if (parts[1] === 'id') {
const type = introspectionResults.types.find(
t => t.name === `${resource.type.name}Filter`
);
const filterSome = type.inputFields.find(
t => t.name === `${parts[0]}_some`
);

if (filterSome) {
return {
...acc,
[`${parts[0]}_some`]: { id: params.filter[key] },
};
}

return { ...acc, [parts[0]]: { id: params.filter[key] } };
}

return { ...acc, [parts[0]]: { id: params.filter[key] } };
const resourceField = resource.type.fields.find(
f => f.name === parts[0]
);
const type = getFinalType(resourceField.type);
return { ...acc, [key]: sanitizeValue(type, params.filter[key]) };
}

const resourceField = resource.type.fields.find(
f => f.name === parts[0]
);
const type = getFinalType(resourceField.type);
return { ...acc, [key]: sanitizeValue(type, params.filter[key]) };
}
const resourceField = resource.type.fields.find(f => f.name === key);

const resourceField = resource.type.fields.find(f => f.name === key);
if (resourceField) {
const type = getFinalType(resourceField.type);
const isAList = isList(resourceField.type);

if (resourceField) {
const type = getFinalType(resourceField.type);
const isAList = isList(resourceField.type);
if (isAList) {
return {
...acc,
[key]: Array.isArray(params.filter[key])
? params.filter[key].map(value =>
sanitizeValue(type, value)
)
: sanitizeValue(type, [params.filter[key]]),
};
}

if (isAList) {
return {
...acc,
[key]: Array.isArray(params.filter[key])
? params.filter[key].map(value =>
sanitizeValue(type, value)
)
: sanitizeValue(type, [params.filter[key]]),
};
return { ...acc, [key]: sanitizeValue(type, params.filter[key]) };
}

return { ...acc, [key]: sanitizeValue(type, params.filter[key]) };
}

return { ...acc, [key]: params.filter[key] };
}, {}) : {};
return { ...acc, [key]: params.filter[key] };
}, {})
}

if (params.pagination) {
variables.page = parseInt(params.pagination.page, 10) - 1
variables.perPage = parseInt(params.pagination.perPage, 10)
}

if (params.sort){
variables.sortField = params.sort.field
variables.sortOrder = params.sort.order
}

return {
page: parseInt(params.pagination.page, 10) - 1,
perPage: parseInt(params.pagination.perPage, 10),
sortField: params.sort.field,
sortOrder: params.sort.order,
filter,
};
return variables
};

const buildCreateUpdateVariables = () => (
Expand Down

0 comments on commit bffcffa

Please sign in to comment.