我正在尝试将 react-admin 与 hasura 适配器一起使用。当我尝试使用杂物时,错误
The response to 'GET_LIST' must be like { data: ... }, but the received response does not have a 'data' key.
身份验证工作正常,但我看不到任何查询“associado”数据的请求。仅启动 AUTH_CHECK 请求,没有针对“associado”表的 graphql 请求。在没有任何请求的情况下抛出错误。怎么了?
CRUD associado 的文件:
import React from 'react';
import { List, Datagrid, TextField, Show, SimpleShowLayout, DateField } from 'react-admin';
export function AssociadoList(props) {
return (
<List {...props}>
<Datagrid>
<TextField source="nome" />
<DateField source="data_aniversario" />
{/*<ReferenceField label="Endereço" source="enderecoId" reference="enderecos">
<TextField source="bairro" />
<TextField source="bairro" />
</ReferenceField>*/}
</Datagrid>
</List>
)
};
export function AssociadoShow(props) {
return (
<Show {...props}>
<SimpleShowLayout>
<TextField source="nome" />
<DateField source="data_aniversario" />
</SimpleShowLayout>
</Show>
)
};
身份验证提供者:
import { AUTH_LOGIN, AUTH_LOGOUT, AUTH_ERROR, AUTH_CHECK } from 'react-admin';
import graphql from './graphqlClient';
import Cookies from 'universal-cookie';
import { GraphQLClient } from 'graphql-request';
const cookies = new Cookies();
const LOGIN = `
mutation($username:String!, $password:String!){
login(username: $username, password: $password){
token
}
}
`;
const ME = `
query {
me {
username
}
}
`;
export default (type, params) => {
if (type === AUTH_LOGIN) {
const { username, password } = params;
return graphql.request(LOGIN, { username, password }).then(data => {
const token = data.login.token;
cookies.set("authToken", token, { path: "/", sameSite: "strict" });
});
}
if (type === AUTH_LOGOUT) {
cookies.remove("authToken", { path: "/", sameSite: "strict" });
return Promise.resolve();
}
if (type === AUTH_ERROR) {
const graphqlauth = new GraphQLClient(process.env.REACT_APP_HASURA_ENDPOINT, {
headers: {
authorization: 'Bearer ' + cookies.get('authToken'),
},
})
return graphqlauth.request(ME).then(data => { return (data.me.username) ? Promise.resolve() : Promise.reject(); }).catch(e => {
cookies.remove("authToken", { path: "/", sameSite: "strict" });
});
}
if (type === AUTH_CHECK) {
return cookies.get('authToken') ? Promise.resolve() : Promise.reject();
}
return Promise.resolve();
};
和 App.js;
import React from 'react';
import { Admin, Resource } from 'react-admin';
import Dashboard from './Dashboard';
import hasuraDataProvider from 'ra-data-hasura';
import authProvider from './authProvider';
import Cookies from 'universal-cookie';
import { AssociadoList, AssociadoShow } from "./Associado";
import portugueseMessages from 'ra-language-portuguese'
const dataProvider = function () {
const cookies = new Cookies();
const dataProvider = hasuraDataProvider(process.env.REACT_APP_HASURA_URL, { "content-type": "application/json", "Authorization": "Bearer " + cookies.get("AuthToken") });
return dataProvider;
}
const messages = {
'pt': portugueseMessages,
};
const i18nProvider = locale => messages[locale];
function App() {
return (
<Admin
dataProvider={dataProvider}
authProvider={authProvider}
dashboard={Dashboard}
locale="pt" i18nProvider={i18nProvider}
>
<Resource name="adear.associado" list={AssociadoList} options={{ label: 'Associado' }} show={AssociadoShow} />
</Admin>
);
}
export default App;
这是我的 package.json
{
"name": "dashboard",
"version": "0.1.0",
"private": true,
"dependencies": {
"graphql-request": "^1.8.2",
"ra-data-hasura": "^0.0.6",
"ra-language-portuguese": "^1.5.1",
"react": "^16.8.6",
"react-admin": "2.9.5",
"react-cookie": "^4.0.1",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}