Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix combineDataProviders doesn't work when returned by an async function #9798

Merged
merged 1 commit into from
Apr 29, 2024

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Apr 29, 2024

Problem

I'm using combineDataProvider to combine a REST and a GraphQL API. As the GraphQL API needs to be built, I'm injecting the data provider as follows:

export const App = () => {
  const [dataProvider, setDataProvider] = useState<DataProvider | undefined>(
    undefined
  );
  useEffect(() => {
    buildDataProvider().then((provider) => setDataProvider(() => provider));
  }, []);

  if (!dataProvider) {
    return <div>Loading</div>;
  }

  return (
    <Admin dataProvider={dataProvider}>
      ...
    </Admin>
  );
};

The buildDataProvider function is async:

export const buildDataProvider = async () => {
  const graphQLDataProvider = await graphQlDataProvider({
    clientOptions: { uri: "http://localhost:3002/" },
  });
  const restDataProvider = jsonServerDataProvider("http://localhost:3001");
  const dataProvider = combineDataProviders((resource) => {
    switch (resource) {
      case "Product":
      case "Category":
        return graphQLDataProvider;
      default:
        return restDataProvider;
    }
  });
  return dataProvider;
};

This fails because of the way combineDataProvider handles then() calls.

@djhi djhi added this to the 4.16.17 milestone Apr 29, 2024
@djhi djhi merged commit 83f6ec3 into master Apr 29, 2024
12 checks passed
@djhi djhi deleted the fix-combine-data-providers-promise branch April 29, 2024 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants