Skip to content
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

comprehensive param support for ra-data-graphql-simple adapter GET_MANY_REFERENCE requests #3759

Merged
merged 6 commits into from
Nov 21, 2019
146 changes: 81 additions & 65 deletions packages/ra-data-graphql-simple/src/buildVariables.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,89 +100,104 @@ const buildGetListVariables = introspectionResults => (
aorFetchType,
params
) => {
const 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(
(filter_acc, k) => ({
...filter_acc,
[`${k}_in`]: params.filter[key][k],
}),
{}
);
return { ...acc, [`${key}_some`]: filter };
let variables = { filter: {} };
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]
f => f.name === key
);
const type = getFinalType(resourceField.type);
return { ...acc, [key]: sanitizeValue(type, params.filter[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]]),
[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);
}

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

return variables;
};

const buildCreateUpdateVariables = (
Expand Down Expand Up @@ -247,13 +262,14 @@ export default introspectionResults => (
};
case GET_MANY_REFERENCE: {
const parts = preparedParams.target.split('.');
return {
page: parseInt(preparedParams.pagination.page, 10) - 1,
perPage: parseInt(preparedParams.pagination.perPage, 10),
sortField: preparedParams.sort.field,
sortOrder: preparedParams.sort.order,
filter: { [`${parts[0]}Id`]: preparedParams.id },
};
let variables = buildGetListVariables(introspectionResults)(
resource,
aorFetchType,
preparedParams,
queryType
);
variables.filter[`${parts[0]}Id`] = preparedParams.id;
return variables;
}
case GET_ONE:
case DELETE:
Expand Down