If I have multiple distinct REST API back-ends (separate apps doing separate things), and I want a single UI (react-admin-based) app that is capable of doing CRUD to entities managed by these various back-ends, I'm curious if it's possible to wire up react-admin to do that.
I'm imagining that instead of this (single/global dataProvider):
const App = () => (
<Admin dataProvider={simpleRestProvider('http://path.to.foo.api')}>
<Resource name="foos" list={FooList} />
</Admin>
);
we could do something like this (resource-specific dataProviders):
const App = () => (
<Admin >
<Resource name="foos" list={FooList}
dataProvider={simpleRestProvider('http://path.to.foo.api')} />
<Resource name="bars" list={BarList}
dataProvider={simpleRestProvider('http://path.to.bar.api')} />
</Admin>
);
At any rate, if you have advice on how I can do REST to multiple back-ends in react-admin, I'd appreciate it very much.
No, but you can have a super dataProvivder which would select the appropriate one depending on the resource. Something like:
const dataProviders = [
{ dataProvider: simpleRestProvider('http://path.to.foo.api'), resources: ['foos'] },
{ dataProvider: simpleRestProvider('http://path.to.bar.api'), resources: ['bars'] },
];
export default (type, resource, params) => {
const dataProviderMapping = dataProviders.find(dp => dp.resources.includes(resource));
return dataProviderMapping.dataProvider(type, resource, params);
}
In the README of recent ra release (3.4.1), there is a section (Extending a Data Provider (Example of File Upload)) that mentions: "... combine two Data Providers for two backends into a single object...". You can write a wrapper with branches to relay to different provider's implementation. An example to add your real data provider on top of their demo is:
// new file dataProvider/super.js
import simpleRestProvider from 'ra-data-simple-rest';
import delayedDataProvider from './rest'
const realDataProvider = simpleRestProvider('http://real_world_url/');
const myDataProvider = {
...delayedDataProvider,
getList: (resource, params) => {
console.log(resource)
if (["customers", "reviews", "commands"].includes(resource)) {
// resources for the demo
return delayedDataProvider.getList(resource, params)
} else {
// your own resources
return realDataProvider.getList(resource, params)
}
},
};
export default myDataProvider;
and replace the delayedDataProvider with the above myDataProvider
// file dataProvider/index.js
case 'graphql':
return import('./graphql').then(factory => factory.default());
default:
- return import('./rest').then(provider => provider.default);
+ return import('./super').then(provider => provider.default);
Disclaimer: I got the same error as previous posts "provider is not function..."
you can make custom of resource choose to which api you will use.
one admin only have one dataProvider.
<Admin
dataProvider={superDataProvider}
/>
but you can do like this:
import superDataProvider from './dataProviderFactory';
following is my code you can reference
import dataProviderRuby from './dataProvider'; //handle ruby dataProvider
import dataProviderJava from './dataProviderForJava';// handle java dataProvider
import { rubyClient, javaClient } from './apolloClient';//custom two diff client one will fetch ruby service ,other will java
const IsOmsResource = resource => {
const omsReource = [
'markets',
'regions',
'countries',
'states',
'items',
'salesOrganizations',
];
return omsReource.some(ele => ele === resource);
}; //those resource will fetch data from java service others will go to ruby
const rubyDataProvider = async (type, resource, params) => {
const provider = await dataProviderRuby({
client: rubyClient,
});
return provider(type, resource, params);
};
const javaDataProvider = async (type, resource, params) => {
const provider = await dataProviderJava({
client: javaClient,
});
return provider(type, resource, params);
};
const superDataProvider = (type, resource, params) => {
if (IsOmsResource(resource)) {
console.log('当前Java', resource);
return javaDataProvider(type, resource, params);
}
console.log('当前ruby', resource);
return rubyDataProvider(type, resource, params);
};
export default superDataProvider;
following is the './apolloClient'
import ApolloClient from 'apollo-client';
import { createHttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { setContext } from 'apollo-link-context';
const httpLinkRuby = createHttpLink({
uri: '/graphql',
});
const httpLinkJava = createHttpLink({
uri: '/oms-graphql',
});
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem('token');
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : '',
},
};
});
export const rubyClient = new ApolloClient({
link: httpLinkRuby,
cache: new InMemoryCache(),
});
export const javaClient = new ApolloClient({
link: authLink.concat(httpLinkJava),
cache: new InMemoryCache(),
});