graphql - React Admin /通过'ra-data-graphql'库构建数据提供者 - 自动注入的'raFetchType'参数未定义
问题描述
我正在尝试通过创建一个 dataProvider ra-data-graphql
,如库的 README中所述,并且成功地将自省响应传递给该buildQuery
方法,但raFetchType
我的buildQuery
方法中的参数始终未定义,并且应该发生的查询映射失败。看起来好像raFetchType
应该由库自动填充,或者我(或文档)遗漏了一些东西。
buildCustomQuery
注入时我需要提供一些额外的选项buildGraphQLProvider
吗?
您所期望的:
raFetchType
已定义(我假设为“GET_LIST”、“GET_ONE”等之一)
相反发生了什么:(在库的源代码中调用)中
的raFetchType
参数始终未定义buildQuery
aorFetchType
相关代码:
// App.tsx
const [dataProvider, setDataProvider] = useState<DataProvider>();
const cache = new InMemoryCache();
const link = createHttpLink({
uri: // my graphql API URL
});
const client = new ApolloClient({
cache: cache,
link: link,
});
useEffect(() => {
buildGraphQLProvider({
client: { client },
buildQuery: buildCustomQuery,
introspection: {
schema, // JSON previously fetched from my GraphQL API
operationNames: introspectionOperationNames, // function that maps the 'Country' resource to the correct query name for 'GET_LIST'
include: ['Country'], // just trying it out with a single Resource first
},
}).then((dataProvider: any) => {
setDataProvider(dataProvider);
})
}, []);
// this is called via `buildGraphQLProvider` on init
export const buildCustomQuery = (introspectionResults: any) => (raFetchType: any, resourceName: any, params: any) => {
// the introspectionResults contain the 4 properties listed here: https://github.com/marmelab/react-admin/tree/master/packages/ra-data-graphql#specify-your-queries-and-mutations
// including the 'Country' resource with the mapped query name
// however, `raFetchType` is undefined, and so no query can ever be returned.
// I get 'TypeError: Cannot read property 'parseResponse' of undefined' on ra-data-graphql/esm/index.js:113
const resource = introspectionResults.resources.find((r: IntrospectionResource) => r.type.name === resourceName);
switch (raFetchType) {
case 'GET_LIST':
return {
query: gql`query ${resource[raFetchType].name}($id: ID) {
data: ${resource[raFetchType].name}(id: $id) {
...
}
}
}`,
variables: params, // params = { id: ... }
parseResponse: (response: any) => response.data,
}
break;
// ... other types handled here
default:
return undefined;
}
}
环境
- 反应管理员版本:3.11.0
- 反应版本:17.0.1
- 浏览器:铬
- 堆栈跟踪:
Uncaught (in promise) TypeError: Cannot read property 'parseResponse' of undefined at raDataProvider (index.js:113)
解决方案
推荐阅读
- flutter - Flutter - 将一个视频导航到另一个视频时出现初始化音频错误
- javascript - 使用 AJAX(Laravel)将数据传递给 php 时出错
- haskell - 如何在 Haskell Stack 测试中对 Maybe 值使用 putStrLn?
- python - 加权平均
- r - 为 csv 输出文件 R 创建一个循环
- javascript - Javascript Firebase 身份验证和 Firestore:保存用户数据之前的页面重定向
- django-templates - django-tables2:在表头中避免大写字母的更好方法
- c++ - 如何在不使用任何布尔值的情况下重复循环?
- html - 如何阻止我的 css 在 django 中重叠?
- java - 服务器设置 Spring Boot & Docker & Traefik