diff --git a/packages/ra-data-graphql-simple/src/buildVariables.js b/packages/ra-data-graphql-simple/src/buildVariables.js index f542e54e7ec..4cc300894b6 100644 --- a/packages/ra-data-graphql-simple/src/buildVariables.js +++ b/packages/ra-data-graphql-simple/src/buildVariables.js @@ -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 = ( @@ -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: