ReferenceInput :使用 GET_ONE 而不是 GET_MANY 就像在 admin-on-rest 中所做的那样
ReferenceInput : using GET_ONE instead of GET_MANY like it was done in admin-on-rest
我正在从 admin-on-rest 迁移到 react-admin
使用完全相同的 Edit/SimpleForm/ReferenceInput 组合,admin-on-rest(似乎使用 GET_ONE)和 react-admin(似乎使用 GET_MANY)之间的行为不同
问题是我的后端 API 不支持 GET_MANY
这是否意味着我必须修改我的数据提供程序才能以某种方式将 GET_MANY 转换为多个 GET_ONE 调用?
如果是这样,您能否提供一个基本示例来做到这一点。
请注意,这可能是 https://marmelab.com/react-admin/Inputs.html#referenceinput 文档的一部分,因为我认为这并不罕见
等待我的问题的明确答案,这是我在我的数据提供者中所做的基于 https://marmelab.com/react-admin/DataProviders.html#example-request-processing :
const convertRESTRequestToHTTP = (type, resource, params) => new Promise((resolve, reject) => {
let url = '';
const options = {};
switch (type) {
/* see other cases in the doc */
case GET_MANY: {
//our API does not support GET_MANY
//=> we fallback to GET_ONE if there is a single id,
// otherwise we throw an error
// also see convertHTTPResponseToREST to transform the JSON
if (params.ids.length === 1) {
url = `${apiUrl}/${resource}/${params.ids[0]}`;
break;
}
throw new Error('the API does not support GET_MANY')
}
}
resolve({ url, options });
});
const convertHTTPResponseToREST = (response, type, resource, params) => {
const { json } = response;
switch (type) {
/* see other cases in the doc */
case GET_MANY:
//see explanantion in convertRESTRequestToHTTP
if (params.ids.length !== 1) {
throw new Error('the API does not support GET_MANY')
}
return { data: [json] };
}
};
我正在从 admin-on-rest 迁移到 react-admin
使用完全相同的 Edit/SimpleForm/ReferenceInput 组合,admin-on-rest(似乎使用 GET_ONE)和 react-admin(似乎使用 GET_MANY)之间的行为不同
问题是我的后端 API 不支持 GET_MANY
这是否意味着我必须修改我的数据提供程序才能以某种方式将 GET_MANY 转换为多个 GET_ONE 调用?
如果是这样,您能否提供一个基本示例来做到这一点。
请注意,这可能是 https://marmelab.com/react-admin/Inputs.html#referenceinput 文档的一部分,因为我认为这并不罕见
等待我的问题的明确答案,这是我在我的数据提供者中所做的基于 https://marmelab.com/react-admin/DataProviders.html#example-request-processing :
const convertRESTRequestToHTTP = (type, resource, params) => new Promise((resolve, reject) => {
let url = '';
const options = {};
switch (type) {
/* see other cases in the doc */
case GET_MANY: {
//our API does not support GET_MANY
//=> we fallback to GET_ONE if there is a single id,
// otherwise we throw an error
// also see convertHTTPResponseToREST to transform the JSON
if (params.ids.length === 1) {
url = `${apiUrl}/${resource}/${params.ids[0]}`;
break;
}
throw new Error('the API does not support GET_MANY')
}
}
resolve({ url, options });
});
const convertHTTPResponseToREST = (response, type, resource, params) => {
const { json } = response;
switch (type) {
/* see other cases in the doc */
case GET_MANY:
//see explanantion in convertRESTRequestToHTTP
if (params.ids.length !== 1) {
throw new Error('the API does not support GET_MANY')
}
return { data: [json] };
}
};