benwinding / react-admin-import-csv

A csv file import button for react-admin
https://benwinding.github.io/react-admin-import-csv
MIT License
135 stars 47 forks source link

Unsupported fetch action type createMany #69

Closed HarryF514 closed 2 years ago

HarryF514 commented 3 years ago

When I tried to use the import function, I am seeing:

Unsupported fetch action type createMany

could you please provide an example of how to implement createMany

benwinding commented 3 years ago

Hi @HarryF514,

Going to need a bit more detail to get an understanding of that error message, but here's a quick example of how to implement the .createMany() in a dataprovider, basically you wrap the dataprovider and implement functionality as shown here too.

  // React admin 3.x
  const dataProviderWrapped = {
    ...dataProvider,
    createMany: async (resource, params) => {
      const items = params.data;
      // Handle create many here
    }
  }
  // 
  return (
    <Admin dataProvider={dataProviderWrapped}

Will add this to the readme too.

Cheers, Ben

techpet commented 3 years ago

When I tried to use the import function, I am seeing:

Unsupported fetch action type createMany

could you please provide an example of how to implement createMany

I am getting a similar error:

No query or mutation matching fetch type createMany could be found for resource ...

That's weird because based on the documentation,

If it doesn't exist, it will fallback to calling .create() on all items

Shouldn't it use .create() method by default as a fallback when createMany method is not defined?

hnaidin commented 2 years ago

Have the same issue, it's not falling back to .create() on all items

a3626a commented 2 years ago

Implementing CreateMany is always better than relying on the fallback. However, in some cases CreateMany is not supported at api(server) level. In those cases, implementing CreateMany is just repeating this code : https://github.com/benwinding/react-admin-import-csv/blob/9b7b840f76270fe7c444710881ed781a8d83c830/src/uploader.ts#L106-L123

Therefore, I think relying on the fallback is better if the api server does not support CreateMany. Fallback condition depends on the error raised, Unknown dataProvider : https://github.com/benwinding/react-admin-import-csv/blob/9b7b840f76270fe7c444710881ed781a8d83c830/src/uploader.ts#L147

Error message for missing data provider function varies depending on data provider implementations. So in many cases, this fallback does not work without additional implmentations. In my case, I catch error from the data provider and throw "Unknown dataProvider".

try {
  ...
} catch (error) {
    if (error.toString().includes("Unsupported Data Provider")) {
        throw "Unknown dataProvider"
    } else {
        throw error;
    }
}

Making fallback work for various data provider implementions looks like difficult. Because there are no protocol to signal missing data provider functions in react admin. I think providing a tutorial for this would be the best.

benwinding commented 2 years ago

Exactly right @a3626a,

There's no way to know if a DataProvider implements the createMany method. I've asked about this and the official response from the react-admin creator is:

Yep: call createMany in a try/catch, and fallback to create in the catch block.

Not sure what can be done here, @a3626a do you know if the error message has changed to "Unsupported Data Provider" or is that just your data-provider?

a3626a commented 2 years ago

Not sure what can be done here, @a3626a do you know if the error message has changed to "Unsupported Data Provider" or is that just your data-provider?

That's not a react-admin's official one, and also not my own. It is a custom implementation of data provider : https://github.com/henvo/ra-jsonapi-client/blob/eabbacf66f4051168a16bdaf17b2095bf19fd7f6/src/index.js#L137-L139

I think catching all those custom errors is impossible.

Talhafayyaz11 commented 2 years ago

Does anyone solve this issue? I am also having this issue while uploading CSV.
I get this error : No query or mutation matching fetch type createMany could be found for resource Event. I also tried to add createMany in Event resource type but no success.

benwinding commented 2 years ago

Version 1.0.31 should fix the issue here. The new version should automatically handle those additional error cases.

But if you're still getting an error from createMany try disabling it by using disableCreateMany in the options.

Let me know if there's any issues